您现在的位置是:首页 > 个人日记个人日记
spring-11-整合mybatis-Mapper
2019-02-03 18:54:13【个人日记】1362人已围观
简介使用Mapper代理的方式整合mybatis和spring
整合mybatis-Mapper
配置spring-config.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"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"><!--扫描注解--><context:component-scan base-package="com.huangxin.order"></context:component-scan><!--AOP注解生效--><aop:aspectj-autoproxy></aop:aspectj-autoproxy><!--加载配置文件--><context:property-placeholder location="property/jdbc.properties"></context:property-placeholder><!--druid连接池--><bean id="dateSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean><!--SqlSessionFactory工厂交给spring--><bean id="sqlSessionFactory" name = "sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!--将数据源注入SqlSessionFactory--><property name="dataSource" ref="dateSource"></property><!--引入映射--><property name="mapperLocations" value="classpath:mapper/*.xml"></property></bean><!--<!–sqlSession交给spring管理–>--><!--<!–使用多例,因为有多个会话–>--><!--<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">--><!--<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>--><!--</bean>--><!--让spring生成mapper接口的实现类--><!--单个mapper接口--><!--MapperFactoryBean为mapper接口的代理实现类--><!--<bean id="orderMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">--><!--<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>--><!--<!–须将接口的全限定名给spring–>--><!--<property name="mapperInterface" value="com.huangxin.order.mapper.OrderMapper"></property>--><!--</bean>--><!--让spring生成mapper接口的实现类--><!--多个mapper接口--><!--MapperScannerConfigurer扫描所有mapper--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property><!--对哪些包下的类生成mapper代理类--><property name="basePackage" value="com.huangxin.order.mapper"></property></bean></beans>
删除原有的mapper代理
直接使用代理
package com.huangxin.order.mapper;import com.huangxin.order.model.Orders;import java.util.List;public interface OrderMapper {/*** 查询所有** @return*/List<Orders> getAll();/*** 通过id查询** @param id* @return*/Orders getById(Integer id);}
最后只需要在使用时注入就行
package com.huangxin.order.mapper;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.core.annotation.Order;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import static org.junit.Assert.*;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = "classpath:spring-config.xml")public class OrderMapperTest {@Autowiredprivate OrderMapper orderMapper;@Testpublic void getById() {System.out.println(orderMapper.getById(2));}}
注意
当只有一个接口则使用xml配置中的注释掉的部分
当然不可能只有一个接口,而且多个中也包括一个,所以直接使用多个接口的配置就行
并且使用时只需要将sqlSessionFactory传入给spring就行,不用在传给他sqlSession
最终清理完的代码为
<?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"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"><!--扫描注解--><context:component-scan base-package="com.huangxin.order"></context:component-scan><!--AOP注解生效--><aop:aspectj-autoproxy></aop:aspectj-autoproxy><!--加载配置文件--><context:property-placeholder location="property/jdbc.properties"></context:property-placeholder><!--druid连接池--><bean id="dateSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean><!--SqlSessionFactory工厂交给spring--><bean id="sqlSessionFactory" name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!--将数据源注入SqlSessionFactory--><property name="dataSource" ref="dateSource"></property><!--引入映射--><property name="mapperLocations" value="classpath:mapper/*.xml"></property></bean><!--让spring生成mapper接口的实现类--><!--多个mapper接口--><!--MapperScannerConfigurer将扫描所有包下的mapper--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property><!--对哪些包下的类生成mapper代理类--><property name="basePackage" value="com.huangxin.order.mapper"></property></bean></beans>
Tags: JavaWeb
上一篇: 阿里云建站-宝塔Linux面板
下一篇: spring-04-对象注入
相关文章
随机图文
评论区
2025-11-09 04:29:42
站长
没有登录功能是为了方便大家留言,但留言接口现在被恶意攻击,将关闭留言接口,如有疑问,请联系我的QQ 1538933906/同微信
