一、JdbcTemplate
1、JdbcTemplate简介
JdbcTemplate时Spring对数据库操作的封装,能够方便的操作数据库;
2、Jdbc操作流程
①准备工作
第一步,导入相关依赖:
第二步,在数据库中创建相关表和对应的Java对象:
public class Person {
private Integer id;
private String name;
private Integer age;
}
第三步,创建配置文件创建数据库连接池对象并配置JdbcTemplate对象:
<!-- 链接外部文件 -->
<context:property-placeholder location="jdbc.properties"></context:property-placeholder>
<!--创建数据库连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${pro.driverClassName}"></property>
<property name="url" value="${pro.url}"></property>
<property name="username" value="${pro.username}"></property>
<property name="password" value="${pro.password}"></property>
</bean>
<!-- 配置JdbcTemplate对象 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
第四步,创建Dao接口和实现类:
@Repository
public class PersonDaoImpl implements PersonDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public int add(Person person) {
String sql = "insert into spring_jdbc(name,age) values(?,?)";
int update = jdbcTemplate.update(sql,person.getName(),person.getAge());
return update;
}
}
第五步,使用注解创建Dao接口实现类,并在配置文件中开启注解扫描;
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.lazy.jdbcTemplate1"></context:component-scan>
②使用JdbcTemplate
第一类,修改|删除|增加,都使用如下方法:
String sql = "对应的sql语句";
int update = jdbcTemplate.update(sql,Object[]() args);
第二类,查询特殊数值,使用如下方法:
String sql = "select count(*) from spring_jdbc";
Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
使用**jdbcTemplate.queryForObject(sql, Integer.class)**方法,第一参数为相应的sql语句,第二参数为返回结果对应的类的类型类;
第三类,查询某个对象:
String sql = "select * from spring_jdbc where id = ?";
Person person = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Person>(Person.class), id);
使用jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper(Person.class), id)方法,第一参数为查询是sql语句,第二参数为RowMapper接口的实现类BeanPropertyRowMapper(Person.class),其中所填写的泛型为返回值的类,构造器参数为返回值类的类型类,最后参数为sql语句中的参数值;
第四类,查询对象集合
String sql = "select * from spring_jdbc";
List<Person> personList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Person>(Person.class));
使用jdbcTemplate.query(sql, new BeanPropertyRowMapper(Person.class))方法,第一参数为查询语句,第二参数为RowMapper接口的实现类BeanPropertyRowMapper(Person.class),其中所填写的泛型为返回值的类,构造器参数为返回值类的类型类;
第五类,批量操作数据:
public int[] batchAdd(List<Object[]> args) {
String sql = "相应的sql语句";
int[] ints = jdbcTemplate.batchUpdate(sql, args);
return ints;
}
使用**jdbcTemplate.batchUpdate(sql, args)**方法,第一参数为相应的sql语句,第二参数为在sql语句中的参数值(以List<Object[]>形式)
3、补充:使用完全注解
@Configuration
@ComponentScan(basePackages = "com.lazy.tx")
//开启事务管理
@EnableTransactionManagement
public class TxConfig {
//创建数据库连接池
@Bean(name = "dataSource")
public DataSource getDataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName("");
druidDataSource.setUrl(");
druidDataSource.setUsername("");
druidDataSource.setPassword("");
return druidDataSource;
}
//配置JdbcTemplate对象,根据类型自动装配
@Bean(name = "jdbcT")
public JdbcTemplate getJdbcTemplate(DataSource dataSource){
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}
二、Spring中的事务管理
1、Spring事务管理简介
在实现业务时,事务是最小的单元,即在一个业务中的数据库操作要么同时成功,要么同时失败,而Spring中提供类对事务的管理;
2、Spring中事务管理操作流程(使用注解)
①搭建事务场景
此处模拟简单的转账事务,准备好数据库表,对应的JavaBean,Dao,Service;
②操作流程
第一步,在配置类中配置事务管理器
//创建事务管理器
@Bean
public DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource){
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSource);
return dataSourceTransactionManager;
}
第二步,在配置类中开启事务注解
@Configuration
@ComponentScan(basePackages = "com.lazy.tx")
//开启事务管理
@EnableTransactionManagement
public class TxConfig {
第三步,在Service类上或Service方法中添加@Transactional注解
@Service
@Transactional
public class UserService {
在类上添加注解表示类中的所有方法都添加事务,在方法上添加表示只有该方法有事务;
③声明事务中的各种参数
(1)propagation:事务传播行为
多事务方法直接进行调用,这个过程中事务 是如何进行管理的
(2)ioslation:事务隔离级别
(3)timeout:超时时间
事务需要在一定时间内进行提交,如果不提交进行回滚
默认值是 -1 ,设置时间以秒单位进行计算
(4)readOnly:是否只读
读:查询操作,写:添加修改删除操作
readOnly 默认值 false,表示可以查询,可以添加修改删除操作
设置 readOnly 值是 true,设置成 true 之后,只能查询
(5)rollbackFor:回滚
设置出现哪些异常进行事务回滚
(6)noRollbackFor:不回滚
行回滚
默认值是 -1 ,设置时间以秒单位进行计算
(4)readOnly:是否只读
读:查询操作,写:添加修改删除操作
readOnly 默认值 false,表示可以查询,可以添加修改删除操作
设置 readOnly 值是 true,设置成 true 之后,只能查询
(5)rollbackFor:回滚
设置出现哪些异常进行事务回滚
(6)noRollbackFor:不回滚
设置出现哪些异常不进行事务回滚