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

spring-11-整合mybatis-Mapper

2019-02-03 18:54:13【个人日记】971人已围观

简介使用Mapper代理的方式整合mybatis和spring

整合mybatis-Mapper

配置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" name = "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. <!--&lt;!&ndash;sqlSession交给spring管理&ndash;&gt;-->
  27. <!--&lt;!&ndash;使用多例,因为有多个会话&ndash;&gt;-->
  28. <!--<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">-->
  29. <!--<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>-->
  30. <!--</bean>-->
  31. <!--让spring生成mapper接口的实现类-->
  32. <!--单个mapper接口-->
  33. <!--MapperFactoryBean为mapper接口的代理实现类-->
  34. <!--<bean id="orderMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">-->
  35. <!--<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>-->
  36. <!--&lt;!&ndash;须将接口的全限定名给spring&ndash;&gt;-->
  37. <!--<property name="mapperInterface" value="com.huangxin.order.mapper.OrderMapper"></property>-->
  38. <!--</bean>-->
  39. <!--让spring生成mapper接口的实现类-->
  40. <!--多个mapper接口-->
  41. <!--MapperScannerConfigurer扫描所有mapper-->
  42. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  43. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
  44. <!--对哪些包下的类生成mapper代理类-->
  45. <property name="basePackage" value="com.huangxin.order.mapper"></property>
  46. </bean>
  47. </beans>

删除原有的mapper代理

直接使用代理

  1. package com.huangxin.order.mapper;
  2. import com.huangxin.order.model.Orders;
  3. import java.util.List;
  4. public interface OrderMapper {
  5. /**
  6. * 查询所有
  7. *
  8. * @return
  9. */
  10. List<Orders> getAll();
  11. /**
  12. * 通过id查询
  13. *
  14. * @param id
  15. * @return
  16. */
  17. Orders getById(Integer id);
  18. }

最后只需要在使用时注入就行

  1. package com.huangxin.order.mapper;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.core.annotation.Order;
  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 OrderMapperTest {
  12. @Autowired
  13. private OrderMapper orderMapper;
  14. @Test
  15. public void getById() {
  16. System.out.println(orderMapper.getById(2));
  17. }
  18. }

注意

当只有一个接口则使用xml配置中的注释掉的部分

当然不可能只有一个接口,而且多个中也包括一个,所以直接使用多个接口的配置就行

并且使用时只需要将sqlSessionFactory传入给spring就行,不用在传给他sqlSession

最终清理完的代码为

  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" name="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. <!--让spring生成mapper接口的实现类-->
  27. <!--多个mapper接口-->
  28. <!--MapperScannerConfigurer将扫描所有包下的mapper-->
  29. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  30. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
  31. <!--对哪些包下的类生成mapper代理类-->
  32. <property name="basePackage" value="com.huangxin.order.mapper"></property>
  33. </bean>
  34. </beans>

Tags: JavaWeb  

评论区

    2024-04-19 13:51:12

    站长

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


文章评论



给自个选个头像吧!






站点信息

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