1 使用到的类
1.1 数据操作
JdbcTemplate
:jdbc模板,传入dataSource作为参数,可以直接通过update或者query方法执行sql语句(包括有占位符的预通道语句)JdbcDaoSupport
:Jdbc对数据操作的支持。我们通常自定义一个数据操作类继承自此类。通过它的getJdbcTemplate()
方法可以拿到它的jdbcTemplate对象。使用前需要通过ioc的方式对JdbcTemplate实例化。RowMapper
:一个用于对查询结果数据行和实体类之间映射的接口,需要在jdbcTemplate的query相关方法中使用。
1.2 事务管理
- PlatformTransactionManager:事务管理的接口。我们通常用
DataSourceTransactionManager
来进行jdbc或mybatis数据库操作的事务管理。
2 范例
2.1 数据操作
spring配置文件:
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/mydb8?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
<property name="initialSize" value="5"></property>
<property name="maxActive" value="10"></property>
<property name="minIdle" value="5"></property>
<property name="maxWait" value="3000"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="teamDao" class="com.kkb.xzk.dao.TeamDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="team" class="com.kkb.xzk.bean.Team">
<property name="name" value="湖人"></property>
<property name="location" value="洛杉矶"></property>
</bean>
<bean id="team2" class="com.kkb.xzk.bean.Team">
<property name="name" value="热火"></property>
<property name="location" value="迈阿密"></property>
</bean>
</beans>
自定义的数据操作类:
package com.kkb.xzk.dao;
import com.kkb.xzk.bean.Team;
import com.kkb.xzk.dao.rowmapper.TeamRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import java.util.List;
import java.util.Map;
public class TeamDao extends JdbcDaoSupport {
public int insert(Team team){
String sql = "insert into team(tname, location) values(?,?)";
return this.getJdbcTemplate().update(sql, team.getName(), team.getLocation());
}
public Team findById(int id){
String sql = "select * from team where `id`=?";
return this.getJdbcTemplate().queryForObject(sql, new TeamRowMapper(),new Object[]{id});
}
public List<Team> findAll(){
String sql = "select * from team";
return this.getJdbcTemplate().query(sql, new TeamRowMapper());
}
public int count(){
String sql = "select count(id) from team";
return this.getJdbcTemplate().queryForObject(sql, Integer.class);
}
public Map<String, Object> maxAndMin(){
String sql = "select max(id), min(id) from team";
return this.getJdbcTemplate().queryForMap(sql);
}
}
自定义的映射器RowMapper:
package com.kkb.xzk.dao.rowmapper;
import com.kkb.xzk.bean.Team;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TeamRowMapper implements RowMapper<Team> {
@Override
public Team mapRow(ResultSet resultSet, int i) throws SQLException {
Team team = new Team();
team.setId(resultSet.getInt("id"));
team.setName(resultSet.getString("tname"));
team.setLocation(resultSet.getString("location"));
return team;
}
}
2.2 事务管理
2.2.1 基于注解的事务管理
- 通过注解
@Transactional(propagation=xxx, rollbackFor=xxx)
对一个方法加上事务管理。
package com.kkb.xzk.service;
import com.kkb.xzk.bean.Team;
import com.kkb.xzk.dao.TeamDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service
public class TeamService {
@Autowired
private TeamDao teamDao;
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
public int insert(Team team){
int num1 = teamDao.insert(team);
//int i = 10/0;
int num2 = teamDao.insert(team);
return num1 + num2;
}
}
- 在配置文件中需要加上事务管理的命名空间(xmlns:tx)、规范和启动事务注解的语句
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
">
...
<!--事务管理相关-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
2.2.2 基于xml的事务管理
基于xml的事务管理的好处是,假如我们的service里有很多的方法都需要加事务,那么不需要通过注解挨个添加,直接在配置文件里可以实现批量添加(通过切入点)
- 首先需要在pom.xml中添加aspectj的依赖:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
- 其次,在配置文件中声明通知(<tx:advice>)
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="pt" expression="execution(* com.kkb.xzk.service..*.insert*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"></aop:advisor>
</aop:config>