1.创建pojo类User
public class User {
private int id;
private String name;
private String pwd;
}
2.创建UserMapper接口
public interface UserMapper {
List<User> queryUser();
int insertUser(Map map);
void deleteUser(int id);
}
3.创建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="dao.UserMapper">
<select id="queryUser" resultType="pojo.User">
select * from user
</select>
<insert id="insertUser" parameterType="pojo.User">
insert into user(id, name, pwd) values(#{id},#{name}, #{pwd})
</insert>
<delete id="deleteUser" parameterType="int">
deletes from user where id = #{id}
</delete>
</mapper>
4.创建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="pojo"/>
</typeAliases>
</configuration>
5.创建spring-dao.xml文件,用于注册mybatis。
5.1.注册数据源,可以选择c3p0,druid等,这里选择spring自带的数据源
5.2.注册sqlsessionFactory。
"configLocation"属性:mybatis-config.xml的位置
“mapperLocations”:在mybatis-config.xml文件中需要映射的dao层中mapper.xml文件
5.3注册sqlsession.
这里的SqlSessionTemplate就是mybatis中的SqlSession。由于SqlSessionTemplate没有set方法只有构造方法,注册sqlSession只能使用构造器注入sqlSessionFactory
5.4创建UserMapperImpl实现类。由于spring-mybatis的SqlSessionTemplate没有set方法,需要手动创建方法
public class UserMapperImpl implements UserMapper{
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public List<User> queryUser() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
Map map = new HashMap();
map.put("id",9);
map.put("name","张二狗");
map.put("pwd","777777");
mapper.insertUser(map);
mapper.deleteUser(9);
return mapper.queryUser();
}
}
5.5注册userMapper。
userMapper底层由sqlSession.getMapper()得到,需要在userMapper中注入sqlSession属性。
5.6注册事务,利用aop织入实现事务管理。
<!--声明事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--结合aop实现事务的织入-->
<!--配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--给哪些方法配置事务,propagaion属性代表是否需要事务,默认为REQUIRED(需要)-->
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="txPoint" expression="execution(* dao.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config>
总的配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--使用spring数据源代替mybatis的数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3305/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="admin"/>
</bean>
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--绑定mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:dao/*.xml"/>
</bean>
<!--sqlSessionTemplate就是sqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能使用构造器注入sqlSessionFactory,因为它没有set方法只有构造方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<bean id="userMapper" class="dao.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
<!--声明事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--结合aop实现事务的织入-->
<!--配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--给哪些方法配置事务,propagaion属性代表是否需要事务,默认为REQUIRED(需要)-->
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="txPoint" expression="execution(* dao.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config>
</beans>
6.测试
public class MyTest {
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
UserMapper userMapper = context.getBean("userMapper",UserMapper.class);
for (User user : userMapper.queryUser()) {
System.out.println(user);
}
}
首先模拟失败事务:
首先注释掉spring-dao文件中的事务注册语句,修改userMaapper.xml文件中的删除语句,制造语法错误。
查看结果:
插入成功,删除失败。
删除表中新添加的那行,不再注释spring-dao文件中的事务注册语句,执行测试,删除失败但是没有插入表中,实现了事务的一致性。