1. spring容器整合jdbc,实现数据库操作
- 创建User类
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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
- 创建UserDao接口
public interface UserDao {
// 增
public void save(User user) throws PropertyVetoException;
// 删
public void delete(Integer id);
// 改
public void update(User user);
// 查
public User getUserById(Integer id);
// 查
public int getTotalCount();
// 查
public List<User> getAllUser();
}
- 创建userDao的实现类,实现接口
public class UserDaoImpl implements UserDao{
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public void save(User user) {
String sql = "insert into t_user values(null, ?)";
jdbcTemplate.update(sql, user.getName());
}
@Override
public void delete(Integer id) {
String sql = "delete from t_user where id = ? ";
jdbcTemplate.update(sql, id);
}
@Override
public void update(User user) {
String sql = "update t_user set name = ? where id = ? ";
jdbcTemplate.update(sql, user.getName(), user.getId());
}
@Override
public User getUserById(Integer id) {
String sql = "select * from t_user where id = ? ";
return jdbcTemplate.queryForObject(sql, new RowMapper<User>() {
@Override
public User mapRow(ResultSet resultSet, int i) throws SQLException {
User user = new User();
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
return user;
}
}, id);
}
@Override
public int getTotalCount() {
String sql = "select count(*) from t_user";
int count = jdbcTemplate.queryForObject(sql, Integer.class);
return count;
}
@Override
public List<User> getAllUser() {
String sql = "select * from t_user";
List<User> list = jdbcTemplate.query(sql, new RowMapper<User>() {
@Override
public User mapRow(ResultSet resultSet, int i) throws SQLException {
User user = new User();
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
return user;
}
});
return list;
}
- 将jdbc连接池、jdbcDataSource、userDao的实现类,配置到spring
<!--将连接池放入spring 容器-->
<bean name="jdbcDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql://xxx.206.7.239/xiaobang"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="123456789"></property>
</bean>
<!--将jdbcDataSource 放入spring容器-->
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="jdbcDataSource"></property>
</bean>
<!--将userDao的实现类放入spring容器-->
<bean name="userDao" class="daoImpl.UserDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
2. 继承JdbcDaoSupport整合jdbc,实现数据库操作
准备阶段还是一样的,只写不一样的地方
1.dao的实现类中,直接继承JdbcDaoSupport ,不再需要我们自己创建JdbcTemplate
public class UserDaoImpl extends JdbcDaoSupport implements UserDao{
@Override
public void save(User user) {
String sql = "insert into t_user values(null, ?)";
super.getJdbcTemplate().update(sql, user.getName());
}
@Override
public void delete(Integer id) {
String sql = "delete from t_user where id = ? ";
super.getJdbcTemplate().update(sql, id);
}
@Override
public void update(User user) {
String sql = "update t_user set name = ? where id = ? ";
super.getJdbcTemplate().update(sql, user.getName(), user.getId());
}
@Override
public User getUserById(Integer id) {
String sql = "select * from t_user where id = ? ";
return super.getJdbcTemplate().queryForObject(sql, new RowMapper<User>() {
@Override
public User mapRow(ResultSet resultSet, int i) throws SQLException {
User user = new User();
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
return user;
}
}, id);
}
@Override
public int getTotalCount() {
String sql = "select count(*) from t_user";
int count = super.getJdbcTemplate().queryForObject(sql, Integer.class);
return count;
}
@Override
public List<User> getAllUser() {
String sql = "select * from t_user";
List<User> list = super.getJdbcTemplate().query(sql, new RowMapper<User>() {
@Override
public User mapRow(ResultSet resultSet, int i) throws SQLException {
User user = new User();
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
return user;
}
});
return list;
}
- 配置文件
<!--将连接池放入spring 容器-->
<bean name="jdbcDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql://xxx.206.7.239/xiaobang"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="123456789"></property>
</bean>
<!--将userDao的实现类放入spring容器-->
<bean name="userDao" class="daoImpl.UserDaoImpl">
<property name="dataSource" ref="jdbcDataSource"></property>
</bean>
3. 通过properties文件来配置spring的jdbc
- 创建db.properties文件
jdbc.jdbcUrl=jdbc:mysql://xxx.206.7.239/xiaobang
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123456789
- 修改applicationContext.xml配置文件
<!--指定spring读取db.properties-->
<context:property-placeholder location="classpath:db.properties"/>
<!--将连接池放入spring 容器-->
<bean name="jdbcDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
4. xml配置spring aop 事务
4.1 . 前期准备
-
创建t_account 表
Snip20180803_17.png
创建对应的Account类
public class Account {
private Integer id;
private String name;
private Double money;
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 Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
- 创建AccountDao接口
public interface AccountDao {
public void rollInMoney(Integer id, Double money);
public void rollOutMoney(Integer id, Double money);
// 增
public void save(Account account) throws PropertyVetoException;
// 改
public void update(Account account);
}
- 创建AccountDaoImpl,接口实现类
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
@Override
public void rollInMoney(Integer id, Double money) {
String sql = "update t_account set money = money + ? where id = ? ";
super.getJdbcTemplate().update(sql, money, id);
}
@Override
public void rollOutMoney(Integer id, Double money) {
String sql = "update t_account set money = money - ? where id = ? ";
super.getJdbcTemplate().update(sql, money, id);
}
@Override
public void save(Account account) throws PropertyVetoException {
String sql = "insert into t_account values(null, ?, ?)";
super.getJdbcTemplate().update(sql, account.getName(), account.getMoney());
}
@Override
public void update(Account account) {
String sql = "update t_account set name = ?, money = ? where id = ? ";
super.getJdbcTemplate().update(sql, account.getName(), account.getMoney(), account.getId());
}
}
- 创建AccountService接口
public interface AccountService {
public void transfer(Integer from, Integer to, Double money);
}
- 创建AccountServiceImpl接口实现类
public class AccountServiceImpl implements AccountService {
@Resource(name = "accountDao")
private AccountDao ad;
@Override
public void transfer(Integer from, Integer to, Double money) {
ad.rollOutMoney(from, money);
// int i = 1 / 0;
ad.rollInMoney(to, money);
}
public AccountDao getAd() {
return ad;
}
public void setAd(AccountDao ad) {
this.ad = ad;
}
}
4.2 书写配置文件
- 准备连接池
<!--指定spring读取db.properties-->
<context:property-placeholder location="classpath:db.properties"/>
<!--将连接池放入spring 容器-->
<bean name="jdbcDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
- 添加事务核心管理器
<!--事务核心管理器,封装了所有事务操作,依赖于连接池-->
<bean name="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="jdbcDataSource"></property>
</bean>
- 配置事务通知
<!--配置事务通知-->
<tx:advice id="transactionInterceptor" transaction-manager="dataSourceTransactionManager">
<tx:attributes>
<!-- 以方法为单位,指定方法应用什么事务属性
isolation:隔离级别
propagation:传播行为
read-only:是否只读
-->
<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<!--增-->
<tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<!--改-->
<tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<!--删-->
<tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<!--查-->
<tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
<tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
- 织入通知
<!--配置织入-->
<aop:config>
<aop:pointcut id="transactionPc" expression="execution(public void service.AccountServiceImpl.transfer(..))"/>
<aop:advisor advice-ref="transactionInterceptor" pointcut-ref="transactionPc"/>
</aop:config>
- 将其他类放入spring容器中
<!--将userDao的实现类放入spring容器-->
<bean name="userDao" class="daoImpl.UserDaoImpl">
<property name="dataSource" ref="jdbcDataSource"></property>
</bean>
<!--将accountDao的实现类放入spring容器-->
<bean name="accountDao" class="daoImpl.AccountDaoImpl">
<property name="dataSource" ref="jdbcDataSource"></property>
</bean>
<!--将service的实现类放入spring容器-->
<bean name="accountService" class="service.AccountServiceImpl">
<property name="ad" ref="accountDao"></property>
</bean>
4.3 测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo2 {
@Resource(name = "accountDao")
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Resource(name = "accountService")
private AccountService accountService;
// 改
@Test
public void testFunc01() throws PropertyVetoException {
Account account = new Account();
account.setId(2);
account.setName("lisi");
account.setMoney(1000d);
accountDao.update(account);
}
@Test
public void testFunc02() {
accountService.transfer(1,2, 100d);
}
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
}
5. 使用注解配置aop事务
5.1 配置文件
<!-- 事务核心管理器,封装了所有事务操作. 依赖于连接池 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="jdbcDataSource" ></property>
</bean>
<!--开启使用注解管理事务-->
<tx:annotation-driven/>
5.2 实现将通知织入的注解
@Override
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, readOnly = false)
public void transfer(Integer from, Integer to, Double money) {
ad.rollOutMoney(from, money);
// int i = 1 / 0;
ad.rollInMoney(to, money);
}
5.3 测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:jdbcTemplate/applicationContext.xml")
public class Demo3 {
@Resource(name = "accountService")
private AccountService accountService;
@Test
public void testFunc() {
accountService.transfer(1,2, 100d);
}
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
}
5.4 注解放在类和方法上
// 给所有方法加注解
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, readOnly = false)
public class AccountServiceImpl implements AccountService {
@Resource(name = "accountDao")
private AccountDao ad;
@Override
// 单独给某个方法加不同的注解
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, readOnly = true)
public void transfer(Integer from, Integer to, Double money) {
ad.rollOutMoney(from, money);
// int i = 1 / 0;
ad.rollInMoney(to, money);
}
public AccountDao getAd() {
return ad;
}
public void setAd(AccountDao ad) {
this.ad = ad;
}
}
网友评论