Spring和Mybatis整合

Application.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<!-- 配置数据源 -->
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  <property name="url" value="jdbc:mysql://localhost:3306/smbms"></property>
  <property name="username" value="root"></property>
  <property name="password" value="root"></property>
</bean>

<!-- 配置sqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="datasource"></property>
  <property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- 配置SqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
  <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>

<!-- 配置DAo -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="cn.yct.dao"></property>
</bean>
<!-- 扫描service层 -->
<context:component-scan base-package="cn.yct.service"></context:component-scan>

<!-- 定义事物管理 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="datasource"></property>
</bean>

<!-- 通过<tx:Advice>标签为指定的事物管理器设置属性 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<!-- 定义属性,声明事物规定 -->
<tx:attributes>
  <tx:method name="find*" propagation="SUPPORTS"/>
  <tx:method name="add*" propagation="REQUIRED"/>
  <tx:method name="del*" propagation="REQUIRED"/>
  <tx:method name="update*" propagation="REQUIRED"/>
  <tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>

<!-- 定义切面 -->
<aop:config>
  <!-- 定义切点 -->
  <aop:pointcut expression="execution(* cn.yct.service..*.*(..))" id="servicerole"/>
  <!-- 将事物增强与切入点组合 -->
  <aop:advisor advice-ref="txAdvice" pointcut-ref="servicerole"/>
</aop:config>

<bean/>

在Service实现层   如:UserServiceImpl

@Service("billservice") 
public class BillServiceImpl implements BillService {

@Autowired
private BillDaoMapper billDaoMapper;             映射接口
public BillDaoMapper getBillDaoMapper() {
  return billDaoMapper;
}
public void setBillDaoMapper(BillDaoMapper billDaoMapper) {
  this.billDaoMapper = billDaoMapper;
}
@Override
public List<Smbms_Bill> getlist(Map map) {
  return billDaoMapper.getlist(map);
}
}

原文地址:https://www.cnblogs.com/y-c-t/p/8563293.html