原理和DBUtil差不多,但是是单例模式,感觉也不必纯JDBC方便多少
每一个操作sql都记得重新写对象吧……不然就会报错。
原始数据源配置
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=UTF-8" /> <property name="username" value="root" /> <property name="password" value="9622738" /> <!-- 连接池启动的初始值 --> <property name="initialSize" value="1"></property> <!-- 连接池的最大值 --> <property name="maxActive" value="500"></property> </bean>
注解方式
注意命名空间要正确
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd "> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/springjdbc?useUnicode=true&characterEncoding=UTF-8" /> <property name="username" value="root" /> <property name="password" value="9622738" /> <!-- 连接池启动的初始值 --> <property name="initialSize" value="1"></property> <!-- 连接池的最大值 --> <property name="maxActive" value="500"></property> </bean> <!-- 事务管理 :把数据源注入到此属性中 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 需要tx命名空间 注解方式实现事务,此功能为注解解析 --> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- 配置业务bean,并且把数据源注入 --> <bean id = "personService" class="com.yiki.imp.PersonImp"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
注意:dataSource这个bean不管是是在xml里还是在bean内的set都要一致
@Transactional也要写进去,让spring去管理这些事务
此作用如果抛出异常就会进行回滚,
package com.yiki.bean; public class Person { private Integer id; private String name; public Person() { // TODO Auto-generated constructor stub } public Person(String name) { this.name=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; } }
package com.yiki.dao; import java.util.List; import com.yiki.bean.Person; public interface PersonService { public void save(Person person); public void update(Person person); public Person getPerson (Integer personid); public List<Person> getPersons(); public void delete(Integer personid); }
package com.yiki.imp; import java.sql.ResultSet; import java.sql.SQLException; import org.springframework.jdbc.core.RowMapper; import com.yiki.bean.Person; public class PersonRowMapper implements RowMapper { @Override public Object mapRow(ResultSet rs, int index) throws SQLException { Person person = new Person(rs.getString("name")); person.setId(rs.getInt("id")); return person; } }
package com.yiki.imp; import java.util.List; import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.yiki.bean.Person; import com.yiki.dao.PersonService; @Transactional public class PersonImp implements PersonService { // 依赖注入的datashource作为参数传入 private JdbcTemplate template; public void setDataSource(DataSource dataSource) { this.template = new JdbcTemplate(dataSource); } @Override public void save(Person person) { String sql = "insert into person(name) value(?)"; template.update(sql, new Object[] { person.getName() }, new int[] { java.sql.Types.VARCHAR }); } @Override public void update(Person person) { String sql = "update person set name=? where id=?"; template.update(sql, // 传参啦-》? new Object[] { person.getName(), person.getId() }, new int[] { java.sql.Types.VARCHAR, java.sql.Types.INTEGER }); } @SuppressWarnings("unchecked") @Transactional(propagation=Propagation.NOT_SUPPORTED)//在方法执行前不产生事务 @Override public Person getPerson(Integer personid) { String sql = "select * from person where id=?"; return (Person) template.queryForObject(sql, new Object[] { personid }, new int[] { java.sql.Types.INTEGER }, new PersonRowMapper()); } @Override public List<Person> getPersons() { String sql = ""; return (List<Person>) template.query(sql, new PersonRowMapper()); } @Override public void delete(Integer personid) { String sql = "delete from " + "person where id=?"; template.update(sql, new Object[] { personid }, new int[] { java.sql.Types.INTEGER }); } }
测试代码
package com.yiki.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.yiki.bean.Person; import com.yiki.dao.PersonService; public class Tests { public static void main(String[] args) { ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml"); PersonService person = (PersonService) cxt.getBean("personService"); person.save(new Person("yiki")); person.save(new Person("tiffany")); Person p=person.getPerson(1); System.out.println(p.getName()); p.setName("change"); person.update(p); person.delete(2); } }
@Transactional注解
@Transactional属性
属性 | 类型 | 描述 |
---|---|---|
value | String | 可选的限定描述符,指定使用的事务管理器 |
propagation | enum: Propagation | 可选的事务传播行为设置 |
isolation | enum: Isolation | 可选的事务隔离级别设置 |
readOnly | boolean | 读写或只读事务,默认读写 |
timeout | int (in seconds granularity) | 事务超时时间设置 |
rollbackFor | Class对象数组,必须继承自Throwable | 导致事务回滚的异常类数组 |
rollbackForClassName | 类名数组,必须继承自Throwable | 导致事务回滚的异常类名字数组 |
noRollbackFor | Class对象数组,必须继承自Throwable | 不会导致事务回滚的异常类数组 |
noRollbackForClassName | 类名数组,必须继承自Throwable | 不会导致事务回滚的异常类名字数组 |