layout: post
title: spring4
subtitle: Spring整合JDBC,Spring的事务
date: 2018-06-15
author: ZL
header-img: img/20180615.jpg
catalog: true
tags:
- spring
- Spring&JDBC
- spring&事务
Spring整合JDBC
简单使用(调用JdbcTemplate方法)
@Test
public void fun1() {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
try {
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql:///test01");
dataSource.setUser("root");
dataSource.setPassword("123456");
} catch (PropertyVetoException e) {
e.printStackTrace();
}
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
String sql = "insert into user values(null,'rose') ";
jdbcTemplate.update(sql);
}
在Spring中使用数据库需要JdbcTemplate对象,这个对象可以对数据库进行各种操作。想要获取JdbcTemplate对象,需要dataSource连接池作为参数。dataSource连接池还保存有关于数据库的数据,
结合applicationContext配置
与数据库匹配的bean类
public class User {
private Integer id;
private String name;
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 interface UserDao {
//增
void save(User u);
//删
void delete(Integer id);
//改
void update(User u);
//查
User getById(Integer id);
//查
int getTotalCount();
//查
List getAll();
}
操作数据库的接口类的实现类
在这个类里面增删改查,需要JdbcTemplate的对象才可以
public class UserDaoImpl implements UserDao {
private JdbcTemplate jt;
public JdbcTemplate getJt() {
return jt;
}
public void setJt(JdbcTemplate jt) {
this.jt = jt;
}
@Override
public void save(User u) {
String sql = "insert into user values(null,?) ";
jt.update(sql, u.getName());
}
@Override
public void delete(Integer id) {
String sql = "delete from user where id = ? ";
jt.update(sql,id);
}
@Override
public void update(User u) {
String sql = "update user set name = ? where id=? ";
jt.update(sql, u.getName(),u.getId());
}
@Override
public User getById(Integer id) {
String sql = "select * from user where id = ? ";
return jt.queryForObject(sql,new RowMapper(){
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
User u = new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
return u;
}}, id);
}
@Override
public int getTotalCount() {
String sql = "select count(*) from user ";
Integer count = jt.queryForObject(sql, Integer.class);
return count;
}
@Override
public List getAll() {
String sql = "select * from user ";
List list = jt.query(sql, new RowMapper(){
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
User u = new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
return u;
}});
return list;
}
}
applicationContext配置
调用
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {
@Resource(name="userDao")
private UserDao ud;
@Test
public void fun2() {
User u = new User();
u.setName("tom");
ud.save(u);
}
}
扩展1 JdbcDaoSupport
从JdbcDaoSupport可以获取到JdbcTemplate对象
上面的代码修改UserDaoImpl
public class UserDaoImpl extends JdbcDaoSupport implements UserDao {
@Override
public void save(User u) {
String sql = "insert into user values(null,?) ";
super.getJdbcTemplate().update(sql, u.getName());
}
@Override
public void delete(Integer id) {
String sql = "delete from user where id = ? ";
super.getJdbcTemplate().update(sql,id);
}
@Override
public void update(User u) {
String sql = "update user set name = ? where id=? ";
super.getJdbcTemplate().update(sql, u.getName(),u.getId());
}
@Override
public User getById(Integer id) {
String sql = "select * from user where id = ? ";
return super.getJdbcTemplate().queryForObject(sql,new RowMapper(){
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
User u = new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
return u;
}}, id);
}
@Override
public int getTotalCount() {
String sql = "select count(*) from user ";
Integer count = super.getJdbcTemplate().queryForObject(sql, Integer.class);
return count;
}
@Override
public List getAll() {
String sql = "select * from user ";
List list = super.getJdbcTemplate().query(sql, new RowMapper(){
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
User u = new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
return u;
}});
return list;
}
}
修改applicationContext
删除JDBCTemplate配置,userDao的参数改为dataSource
扩展2 properties配置文件
把数据库的配置信息放在外面
创建db.properties
//加一个前缀jdbc.可以避免冲突
jdbc.jdbcUrl=jdbc:mysql:///hibernate_32
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=1234
applicationContext使用
Spring中的aop事务
Spring封装了事务管理代码
编码式(不使用)
applicationContext配置
(部分代码)
事务调用
tt.execute(new transactionCallbackWithoutResult(){
protected void doInTransactionWithoutResult(TransactionStatus status){
//这里进行数据的操作
xxxDao.增删改查
//这里的代码都是在事务中执行的
}
});
xml配置实现事务
applicationContext配置
调用
@Override
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
public void transfer(final Integer from,final Integer to,final Double money) {
//减钱
ad.decreaseMoney(from, money);
int i = 1/0;
//加钱
ad.increaseMoney(to, money);
}
注解配置
applicationContext配置
调用
@Override
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
public void transfer(final Integer from,final Integer to,final Double money) {
//减钱
ad.decreaseMoney(from, money);
int i = 1/0;
//加钱
ad.increaseMoney(to, money);
}