#使用jdbc模版#
<!--实现类-->
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContext.xml")
public class jdbc01 {
@Resource(name="jdbct")
private static JdbcTemplate jdbct;
public static JdbcTemplate getjdbc(){
return jdbct;
}
@Test
public void test() {
jdbct.update("INSERT INTO mysql.tb_admin (ID, NAME, PASSWORD, POWER, flag)"
+ "VALUES(?, ?, ?, ?, ?)","221","xx","1234","2","1");
}
@Test
public void test01(){
jdbct.update("INSERT INTO mysql.account (id, NAME, money)VALUES(?, ?, ?)",12,"www",12);
}
@Test
public void test02(){
int i=jdbct.queryForObject("select count(*) from mysql.account ",int.class);
System.out.println(i);
}
@Test
public void test03(){
String name=jdbct.queryForObject("select name from mysql.account where id=? ",String.class,22);
System.out.println(name);
}
@Test
public void test04(){
Account name=jdbct.queryForObject("select * from mysql.account where id=? ",new MyRowMapper(),22);
System.out.println(name.toString());
}
@Test
public void test05(){
List<Account> name=jdbct.query("select * from mysql.account ",new MyRowMapper());
for (Account account : name) {
System.out.println(account.toString());
}
}
}
(通过RowMapper来映射数据到类,从而在测试方法中处理接收数据)
class MyRowMapper implements RowMapper<Account>{
@Override
public Account mapRow(ResultSet rSet, int arg1) throws SQLException {
Account account=new Account();
account.setId(rSet.getInt("id"));
account.setName(rSet.getString("name"));
account.setMoney(rSet.getDouble("money"));
return account;
}
}
#xml#
<bean id="jdbct" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="url" value="jdbc:mysql://localhost/mysql"></property>
</bean>
#事务#
事务介绍:逻辑上的一组操作,组成这组操作的各个单元,理论上来说,可以有无限个,要么全部成功,要么全部失败
特性:
原子性:事务是不可分割的
一致性:事务执行前后数据完整性保持一致
隔离性:一个事务的执行不应该受到其他事务的影响
持久性:事务一旦执行了,数据库就进行了持久化
1. 编写事务代码管理
2. XML方式申明事务
3. 注解的方式申明事务
比较差别:如果使用的是注解,那么需要很慎重。因为可能忘记了添加注解,后期维护可能也较为麻烦。
如果使用XML,因为我们可以使用aop的思想,所以直接一步到位
#spring事务#
编程式事务管理:编程方式管理事务,极大灵活性,难维护。
声明式事务管理:可以将业务代码和事务管理分离,用注解和xml配置来管理事务
工厂模式:每个Bean的创建通过方法
单例模式:默认的每个Bean的作用域都是单例
代理模式:关于Aop的实现通过代理模式 动态代理
#实现类#
<!-- dao层 -->
(通过继承JdbcDaoSupport直接获取JdbcTemplate()调用方法)
public class Accountlmpl extends JdbcDaoSupport implements AccountDao {
@Override
public void save(String name, Double i) {
this.getJdbcTemplate().update("UPDATE mysql.account SET money = money+? WHERE NAME = ? ", i, name);
}
@Override
public void get(String name, Double i) {
// TODO Auto-generated method stub
this.getJdbcTemplate().update("UPDATE mysql.account SET money = money-? WHERE NAME = ? ", i, name);
}
}
<!-- 服务层 -->
@Transactional(propagation=Propagation.REQUIRED)
(如果xml写了 <tx:annotation-driven transaction-manager="TransactionManager"/>这个则直接使用上面的
注解方式直接使用调用事务)
public class AccountSerlmpl implements AccountSer {
private AccountDao accountDao;
private TransactionTemplate transactionTemplate;
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
}
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void transfor(final String n1, final String n2, final Double i) {
(//内代码是开始使用transactionTemplate调用事务,后面使用aop直接事务处理transfor方法)
// transactionTemplate.execute(new TransactionCallbackWithoutResult() {
// @Override
// protected void doInTransactionWithoutResult(TransactionStatus arg0) {
// TODO Auto-generated method stub
accountDao.save(n1, i);
(报数学计算错误,开启事务后,报错后面的不执行,前面的也不执行【一致性】)
// int i1=10/0;
accountDao.get(n2, i);
// }
// });
//
}
}
##测试类##
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContext4.xml")
public class AccountTest {
@Resource(name="Ser")
private AccountSer Ser;
@Test
public void test() {
Ser.transfor("www", "ww", 5d);
}
}
#spring的xml注解#
直接复制使用
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>
#xml#
<!--数据源 注解模式导入-->
<context:property-placeholder location="classpath:database.properties"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="username" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="url" value="${url}"></property>
</bean>
<!--Spring配置平台管理器 -->
<bean id="TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
(默认:scope="singleton单例" 可设置成多例 prototype)
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务管理模版 -->
<bean id="TransactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="TransactionManager"></property>
</bean>
<!-- dao层 -->
<bean id="accountdao" class="Dao.Accountlmpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 服务层 -->
<bean id="Ser" class="Dao.AccountSerlmpl">
<property name="accountDao" ref="accountdao"></property>
<property name="transactionTemplate" ref="TransactionTemplate"></property>
</bean>
# 利用aop切面来使用事务 #
<!-- 配置事务的增强 -->
<tx:advice id="advice" transaction-manager="TransactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* Dao.AccountSerlmpl.*(..))" id="point1"/>
<aop:advisor advice-ref="advice" pointcut-ref="point1"/>
</aop:config>
#错误提示#
Failed to load ApplicationContext
查看xml配置文件。不规范