使用原生的JDBC API进行事务管理
编程式事务管理需要将事务管理代码嵌入到业务方法中来控制事务的提交和回滚
缺点
DataSourceTransactionManager:在应用程序中只需要处理一个数据源,而且通过JDBC存取
导入相关jar包
<dependencies>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.2version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.5version>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.9.4version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.1.10.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.1.10.RELEASEversion>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
dependencies>
pojo实体类
public class User {
private Integer id;
private String name;
private String password;
private String email;
private Date birthday;
public User() {
}
public User(Integer id, String name, String password, String email, Date birthday) {
this.id = id;
this.name = name;
this.password = password;
this.email = email;
this.birthday = birthday;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
", birthday=" + birthday +
'}';
}
}
userMapper.xml配置
<mapper namespace="com.song.mapper.UserMapper">
<select id="selectUser" resultType="User">
select * from users
select>
mapper>
mybatis-config.xml配置
<configuration>
<typeAliases>
<package name="com.song.pojo"/>
typeAliases>
configuration>
Spring中spring-mybatis的配置
<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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/jdbc?useSSL=false&useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="824008"/>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/song/mapper/UserMapper.xml"/>
bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
bean>
<bean id="userMapperImpl" class="com.song.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
bean>
beans>
私有化sqlSessionTemplate
public class UserMapperImpl implements UserMapper {
//sqlSession不用我们自己创建了,Spring来管理
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
public List<User> selectUser() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.selectUser();
}
}
测试
@Test
public void test01(){
ApplicationContext context = new ClassPathXmlApplicationContext("mybatis-spring.xml");
UserMapper bean = (UserMapper) context.getBean("userMapperImpl");
List<User> userList = bean.selectUser();
for (User user : userList) {
System.out.println(user);
}
}
要和 Spring 一起使用 MyBatis,需要在 Spring 应用上下文中定义至少两样东西:一个 SqlSessionFactory 和至少一个数据映射器类。
在 MyBatis-Spring 中,可使用 SqlSessionFactoryBean来创建 SqlSessionFactory
可以使用SqlSessionDaoSupport来实现Spring mybatis的整合
代码环境还是上面的环境
增加一个public void deleteUser(@Param(“id”) int id);方法
public class UserDaoImpl extends SqlSessionDaoSupport implements UserMapper {
public List<User> selectUser() {
Map<String, Object> map = new HashMap<String, Object>();
//出生日期
Date parse = null;
try {
parse = new SimpleDateFormat("yyyy-MM-dd").parse("2020-7-1");
} catch (ParseException e) {
e.printStackTrace();
}
map.put("id",4);
map.put("name","小张");
map.put("password","123456");
map.put("email","[email protected]");
map.put("birthday",parse);
UserMapper mapper = getSqlSession().getMapper(UserMapper.class);
mapper.addUser(map);
mapper.deleteUser(2);
return mapper.selectUser();
}
public int addUser(Map<String, Object> map) {
UserMapper mapper = getSqlSession().getMapper(UserMapper.class);
return mapper.addUser(map);
}
//删除
public int deleteUser(int id) {
UserMapper mapper = getSqlSession().getMapper(UserMapper.class);
return mapper.deleteUser(id);
}
}
xml配置(删除方法故意写错)
<delete id="deleteUser" parameterType="int">
deletes from users where id = #{id}
delete>
<insert id="addUser">
insert into users (id,name,password,email,birthday) values (#{id},#{name},#{password},#{email},#{birthday})
insert>
Spring中事务开启
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add" propagation="REQUIRED"/>
<tx:method name="delete" propagation="REQUIRED"/>
<tx:method name="update" propagation="REQUIRED"/>
<tx:method name="search*" propagation="REQUIRED"/>
<tx:method name="get" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
tx:attributes>
tx:advice>
<aop:config>
<aop:pointcut id="txPointcut" expression="execution(* com.song.mapper.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
aop:config>
测试
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("mybatis-spring.xml");
UserMapper mapper = (UserMapper) context.getBean("userMapper");
List<User> user = mapper.selectUser();
for (User user1 : user) {
System.out.println(user1);
}
}
若想了解更多的Spring建议查看 Spring官网
Spring下载地址
Mybatis-Spring官方文档学习
推荐学习Spring的视频 B站 遇见狂神说 或者 尚硅谷
谢谢大家的阅读! 若上面有写错的 欢迎纠正哦!