1、准备jar包

A、第一种方式:配置SqlSessionFactoryBean+配置SqlSessionTemplate

a、项目结构

b、applicationContext.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:p="http://www.springframework.org/schema/p"
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/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"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<!-- <property name="url">
<value><![CDATA[jdbc:mysql://127.0.0.1:3306/smbms?
useUnicode=true&characterEncoding=utf-8]]></value>
</property> -->
<property name="url" value="jdbc:mysql://127.0.0.1:3306/smbms?
useUnicode=true&amp;characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean> <!-- 配置SqlSessionFactoryBean --><!-- 为整合应用提供SqlSession对象资源 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 引用数据源组件 -->
<property name="dataSource" ref="dataSource" />
<!-- 引用MyBatis配置文件中的配置 -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!-- 配置SQL映射文件信息 -->
<property name="mapperLocations">
<list>
<value>classpath:cn/smbms/dao/*/*.xml</value><!-- 映射文件 之前在mybatis配置文件中-->
</list>
</property>
</bean> <!-- 配置SqlSessionTemplate --><!-- 负责管理MyBatis的SqlSession,调用SQL映射语句,实现对数据库的访问-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" /><!-- 整合好的对象资源赋值给SqlSessionTemplate -->
</bean>
<!-- 配置DAO -->
<bean id="userMapper" class="cn.smbms.dao.user.UserMapperImpl"><!-- Dao层声明的对象是SqlSessionTemplate来获得SqlSession -->
<property name="sqlSession" ref="sqlSessionTemplate" /><!-- 把管理的SqlSession赋值给Dao层set方法 -->
</bean>
<!-- 配置业务Bean -->
<bean id="userService" class="cn.smbms.service.user.UserServiceImpl">
<property name="userMapper" ref="userMapper" /><!-- 把Dao层对象赋值给Server层set方法 -->
</bean>
</beans>

c、mybatis-config.xml

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--类型别名 -->
<typeAliases>
<package name="cn.smbms.pojo" />
</typeAliases>
</configuration>

d、log4j

 log4j.rootLogger=DEBUG,CONSOLE,file
#log4j.rootLogger=ERROR,ROLLING_FILE
log4j.logger.cn.smbms.dao=debug
log4j.logger.com.ibatis=debug
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug
log4j.logger.java.sql.Connection=debug
log4j.logger.java.sql.Statement=debug
log4j.logger.java.sql.PreparedStatement=debug
log4j.logger.java.sql.ResultSet=debug
log4j.logger.org.tuckey.web.filters.urlrewrite.UrlRewriteFilter=debug ######################################################################################
# Console Appender \u65e5\u5fd7\u5728\u63a7\u5236\u8f93\u51fa\u914d\u7f6e
######################################################################################
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=error
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern= [%p] %d %c - %m%n ######################################################################################
# DailyRolling File \u6bcf\u5929\u4ea7\u751f\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u683c\u5f0f:log2009-09-11
######################################################################################
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern=yyyy-MM-dd
log4j.appender.file.File=log.log
log4j.appender.file.Append=true
log4j.appender.file.Threshold=error
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n log4j.logger.com.opensymphony.xwork2=error

e、UserMapper.java

 package cn.smbms.dao.user;

 import java.util.List;
import cn.smbms.pojo.User; public interface UserMapper {
/**
* 查询用户列表(参数:对象入参)
* @return
*/
public List<User> getUserList(User user);
}

f、UserMapperImpl.java

 package cn.smbms.dao.user;

 import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate; import cn.smbms.pojo.User; public class UserMapperImpl implements UserMapper {
private SqlSessionTemplate sqlSession; @Override
public List<User> getUserList(User user) {
return sqlSession.selectList(
"cn.smbms.dao.user.UserMapper.getUserList", user);
} public SqlSessionTemplate getSqlSession() {
return sqlSession;
} public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
} }

g、UserMapper.xml

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.smbms.dao.user.UserMapper">
<!-- 当数据库中的字段信息与对象的属性不一致时需要通过resultMap来映射 -->
<resultMap type="User" id="userList">
<!-- <result property="id" column="id" />
<result property="userCode" column="userCode" />
<result property="userName" column="userName" />
<result property="phone" column="phone" />
<result property="birthday" column="birthday" />
<result property="gender" column="gender" />
<result property="userRole" column="userRole" /> -->
<result property="userRoleName" column="roleName" />
</resultMap>
<!-- 查询用户列表(参数:对象入参) -->
<select id="getUserList" resultMap="userList" parameterType="User">
select u.*,r.roleName from smbms_user u,smbms_role r
where u.userName like CONCAT ('%',#{userName},'%')
and u.userRole = #{userRole} and u.userRole = r.id
</select>
</mapper>

h、User.java

 package cn.smbms.pojo;

 import java.util.Date;

 public class User {
private Integer id; // id
private String userCode; // 用户编码
private String userName; // 用户名称
private String userPassword; // 用户密码
private Integer gender; // 性别
private Date birthday; // 出生日期
private String phone; // 电话
private String address; // 地址
private Integer userRole; // 用户角色ID
private Integer createdBy; // 创建者
private Date creationDate; // 创建时间
private Integer modifyBy; // 更新者
private Date modifyDate; // 更新时间
private String userRoleName; // 用户角色名称 public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUserCode() {
return userCode;
} public void setUserCode(String userCode) {
this.userCode = userCode;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getUserPassword() {
return userPassword;
} public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
} public Integer getGender() {
return gender;
} public void setGender(Integer gender) {
this.gender = gender;
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} public String getPhone() {
return phone;
} public void setPhone(String phone) {
this.phone = phone;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public Integer getUserRole() {
return userRole;
} public void setUserRole(Integer userRole) {
this.userRole = userRole;
} public Integer getCreatedBy() {
return createdBy;
} public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
} public Date getCreationDate() {
return creationDate;
} public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
} public Integer getModifyBy() {
return modifyBy;
} public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
} public Date getModifyDate() {
return modifyDate;
} public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
} public String getUserRoleName() {
return userRoleName;
} public void setUserRoleName(String userRoleName) {
this.userRoleName = userRoleName;
}
}

i、UserService.java

 package cn.smbms.service.user;

 import java.util.List;
import cn.smbms.pojo.User; public interface UserService {
public List<User> findUsersWithConditions(User user);
}

J、UserServiceImpl.java

 package cn.smbms.service.user;

 import java.util.List;

 import cn.smbms.dao.user.UserMapper;
import cn.smbms.pojo.User; public class UserServiceImpl implements UserService {
private UserMapper userMapper; @Override
public List<User> findUsersWithConditions(User user) {
try {
return userMapper.getUserList(user);
} catch (RuntimeException e) {
e.printStackTrace();
throw e;
}
} public UserMapper getUserMapper() {
return userMapper;
} public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
} }

k、test

 package cn.smbms.test.user;

 import java.util.ArrayList;
import java.util.List; import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.smbms.pojo.User;
import cn.smbms.service.user.UserService; public class UserTest { private Logger logger = Logger.getLogger(UserTest.class); @Before
public void setUp() throws Exception {
} @Test
public void testGetUserList() {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
UserService userService = (UserService) ctx.getBean("userService");
List<User> userList = new ArrayList<User>();
User userCondition = new User();
userCondition.setUserName("赵");
userCondition.setUserRole(3);
userList = userService.findUsersWithConditions(userCondition); for (User userResult : userList) {
// logger.debug("testGetUserList userCode: "
// + userResult.getUserCode() + " and userName: "
// + userResult.getUserName() + " and userRole: "
// + userResult.getUserRole() + " and userRoleName: "
// + userResult.getUserRoleName() + " and address: "
// + userResult.getAddress());
System.out.println(userResult.getUserName());
}
} }

B、第一种方式:配置SqlSessionFactoryBean+采用数据映射器(MapperFactoryBean)

a、项目结构(dao层少了impl)

b、applicationContext.xml(主要变化在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:p="http://www.springframework.org/schema/p"
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/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"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://127.0.0.1:3306/smbms?
useUnicode=true&amp;characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean> <!-- 配置SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 引用数据源组件 -->
<property name="dataSource" ref="dataSource" />
<!-- 引用MyBatis配置文件中的配置 -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!-- 配置SQL映射文件信息 -->
<!-- <property name="mapperLocations">
<list>
<value>classpath:cn/smbms/dao/**/*.xml</value>
</list>
</property> -->
</bean>
<!-- 配置DAO --><!-- 采用数据映射器(MapperFactoryBean)的方式完成对数据库操作-->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!-- 根据Mapper接口获取Mapper对象,它封装了原有的SqlSession.getMapper()功能的实现-->
<property name="mapperInterface" value="cn.smbms.dao.user.UserMapper" /><!-- 指定映射器,只能是接口类型 -->
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean> <!-- 注入SqlSessionFactory以提供SqlSessionTemplate实例 --> <!-- 配置业务Bean -->
<bean id="userService" class="cn.smbms.service.user.UserServiceImpl">
<property name="userMapper" ref="userMapper" />
</bean>
</beans>

c、UserServiceImpl.java(发生变化)

 package cn.smbms.service.user;

 import java.util.List;
import cn.smbms.dao.user.UserMapper;
import cn.smbms.pojo.User; public class UserServiceImpl implements UserService {
private UserMapper userMapper; @Override
public List<User> findUsersWithConditions(User user) {
try {
return userMapper.getUserList(user);
} catch (RuntimeException e) {
e.printStackTrace();
throw e;
}
} public UserMapper getUserMapper() {
return userMapper;
} public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
} }

C、第三种方式:配置SqlSessionFactoryBean+MapperScannerConfigurer(递归扫描基准包下所有接口,若它们在SQL映射文件中 定义过,则动态注册为MapperFactoryBean,如此即可批量产生映射器实现类)+注解

a、applicationContext.xml(主要变化在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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<!-- <property name="url">
<value><![CDATA[jdbc:mysql://127.0.0.1:3306/smbms?
useUnicode=true&characterEncoding=utf-8]]></value>
</property> -->
<property name="url" value="jdbc:mysql://127.0.0.1:3306/smbms?
useUnicode=true&amp;characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean> <!-- 配置SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 引用数据源组件 -->
<property name="dataSource" ref="dataSource" />
<!-- 引用MyBatis配置文件中的配置 -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean> <!-- MapperScannerConfigurer递归扫描基准包下所有接口,若它们在SQL映射文件中 定义过,
则动态注册为MapperFactoryBean,如此即可批量产生映射器实现类-->
<!-- 配置DAO -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> -->
<property name="basePackage" value="cn.smbms.dao" /><!-- 指定扫描的基准包 -->
</bean>
<!-- 配置业务Bean -->
<!-- <bean id="userService" class="cn.smbms.service.user.UserServiceImpl">
<property name="userMapper" ref="userMapper" />
</bean> -->
<context:component-scan base-package="cn.smbms.service" />
</beans>

b、UserServiceImpl.java(发生变化)

 package cn.smbms.service.user;

 import java.util.List;

 import javax.annotation.Resource;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import cn.smbms.dao.user.UserMapper;
import cn.smbms.pojo.User; @Service("userService")
public class UserServiceImpl implements UserService {
@Autowired // @Resource
private UserMapper userMapper; @Override
public List<User> findUsersWithConditions(User user) {
try {
return userMapper.getUserList(user);
} catch (RuntimeException e) {
e.printStackTrace();
throw e;
}
} // public UserMapper getUserMapper() {
// return userMapper;
// }
//
// public void setUserMapper(UserMapper userMapper) {
// this.userMapper = userMapper;
// } }

D、业务层添加声明式事务

a.applicationContext.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:p="http://www.springframework.org/schema/p"
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/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<!-- <property name="url">
<value><![CDATA[jdbc:mysql://127.0.0.1:3306/smbms?
useUnicode=true&characterEncoding=utf-8]]></value>
</property> -->
<property name="url" value="jdbc:mysql://127.0.0.1:3306/smbms?
useUnicode=true&amp;characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean> <!-- 配置SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 引用数据源组件 -->
<property name="dataSource" ref="dataSource" />
<!-- 引用MyBatis配置文件中的配置 -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<!-- 配置DAO -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> -->
<property name="basePackage" value="cn.smbms.dao" />
</bean>
<!-- 配置业务Bean -->
<!-- <bean id="userService" class="cn.smbms.service.user.UserServiceImpl">
<property name="userMapper" ref="userMapper" />
</bean> -->
<context:component-scan base-package="cn.smbms.service" /> <!-- 定义事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="search*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice> <!-- 定义切面 -->
<aop:config>
<aop:pointcut id="serviceMethod"
expression="execution(* cn.smbms.service..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
</aop:config>
</beans>

E、注解实现声明式事务处理

a、UserServiceImpl.java

 package cn.smbms.service.user;

 import java.util.List;

 import javax.annotation.Resource;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import cn.smbms.dao.user.UserMapper;
import cn.smbms.pojo.User; @Transactional
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired // @Resource
private UserMapper userMapper; @Override
@Transactional(propagation = Propagation.SUPPORTS)
public List<User> findUsersWithConditions(User user) {
try {
return userMapper.getUserList(user);
} catch (RuntimeException e) {
e.printStackTrace();
throw e;
}
} @Override
@Transactional(propagation=Propagation.REQUIRED)
public boolean addNewUser(User user) {
boolean result = false;
try {
if (userMapper.add(user) == 1)
result = true;
// int i = 1 / 0;
} catch (RuntimeException e) {//抛出运行时异常会事务回滚
e.printStackTrace();
throw e;
}
return result;
} // public UserMapper getUserMapper() {
// return userMapper;
// }
//
// public void setUserMapper(UserMapper userMapper) {
// this.userMapper = userMapper;
// } }

b、applicationContext.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:p="http://www.springframework.org/schema/p"
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/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<!-- <property name="url">
<value><![CDATA[jdbc:mysql://127.0.0.1:3306/smbms?
useUnicode=true&characterEncoding=utf-8]]></value>
</property> -->
<property name="url" value="jdbc:mysql://127.0.0.1:3306/smbms?
useUnicode=true&amp;characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean> <!-- 配置SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 引用数据源组件 -->
<property name="dataSource" ref="dataSource" />
<!-- 引用MyBatis配置文件中的配置 -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<!-- 配置DAO -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> -->
<property name="basePackage" value="cn.smbms.dao" />
</bean>
<!-- 配置业务Bean -->
<!-- <bean id="userService" class="cn.smbms.service.user.UserServiceImpl">
<property name="userMapper" ref="userMapper" />
</bean> -->
<context:component-scan base-package="cn.smbms.service" />
<!-- 定义事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/><!-- 添加对注解配置的事务的支持 -->
<!-- <tx:advice id="txAdvice">
<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 id="serviceMethod"
expression="execution(* cn.smbms.service..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
</aop:config> -->
</beans>

常用两个事务:

REQUIRED(默认值增删改):在有transaction状态下执行;如当前没有transaction,则创建新的transaction;
SUPPORTS(查询):如当前有transaction,则在transaction状态下执行;如果当前没有transaction,在无transaction状态下执行;

注意:

Spring默认只在抛出runtime exception时才标识事务回滚

欢迎进群学习交流(927465926) Full Stack engineer

Spring整合MyBaytis的更多相关文章

  1. 使用Spring整合Quartz轻松完成定时任务

    一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...

  2. 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】

    一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...

  3. spring整合hibernate的详细步骤

    Spring整合hibernate需要整合些什么? 由IOC容器来生成hibernate的sessionFactory. 让hibernate使用spring的声明式事务 整合步骤: 加入hibern ...

  4. Spring整合Ehcache管理缓存

    前言 Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存. Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它 ...

  5. spring整合hibernate

    spring整合hibernate包括三部分:hibernate的配置.hibernate核心对象交给spring管理.事务由AOP控制 好处: 由java代码进行配置,摆脱硬编码,连接数据库等信息更 ...

  6. MyBatis学习(四)MyBatis和Spring整合

    MyBatis和Spring整合 思路 1.让spring管理SqlSessionFactory 2.让spring管理mapper对象和dao. 使用spring和mybatis整合开发mapper ...

  7. Mybatis与Spring整合,使用了maven管理项目,作为初学者觉得不错,转载下来

    转载自:http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype ...

  8. Spring整合HBase

    Spring整合HBase Spring HBase SHDP § 系统环境 § 配置HBase运行环境 § 配置Hadoop § 配置HBase § 启动Hadoop和HBase § 创建Maven ...

  9. Spring整合Ehcache管理缓存(转)

    目录 前言 概述 安装 Ehcache的使用 HelloWorld范例 Ehcache基本操作 创建CacheManager 添加缓存 删除缓存 实现基本缓存操作 缓存配置 xml方式 API方式 S ...

随机推荐

  1. Atlas安装配置

    准备环境 192.168.1.1(Altas) 192.168.1.2(MySQL主) 192.168.1.3(MySQL从) 官方链接:https://github.com/Qihoo360/Atl ...

  2. 变量对象VO与活动对象AO

    变量对象VO 变量对象VO是与执行上下文相关的特殊对象,用来存储上下文的函数声明,函数形参和变量.在global全局上下文中,变量对象也是全局对象自身,在函数上下文中,变量对象被表示为活动对象AO. ...

  3. 设计模式 --> (2)单例模式

    单例模式 单例模式也称为单件模式.单子模式,可能是使用最广泛的设计模式.其意图是保证一个类仅有一个实例,并提供一个访问它的全局访问点,该实例被所有程序模块共享.如系统的日志输出,GUI应用必须是单鼠标 ...

  4. (转)[置顶] Android APK反编译就这么简单 详解(附图) .

    在学习Android开发的过程你,你往往会去借鉴别人的应用是怎么开发的,那些漂亮的动画和精致的布局可能会让你爱不释手,作为一个开发者,你可能会很想知道这些效果界面是怎么去实现的,这时,你便可以对改应用 ...

  5. selenium2自动化测试学习笔记(二)

    chromedriver报错问题解决了,真是无语 是因为chromedriver与浏览器版本不一致 http://chromedriver.storage.googleapis.com/index.h ...

  6. 爬虫实践---悦音台mv排行榜与简单反爬虫技术应用

    由于要抓取的是悦音台mv的排行榜,这个排行榜是实时更新的,如果要求不停地抓取,这将有可能导致悦音台官方采用反爬虫的技术将ip给封掉.所以这里要应用一些反爬虫相关知识. 目标网址:http://vcha ...

  7. Node.js + gulp 合并静态页模版,文件更新自动热重载。浏览器可预览

    github地址:https://github.com/Liaozhenting/template 使用的是ejs的语法.其实你用什么文件后缀都可以,都是按ejs来解析. 模板文件放在componen ...

  8. Mybatis学习笔记二

    本篇内容,紧接上一篇内容Mybatis学习笔记一 输入映射和输出映射 传递简单类型和pojo类型上篇已介绍过,下面介绍一下包装类型. 传递pojo包装对象 开发中通过可以使用pojo传递查询条件.查询 ...

  9. 02_LInux的目录结构_我的Linux之路

    前两节已经教大家怎么在虚拟机安装Linux系统 这一节我们来学习Linux的目录结构,讲一下linux的整个系统架构,提前熟悉一下Linux 在Linux或Unix系统中有一个非常重要的概念,就是一切 ...

  10. Leetcode 4——Partition List

    Problems: Given a linked list and a value x, partition it such that all nodes less than x come befor ...