您现在的位置是:首页 > 个人日记个人日记

spring-10-整合mybatis-DAO

2019-02-03 18:51:35【个人日记】345人已围观

简介使用DAO的方式整合mybatis和spring

整合mybatis-DAO

这次jar包较多

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.huangxin</groupId>
  7. <artifactId>mybatis-spring</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <dependencies>
  10. <!--log记录-->
  11. <dependency>
  12. <groupId>log4j</groupId>
  13. <artifactId>log4j</artifactId>
  14. <version>1.2.17</version>
  15. </dependency>
  16. <!--MySQL驱动-->
  17. <dependency>
  18. <groupId>mysql</groupId>
  19. <artifactId>mysql-connector-java</artifactId>
  20. <version>5.1.46</version>
  21. </dependency>
  22. <!-- mybatis.jar -->
  23. <dependency>
  24. <groupId>org.mybatis</groupId>
  25. <artifactId>mybatis</artifactId>
  26. <version>3.4.6</version>
  27. </dependency>
  28. <!-- junit.jar -->
  29. <dependency>
  30. <groupId>junit</groupId>
  31. <artifactId>junit</artifactId>
  32. <version>4.12</version>
  33. <scope>test</scope>
  34. </dependency>
  35. <!--spring框架导入-->
  36. <dependency>
  37. <groupId>org.springframework</groupId>
  38. <artifactId>spring-context</artifactId>
  39. <version>4.3.17.RELEASE</version>
  40. </dependency>
  41. <!--spring-test-->
  42. <dependency>
  43. <groupId>org.springframework</groupId>
  44. <artifactId>spring-test</artifactId>
  45. <version>4.3.17.RELEASE</version>
  46. <scope>test</scope>
  47. </dependency>
  48. <!--AspectJ AOP环境-->
  49. <dependency>
  50. <groupId>org.springframework</groupId>
  51. <artifactId>spring-aspects</artifactId>
  52. <version>4.3.17.RELEASE</version>
  53. </dependency>
  54. <!--druid连接池-->
  55. <dependency>
  56. <groupId>com.alibaba</groupId>
  57. <artifactId>druid</artifactId>
  58. <version>1.1.12</version>
  59. </dependency>
  60. <!--spring和mybatis整合包-->
  61. <dependency>
  62. <groupId>org.mybatis</groupId>
  63. <artifactId>mybatis-spring</artifactId>
  64. <version>1.3.2</version>
  65. </dependency>
  66. <!--spring-jdbc依赖包-->
  67. <dependency>
  68. <groupId>org.springframework</groupId>
  69. <artifactId>spring-jdbc</artifactId>
  70. <version>4.3.17.RELEASE</version>
  71. </dependency>
  72. </dependencies>
  73. </project>

这时候直接使用druid连接池,并且交给spring管理
配置spring-config.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. 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">
  7. <!--扫描注解-->
  8. <context:component-scan base-package="com.huangxin.order"></context:component-scan>
  9. <!--AOP注解生效-->
  10. <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
  11. <!--加载配置文件-->
  12. <context:property-placeholder location="property/jdbc.properties"></context:property-placeholder>
  13. <!--druid连接池-->
  14. <bean id="dateSource" class="com.alibaba.druid.pool.DruidDataSource">
  15. <property name="url" value="${jdbc.url}"></property>
  16. <property name="username" value="${jdbc.username}"></property>
  17. <property name="password" value="${jdbc.password}"></property>
  18. </bean>
  19. <!--SqlSessionFactory工厂交给spring-->
  20. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  21. <!--将数据源注入SqlSessionFactory-->
  22. <property name="dataSource" ref="dateSource"></property>
  23. <!--引入映射-->
  24. <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
  25. </bean>
  26. <!--sqlSession交给spring管理-->
  27. <!--使用多例,因为有多个会话-->
  28. <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
  29. <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
  30. </bean>
  31. </beans>

因为在以上配置中已经将对象映射文件引入,所以已经不再使用mybatis-config.xml文件,可以将其删除

使用DAO方式实现映射

  1. package com.huangxin.order.mapper.impl;
  2. import com.huangxin.order.mapper.OrderMapper;
  3. import com.huangxin.order.model.Orders;
  4. import org.apache.ibatis.session.SqlSession;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Component;
  7. import java.util.List;
  8. @Component//将其注册为bean
  9. public class OrderMapperImpl implements OrderMapper {
  10. @Autowired//为其注入
  11. private SqlSession sqlSession;
  12. public Orders getById(Integer id) {
  13. Orders orders = sqlSession.selectOne("com.huangxin.order.mapper.OrderMapper.getById", id);
  14. //spring已经管理,不能调用
  15. // sqlSession.close();
  16. return orders;
  17. }
  18. }

这是对OrderMapper文件的实现,使用注解方式给对象注入

在Junit中直接调用即可

  1. package com.huangxin.order.mapper.impl;
  2. import com.huangxin.order.mapper.OrderMapper;
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.test.context.ContextConfiguration;
  7. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  8. import static org.junit.Assert.*;
  9. @RunWith(SpringJUnit4ClassRunner.class)
  10. @ContextConfiguration(locations = "classpath:spring-config.xml")//读取配置文件
  11. public class OrderMapperImplTest {
  12. @Autowired
  13. private OrderMapper orderMapper;
  14. @Test
  15. public void getById() {
  16. System.out.println(orderMapper.getById(1));
  17. }
  18. }

这是整合以后,以后开发方式改变

特点

spring已经管理整个SqlSession会话,以及工厂,并且自动提交事务,自动关闭会话,以后只需要写逻辑代码

Tags: JavaWeb  

上一篇: 创建博客心得

下一篇: spring-02-配置细节

评论区

    2024-12-21 20:15:11

    站长

    没有登录功能是为了方便大家留言,但留言接口现在被恶意攻击,将关闭留言接口,如有疑问,请联系我的QQ 1538933906/同微信


文章评论



给自个选个头像吧!






站点信息

  • 建站时间:   2019-01-31
  • 网站程序:   Tomcat+nginx
  • 文章统计:   44篇文章
  • 标签管理:   标签云
  • 微信公众号:  扫描二维码,联系我