代码:
ssmTest.zip
6.1 Mybatis整合Spring
6.1.1 初始化项目
- 整合⽬标
- 数据库连接池以及事务管理都交给Spring容器来完成
- SqlSessionFactory对象应该放到Spring容器中作为单例对象管理
- Mapper动态代理对象交给Spring管理,我们从Spring容器中直接获得Mapper的代理对象
- 整合所需 Jar 分析
- unit测试jar(4.12版本)
- Mybatis的jar(3.4.5)
- Spring相关jar(spring-context、spring-test、spring-jdbc、spring-tx、spring-aop、aspectjweaver)
- Mybatis/Spring整合包jar(mybatis-spring-xx.jar)
- Mysql数据库驱动jar
- Druid数据库连接池的jar
整合后的pom
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--spring相关-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<!--mybatis与spring的整合包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.3</version>
</dependency>
<!--数据库驱动jar-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
6.1.2 编写业务代码
DAO层代码
@Mapper
public interface AccountMapper {
/**
* 定义dao层接口方法
* @return User
*/
List<User> listUser();
}
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atm.mapper.AccountMapper">
<select id="listUser" resultType="com.atm.pojo.User">
SELECT * FROM `user`
</select>
</mapper>
Service层代码
@Service
public interface AccountService {
/**
* 定义service层接口方法
* @return User
*/
List<User> listUser();
}
@Service
@Transactional(rollbackFor = Exception.class)
public class AccountServiceImpl implements AccountService {
@Autowired
AccountMapper accountMapper;
@Override
public List<User> listUser() {
return accountMapper.listUser();
}
}
6.1.3 编写配置文件applicaition.xml
主要是把一些类交给IOC容器管理
配置数据库连接Datasource
- 配置事务
- 配置sqlSessionFactory
配置代理对象 ```xml <?xml version=”1.0” encoding=”UTF-8”?> <beans xmlns=”http://www.springframework.org/schema/beans“
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
"
>
<a name="echjN"></a>
## 6.1.4 测试
编写测试类
```java
@RunWith(SpringJUnit4ClassRunner.class) // Spring测试类配置自动启动容器
@ContextConfiguration(locations = {"classpath*:application*.xml"}) // 容器配置文件
public class MybatisSpringTest {
@Autowired
AccountService accountService;
@Test
public void test() {
List<User> users = accountService.listUser();
System.out.println(users);
}
}
6.2 整合SpringMVC
6.2.1 初始化
引入依赖
<!--SpringMVC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<!--jsp-api&servlet-api-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--⻚⾯使⽤jstl表达式-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!--json数据交互所需jar,start-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
配置webapp下的web.xml:主要配置servlet,springmvc配置文件来启动springmvc,配置监听器来启动spring ```xml <!DOCTYPE web-app PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN” “http://java.sun.com/dtd/web-app_2_3.dtd“ >
```xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"
>
<!--扫描包-->
<context:component-scan base-package="com.atm.controller"/>
<!--配置springmvc注解驱动,自动注册适合的组件HandlerMapping和HandlerAdapter spring5.x不需要配置了-->
<mvc:annotation-driven/>
</beans>
6.2.2 代码编写
@RequestMapping("account")
@Controller
public class AccountController {
/**
* spring和springmvc容器是有层次的
* spring容器:管理service层和dao层对象
* springmvc容器:管理controller层对象,可以引用spring容器中的对象
*/
@Autowired
AccountService accountService;
@RequestMapping("queryUser")
@ResponseBody
public List<User> queryUser() {
List<User> users = accountService.listUser();
return users;
}
}