Spring整合Mybatis

Spring和Mbatis整合的三种配置方式

源码:https://github.com/wmjGood/Spring_Day03

公共部分

jdbc.propertits文件:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///mybatis
jdbc.username=root
jdbc.password=0000

XML文件(部分):<?xml version="1.0" encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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
">
//引用jdbc.propertits文件
<
bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:jdbc.properties</value> </property> </bean> <!--数据源--> <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--配置SqlSessionFactoryBean--> //创建sqlSessionFactory <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--引用数据源--> <propertyname="dataSource" ref="datasource"></property> <!--引用mybatis配置文件中的配置--> <property name="configLocation" value="classpath:mybatis.xml"></property> <propertyname="mapperLocations"> <list> <value>classpath:mybatis_spring/dao/**/**.xml</value> </list> </property> </bean>

1.使用sqlSessionTemplate,需要接口的实现层

<!--配置1.sqlSessionTemplate 需要接口的实现类 创建sqlSeesion-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" ></constructor-arg>
</bean>
<!--配置dao-->
<bean id="billDao" class="mybatis_spring.dao.bill.BillDaoImpl">
//name与实现类的一致,要不然找不到getset方法
<property name="SqlSession" ref="sqlSessionTemplate"></property>
</bean>
<!--配置service-->
<bean id="billService" class="mybatis_spring.service.BillServiceImpl">
<property name="billDao" ref="billDao"></property>
</bean>

Userdao实现类

public class BillDaoImpl implements BillDao {
private SqlSessionTemplate SqlSession;
@Override
public int deleteBill(Integer id) {
//方法的路径,参数(没有可以不加)
return SqlSession.delete("mybatis_spring.dao.bill.BillDao.deleteBill",id);
}
public SqlSessionTemplate getSqlSession() {
return SqlSession;
}
public void setSqlSession(SqlSessionTemplate sqlSession) {
SqlSession = sqlSession;
}


Service层的实现类:
//@Autowired
private BillDao billDao=null;
@Override
public int deleteBill(Integer id) {
return billDao.deleteBill(id);
}
public BillDao getBillDao() {
return billDao;
}
public void setBillDao(BillDao billDao) {
this.billDao = billDao;
}

2.注入映射器 MapperFactoryBean(不使用接口实现类)

<!--不使用service实现类 只有接口即可 需要注解-->
<!--配置Dao-->
<bean id="billService" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="mybatis_spring.dao.bill.BillDao"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>

Service类

需要注解
@Service("billService")

public class BillServiceImpl implements BillService {
private BillDao billDao=null;(getset方法)
@Override
public int addBill(SmbmsBill bill) {
return billDao.addBill(bill);
}

3.使用MapperScannerConfigurer注入映射器和<context:conponent>标签

<!--使用service实现类 需要service层-->自动扫dao层和service层
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mybatis_spring.dao"></property>
</bean>
<context:component-scan base-package="mybatis_spring.service"></context:component-scan>
需要注解 @Autowired/@Resource(在serviceimpl添加)
@Autowired
private BillDao billDao=null;(getset方法)
自动创建映射器实现并注入给业务组件
原文地址:https://www.cnblogs.com/luoxionghenku/p/9634033.html