目录
一、表
二、创建项目
1.创建mavenJavaWeb项目
2.补齐目录
3.导入依赖
三、创建包结构
四、实体类
五、spring框架
1.service接口和实现类
(1)service接口
(2)实现类
2.applicationContext.xml配置文件
六、spring整合springMVC
1.web.xml
2.spring-mvc.xml
3、controller层
七、spring整合mybatis
1.AccountDao接口
2.AccountMapper.xml
3.service中注入dao对象
4.AccountController类
5.配置声明式事务管理
6.表单
八、运行
启动tomcat
查询所有:
控制台:
九、根据id查询
1.AccountDao接口
2.AccountMapper.xml
3.AccountService接口
4.AccountServiceImpl实现类
5.AccountController类
6.web.xml
7.index.jsp
8.运行
根据id查:
控制台:
十、添加数据
1.AccountDao接口
2.AccountMapper.xml
3.AccountService接口
4.AccountServiceImpl实现类
5.AccountController类
6.index.jsp
7.运行
添加:
控制台:
查看数据库:
十一、修改数据
1.AccountDao接口
2.AccountMapper.xml
3.AccountService接口
4.AccountServiceImpl实现类
5.AccountController类
6.index.jsp
7.运行
修改:
控制台:
查看数据库:
十二、删除数据
1.AccountDao接口
2.AccountMapper.xml
3.AccountService接口
4.AccountServiceImpl实现类
5.AccountController类
6.index.jsp
7.运行
根据id删除
一、表
account表
create database ssm;
create table account(
id int primary key auto_increment,
name varchar(20),
money double
);
二、创建项目
1.创建mavenJavaWeb项目
2.补齐目录
3.导入依赖
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qcby</groupId>
<artifactId>ssm1</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<properties>
<spring.version>5.0.2.RELEASE</spring.version>
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.12</log4j.version>
<mysql.version>5.1.6</mysql.version>
<mybatis.version>3.4.5</mybatis.version>
</properties>
<dependencies>
<!-- spring -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!--连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
</dependencies>
</project>
三、创建包结构
四、实体类
Account
package com.qcby.pojo;
public class Account {
private Integer id;
private String name;
private double money;
public Account() {
}
public Account(Integer id, String name, double money) {
this.id = id;
this.name = name;
this.money = money;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
五、spring框架
1.service接口和实现类
(1)service接口
package com.qcby.service;
import com.qcby.pojo.Account;
import java.util.List;
public interface AccountService {
/* * 查询所有 * */ public List<Account> findAll(); } |
(2)实现类
package com.qcby.service.impl;
import com.qcby.pojo.Account; import com.qcby.service.AccountService; import org.springframework.stereotype.Service;
import java.util.List;
@Service public class AccountServiceImpl implements AccountService { @Override public List<Account> findAll() { System.out.println("业务层,查询所有账号--------------"); return null; } } |
2.applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--开启注解扫描,扫描service--> <context:component-scan base-package="com.qcby.service" />
</beans> |
六、spring整合springMVC
1.web.xml
配置spring的监听器、解决post请求中文乱码的问题、配置前端控制器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
<!--配置spring的监听器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--配置加载类路径的配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
<!--解决post请求中文乱码的过滤器--> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<!--配置前端控制器--> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--加载spring-mvc.xml配置文件--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <!--启动加载--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
</web-app> |
2.spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置spring创建容器时要扫描的包 --> <context:component-scan base-package="com.qcby.controller"></context:component-scan>
<!-- 配置视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean>
<!-- 配置spring开启注解mvc的支持--> <mvc:annotation-driven></mvc:annotation-driven> </beans> |
3、controller层
controller中注入service对象
AccountController类
package com.qcby.controller;
import com.qcby.pojo.Account; import com.qcby.service.AccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller @RequestMapping("/account") public class AccountController { //注入 @Autowired private AccountService accountService; /* * 查询所有 * */ @RequestMapping("/findAll.do") public ModelAndView findAll(){ System.out.println("表现层:查询所有账号------");
List<Account> list=accountService.findAll();
ModelAndView modelAndView=new ModelAndView(); modelAndView.setViewName("success"); return modelAndView; } } |
七、spring整合mybatis
把mybatis-config.xml配置文件中的内容配置到applicationContext.xml配置文件中
1.AccountDao接口
package com.qcby.dao;
import com.qcby.pojo.Account; import org.springframework.stereotype.Repository;
import java.util.List;
@Repository public interface AccountDao { /* * 查询所有 * */ public List<Account> findAll(); } |
2.AccountMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.qcby.dao.AccountDao"> <!--对谁进行操作就写谁-->
<!--查询所有:public List<Account> findAll();--> <select id="findAll" resultType="com.qcby.pojo.Account"> select * from account; </select> </mapper> |
3.service中注入dao对象
package com.qcby.service.impl;
import com.qcby.dao.AccountDao; import com.qcby.pojo.Account; import com.qcby.service.AccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
import java.util.List;
@Service public class AccountServiceImpl implements AccountService { //注入 @Autowired private AccountDao accountDao; @Override public List<Account> findAll() { System.out.println("业务层,查询所有账号--------------"); return accountDao.findAll(); //return null; } } |
4.AccountController类
package com.qcby.controller;
import com.qcby.pojo.Account; import com.qcby.service.AccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller @RequestMapping("/account") public class AccountController {
//注入 @Autowired private AccountService accountService;
/* * 查询所有 * */ @RequestMapping("/findAll.do") public ModelAndView findAll(){ System.out.println("表现层:查询所有账号------");
List<Account> list=accountService.findAll(); for(Account account:list){ System.out.println(account); }
ModelAndView modelAndView=new ModelAndView(); modelAndView.setViewName("success"); return modelAndView; } } |
5.配置声明式事务管理
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--开启注解扫描,扫描service--> <context:component-scan base-package="com.qcby.service" />
<!--配置druid连接池--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql:///ssm" /> <property name="username" value="root" /> <property name="password" value="2020" /> </bean>
<!--spring整合mybatis--> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--注入连接池对象--> <property name="dataSource" ref="dataSource" /> <!--加载映射配置文件--> <property name="mapperLocations" value="classpath:com/qcby/mapper/*.xml" /> </bean>
<!--把dao对象存到IOC容器--> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactory" ref="sessionFactory" /> <!--扫描dao在的包,把包中接口的代码对象存入到IOC容器--> <property name="basePackage" value="com.qcby.dao" /> </bean>
<!--平台事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean>
<!--配置事务的通知--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!--所有以find开头的方法都是只读,不会对数据库进行修改--> <tx:method name="find*" read-only="true"/> <!--其他所有的方法都使用默认的事务设置--> <tx:method name="*" /> </tx:attributes> </tx:advice> <!--配置事务的增强--> <aop:config> <!-- ServiceImpl结尾的类,都会增强 --> <aop:advisor advice-ref="txAdvice" pointcut="execution(public * com.qcby.service.*ServiceImpl.*(..))" /> </aop:config> </beans> |
6.表单
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <body>
<h3>账号列表页面</h3> <a href="/account/findAll.do">查询所有</a>
</body> </html> |
八、运行
启动tomcat
查询所有:
控制台:
九、根据id查询
1.AccountDao接口
/* * 根据id查询 * */ public Account findById(Integer id); |
2.AccountMapper.xml
<!--根据id查询:public Account findById(Integer id);--> <select id="findById" parameterType="int" resultType="com.qcby.pojo.Account"> select * from account where id=#{id} </select> |
3.AccountService接口
/* * 根据id查询 * */ public Account findById(Integer id); |
4.AccountServiceImpl实现类
@Override public Account findById(Integer id) { System.out.println("业务层,根据id查询账号-----"); return accountDao.findById(id); } |
5.AccountController类
/* * 根据id查询 * */ @RequestMapping("/findById/{id}") public String findById(@PathVariable(value = "id") Integer id){ System.out.println("表现层,根据id查询账号-----"); Account account=accountService.findById(id); System.out.println("查询到的账户:"+account); return "success"; } |
6.web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
<!--<!–配置spring的监听器–> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!–配置加载类路径的配置文件–> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>-->
<!--解决post请求中文乱码的过滤器--> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<!--配置前端控制器--> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--加载spring-mvc.xml配置文件--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> <!--启动加载--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
</web-app> |
7.index.jsp
<h3>根据id查询账号</h3> <a href="/account/findById/1">根据id查</a> |
8.运行
根据id查:
控制台:
十、添加数据
1.AccountDao接口
/* * 添加 * */ public void insertAccount(Account account); |
2.AccountMapper.xml
<!--添加:public void insertAccount(Account account);--> <insert id="insertAccount" parameterType="com.qcby.pojo.Account"> insert into account(name,money) values (#{name},#{money}) </insert> |
3.AccountService接口
/* * 添加 * */ public void insertAccount(Account account); |
4.AccountServiceImpl实现类
@Override public void insertAccount(Account account) { System.out.println("业务层,添加账号-------"); accountDao.insertAccount(account); } |
5.AccountController类
/* * 添加 * */ @RequestMapping("/insertAccount.do") private String insertAccount(Account account){ System.out.println("表现层:添加账号------");
accountService.insertAccount(account);
return "success"; } |
6.index.jsp
form表单
<h3>添加账号</h3> <form action="/account/insertAccount.do" method="post"> 账号名称:<input type="text" name="name"><br> 账号金额:<input type="text" name="money"><br> <input type="submit" value="添加"> </form> |
7.运行
添加:
控制台:
查看数据库:
十一、修改数据
1.AccountDao接口
/* * 修改 * */ public void updateAccount(Account account); |
2.AccountMapper.xml
<!--修改:public void updateAccount(Account account);--> <update id="updateAccount" parameterType="com.qcby.pojo.Account"> update account <set> <if test="name!=null and name!=''"> name=#{name}, </if> <if test="money!=null"> money=#{money} </if> </set> where id=#{id} </update> |
3.AccountService接口
/* * 修改 * */ public void updateAccount(Account account); |
4.AccountServiceImpl实现类
@Override public void updateAccount(Account account) { System.out.println("业务层,修改账号-------"); accountDao.updateAccount(account); } |
5.AccountController类
/* * 修改 * */ @RequestMapping("/updateAccount.do") private String updateAccount(Account account){ System.out.println("表现层:修改账号------"); accountService.updateAccount(account); return "success"; } |
6.index.jsp
<h3>修改账号</h3> <form action="/account/updateAccount.do" method="post"> 账户id:<input type="text" name="id"><br> 账号名称:<input type="text" name="name"><br> 账号金额:<input type="text" name="money"><br> <input type="submit" value="修改"> </form> |
7.运行
修改:
控制台:
查看数据库:
修改成功!
十二、删除数据
1.AccountDao接口
/* * 删除 * */ public void deleteAccount(Integer id); |
2.AccountMapper.xml
<!--删除:public void deleteAccount(Integer id);--> <delete id="deleteAccount" parameterType="int"> delete from account where id=#{id} </delete> |
3.AccountService接口
/* * 删除 * */ public void deleteAccount(Integer id); |
4.AccountServiceImpl实现类
@Override public void deleteAccount(Integer id) { System.out.println("业务层,删除账号-------"); accountDao.deleteAccount(id); } |
5.AccountController类
/* * 删除 * */ @RequestMapping("/deleteAccount/{id}") private String deleteAccount(@PathVariable(value = "id") Integer id){ System.out.println("表现层:删除账号------"); accountService.deleteAccount(id); return "success"; } |
6.index.jsp
<h3>根据id删除账号</h3> <a href="/account/deleteAccount/5">根据id删除</a> |
7.运行
根据id删除
控制台:
查看数据库:
第五条数据没了