代码:
ssmTest.zip

6.1 Mybatis整合Spring

6.1.1 初始化项目

  1. 整合⽬标
  • 数据库连接池以及事务管理都交给Spring容器来完成
  • SqlSessionFactory对象应该放到Spring容器中作为单例对象管理
  • Mapper动态代理对象交给Spring管理,我们从Spring容器中直接获得Mapper的代理对象
  1. 整合所需 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
  1. 整合后的pom

    1. <!--junit-->
    2. <dependency>
    3. <groupId>junit</groupId>
    4. <artifactId>junit</artifactId>
    5. <version>4.12</version>
    6. <scope>test</scope>
    7. </dependency>
    8. <!--mybatis-->
    9. <dependency>
    10. <groupId>org.mybatis</groupId>
    11. <artifactId>mybatis</artifactId>
    12. <version>3.4.5</version>
    13. </dependency>
    14. <!--spring相关-->
    15. <dependency>
    16. <groupId>org.springframework</groupId>
    17. <artifactId>spring-context</artifactId>
    18. <version>5.1.12.RELEASE</version>
    19. </dependency>
    20. <dependency>
    21. <groupId>org.springframework</groupId>
    22. <artifactId>spring-test</artifactId>
    23. <version>5.1.12.RELEASE</version>
    24. </dependency>
    25. <dependency>
    26. <groupId>org.springframework</groupId>
    27. <artifactId>spring-jdbc</artifactId>
    28. <version>5.1.12.RELEASE</version>
    29. </dependency>
    30. <dependency>
    31. <groupId>org.springframework</groupId>
    32. <artifactId>spring-tx</artifactId>
    33. <version>5.1.12.RELEASE</version>
    34. </dependency>
    35. <dependency>
    36. <groupId>org.springframework</groupId>
    37. <artifactId>spring-aop</artifactId>
    38. <version>5.1.12.RELEASE</version>
    39. </dependency>
    40. <dependency>
    41. <groupId>org.aspectj</groupId>
    42. <artifactId>aspectjweaver</artifactId>
    43. <version>1.8.9</version>
    44. </dependency>
    45. <!--mybatis与spring的整合包-->
    46. <dependency>
    47. <groupId>org.mybatis</groupId>
    48. <artifactId>mybatis-spring</artifactId>
    49. <version>2.0.3</version>
    50. </dependency>
    51. <!--数据库驱动jar-->
    52. <dependency>
    53. <groupId>mysql</groupId>
    54. <artifactId>mysql-connector-java</artifactId>
    55. <version>5.1.46</version>
    56. </dependency>
    57. <!--druid连接池-->
    58. <dependency>
    59. <groupId>com.alibaba</groupId>
    60. <artifactId>druid</artifactId>
    61. <version>1.1.21</version>
    62. </dependency>

    6.1.2 编写业务代码

  2. DAO层代码

    1. @Mapper
    2. public interface AccountMapper {
    3. /**
    4. * 定义dao层接口方法
    5. * @return User
    6. */
    7. List<User> listUser();
    8. }
  1. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  2. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.atm.mapper.AccountMapper">
  4. <select id="listUser" resultType="com.atm.pojo.User">
  5. SELECT * FROM `user`
  6. </select>
  7. </mapper>
  1. Service层代码

    1. @Service
    2. public interface AccountService {
    3. /**
    4. * 定义service层接口方法
    5. * @return User
    6. */
    7. List<User> listUser();
    8. }
    1. @Service
    2. @Transactional(rollbackFor = Exception.class)
    3. public class AccountServiceImpl implements AccountService {
    4. @Autowired
    5. AccountMapper accountMapper;
    6. @Override
    7. public List<User> listUser() {
    8. return accountMapper.listUser();
    9. }
    10. }

    6.1.3 编写配置文件applicaition.xml

    主要是把一些类交给IOC容器管理

  2. 配置数据库连接Datasource

  3. 配置事务
  4. 配置sqlSessionFactory
  5. 配置代理对象 ```xml <?xml version=”1.0” encoding=”UTF-8”?> <beans xmlns=”http://www.springframework.org/schema/beans

    1. xmlns:context="http://www.springframework.org/schema/context"
    2. xmlns:tx="http://www.springframework.org/schema/tx"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="
    5. http://www.springframework.org/schema/beans
    6. http://www.springframework.org/schema/beans/spring-beans.xsd
    7. http://www.springframework.org/schema/context
    8. http://www.springframework.org/schema/context/spring-context.xsd
    9. http://www.springframework.org/schema/tx
    10. http://www.springframework.org/schema/tx/spring-tx.xsd
    11. "

    >

  1. <a name="echjN"></a>
  2. ## 6.1.4 测试
  3. 编写测试类
  4. ```java
  5. @RunWith(SpringJUnit4ClassRunner.class) // Spring测试类配置自动启动容器
  6. @ContextConfiguration(locations = {"classpath*:application*.xml"}) // 容器配置文件
  7. public class MybatisSpringTest {
  8. @Autowired
  9. AccountService accountService;
  10. @Test
  11. public void test() {
  12. List<User> users = accountService.listUser();
  13. System.out.println(users);
  14. }
  15. }

6.2 整合SpringMVC

6.2.1 初始化

  1. 引入依赖

    1. <!--SpringMVC-->
    2. <dependency>
    3. <groupId>org.springframework</groupId>
    4. <artifactId>spring-webmvc</artifactId>
    5. <version>5.1.12.RELEASE</version>
    6. </dependency>
    7. <!--jsp-api&servlet-api-->
    8. <dependency>
    9. <groupId>javax.servlet</groupId>
    10. <artifactId>jsp-api</artifactId>
    11. <version>2.0</version>
    12. <scope>provided</scope>
    13. </dependency>
    14. <dependency>
    15. <groupId>javax.servlet</groupId>
    16. <artifactId>javax.servlet-api</artifactId>
    17. <version>3.1.0</version>
    18. <scope>provided</scope>
    19. </dependency>
    20. <!--⻚⾯使⽤jstl表达式-->
    21. <dependency>
    22. <groupId>jstl</groupId>
    23. <artifactId>jstl</artifactId>
    24. <version>1.2</version>
    25. </dependency>
    26. <dependency>
    27. <groupId>taglibs</groupId>
    28. <artifactId>standard</artifactId>
    29. <version>1.1.2</version>
    30. </dependency>
    31. <!--json数据交互所需jar,start-->
    32. <dependency>
    33. <groupId>com.fasterxml.jackson.core</groupId>
    34. <artifactId>jackson-core</artifactId>
    35. <version>2.9.0</version>
    36. </dependency>
    37. <dependency>
    38. <groupId>com.fasterxml.jackson.core</groupId>
    39. <artifactId>jackson-databind</artifactId>
    40. <version>2.9.0</version>
    41. </dependency>
    42. <dependency>
    43. <groupId>com.fasterxml.jackson.core</groupId>
    44. <artifactId>jackson-annotations</artifactId>
    45. <version>2.9.0</version>
    46. </dependency>
  2. 配置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“ >

Archetype Created Web Application contextConfigLocation classpath:applicationContext.xml org.springframework.web.context.ContextLoaderListener

springmvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springmvc.xml 1 springmvc /

  1. ```xml
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/mvc
  12. http://www.springframework.org/schema/mvc/spring-mvc.xsd"
  13. >
  14. <!--扫描包-->
  15. <context:component-scan base-package="com.atm.controller"/>
  16. <!--配置springmvc注解驱动,自动注册适合的组件HandlerMapping和HandlerAdapter spring5.x不需要配置了-->
  17. <mvc:annotation-driven/>
  18. </beans>

6.2.2 代码编写

  1. @RequestMapping("account")
  2. @Controller
  3. public class AccountController {
  4. /**
  5. * spring和springmvc容器是有层次的
  6. * spring容器:管理service层和dao层对象
  7. * springmvc容器:管理controller层对象,可以引用spring容器中的对象
  8. */
  9. @Autowired
  10. AccountService accountService;
  11. @RequestMapping("queryUser")
  12. @ResponseBody
  13. public List<User> queryUser() {
  14. List<User> users = accountService.listUser();
  15. return users;
  16. }
  17. }