springJDBC在事务管理方面更占优点,同时处理速度也比mybatis快一点。java
本文代码连接mysql
Jdbc使用步骤:
Maven工程弓|入依赖spring-jdbc
applicationContext.xml配置DataSource数据源
在Dao注入JdbcTemplate对象,实现数据CRUDgit
1:首先在pom中引入如下包spring
<dependencies> <!-- spring ioc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.6.RELEASE</version> </dependency> <!--spring jdbc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.6.RELEASE</version> </dependency> <!--mysql引入--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency> <!-- 单元测试--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.4.RELEASE</version> </dependency> <!-- 单元测试 框架--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> </dependencies>
2:建立一个实体sql
public class Employee { private Integer eno; private String ename; private Float salary; private String dname; private Date hiredate; public Integer getEno() { return eno; } public void setEno(Integer eno) { this.eno = eno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public Float getSalary() { return salary; } public void setSalary(Float salary) { this.salary = salary; } public String getDname() { return dname; } public void setDname(String dname) { this.dname = dname; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } @Override public String toString() { return "Employee{" + "eno=" + eno + ", ename='" + ename + '\'' + ", salary=" + salary + ", dname='" + dname + '\'' + ", hiredate=" + hiredate + '}'; }
3:建立dao数据库
public class EmployeeDao { private JdbcTemplate jdbcTemplate; public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } /** * 根据id查找数据 * queryForObject 返回单条数据 * * @param eno * @return */ public Employee findById(Integer eno) { String sql = "SELECT * FROM employee WHERE eno = ?"; //查询单条数据 若是有多个条件,则 new Object[]{eno,eno2,eno3} 数组中的顺序对应着sql中问好的顺序 Employee employee = jdbcTemplate.queryForObject(sql, new Object[]{eno}, new BeanPropertyRowMapper<Employee>(Employee.class)); return employee; } /** * query 返回list * * @param depName * @return */ public List<Employee> findByName(String depName) { String sql = "SELECT * FROM employee WHERE dname = ?"; List<Employee> query = jdbcTemplate.query(sql, new Object[]{depName}, new BeanPropertyRowMapper<Employee>(Employee.class)); return query; } /** * queryForList 返回map集合数据 ,当返回的字段名字是entity中没有的时候就使用这个 * @param depName * @return */ public List<Map<String, Object>> findMapByName(String depName) { String sql = "SELECT eno enoAs,ename enameAs,salary salaryAs,dname dnameAs FROM employee WHERE dname = ?"; List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql, new Object[]{"depName"}); return maps; } public void insert(Employee employee){ String sql = "insert into employee(eno,ename,salary,dname,hiredate) values(?,?,?,?,?)"; //利用update方法实现数据写入操做 jdbcTemplate.update(sql,new Object[]{ employee.getEno() , employee.getEname(),employee.getSalary(),employee.getDname() , employee.getHiredate() }); } public int update(Employee employee){ String sql = "UPDATE employee SET ename = ?, salary = ?, dname = ?, hiredate = ? WHERE eno = ?"; int count = jdbcTemplate.update(sql, new Object[]{employee.getEname(), employee.getSalary(), employee.getDname(), employee.getHiredate(), employee.getEno()}); return count; } public int delete(Integer eno){ String sql = "delete from employee where eno = ?"; return jdbcTemplate.update(sql, new Object[]{eno}); } }
4:建立serviceexpress
public class EmployeeService { private EmployeeDao employeeDao; private DataSourceTransactionManager transactionManager; public EmployeeDao getEmployeeDao() { return employeeDao; } public void setEmployeeDao(EmployeeDao employeeDao) { this.employeeDao = employeeDao; } public DataSourceTransactionManager getTransactionManager() { return transactionManager; } public void setTransactionManager(DataSourceTransactionManager transactionManager) { this.transactionManager = transactionManager; } //编程性事务 public void batchImport(){ //定义了事务默认的标准配置 TransactionDefinition definition = new DefaultTransactionDefinition(); //开始一个事务,返回事务状态,事务状态说明当前事务的执行阶段 TransactionStatus status = transactionManager.getTransaction(definition); try { for (int i = 1; i <= 10; i++) { /*if (i == 3) { throw new RuntimeException("意料以外的异常"); }*/ Employee employee = new Employee(); employee.setEno(8000 + i); employee.setEname("员工" + i); employee.setSalary(4000f); employee.setDname("市场部"); employee.setHiredate(new Date()); employeeDao.insert(employee); } transactionManager.commit(status); //提交事务 }catch (RuntimeException e){ transactionManager.rollback(status); //回滚事务 throw e; } } }
5:建立applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:33068/imooc?useSSL=false&useUnicode=true& characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true"/> <property name="username" value="root"/> <property name="password" value="Ee123"/> </bean> <!-- JdbcTemplate提供数据CRUD的api--> <bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="employeeDao" class="com.item.spring.jdbc.dao.EmployeeDao"> <!--为Dao注入JdbcTemplate对象--> <property name="jdbcTemplate" ref="JdbcTemplate"/> </bean> <!-- 编程性 事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="employeeService" class="com.item.spring.jdbc.service.EmployeeService"> <property name="employeeDao" ref="employeeDao"/> <property name="transactionManager" ref="transactionManager"/> </bean> </beans>
6:建立一个测试类api
/** * @RunWith+ @ContextConfiguration做用= ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); * */ @RunWith(SpringJUnit4ClassRunner.class) //JUnit 控制权交给spring @ContextConfiguration(locations = {"classpath:applicationContext.xml"}) public class JdbctemplateTestor { @Resource private EmployeeDao employeeDao; @Resource private EmployeeService employeeService; @Test public void textFindById(){ Employee byId = employeeDao.findById(3308); System.out.println(byId); } @Test public void findByName(){ List<Employee> list = employeeDao.findByName("研发部"); System.out.println(list); } @Test public void findMapByName(){ List<Map<String, Object>> map = employeeDao.findMapByName("研发部"); System.out.println(map); } @Test public void testBatchImport(){ employeeService.batchImport(); System.out.println("批量导入成功"); } }
1:applicationContext.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:33068/imooc?useSSL=false&useUnicode=true& characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true"/> <property name="username" value="root"/> <property name="password" value="Ee123"/> </bean> <!--JdbcTemplate提供数据CRUD的API--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="employeeDao" class="com.item.springJdbc.dao.EmployeeDao"> <!--为Dao注入JdbcTemplate对象--> <property name="jdbcTemplate" ref="jdbcTemplate"/> </bean> <bean id="batchService" class="com.item.springJdbc.service.BatchService"> <property name="employeeDao" ref="employeeDao"/> </bean> <bean id="employeeService" class="com.item.springJdbc.service.EmployeeService"> <property name="employeeDao" ref="employeeDao"/> <property name="batchService" ref="batchService"/> </bean> <!--
此处事务的做用域是com.item下任何子子孙孙/子包,只要文件是Service结尾,的方法都将使用到事务。而具体是某些方法在<tx:advice id="txAdvice" transaction-manager="transactionManager">中配置
<tx:method name="batchImport" propagation="REQUIRED"/> 说明只要方法名是batchImport,都将使用事务
--> <!-- 1.事务管理器,用于建立事务/提交/回滚 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--2.事务通知配置,决定哪些方法使用事务,哪些方法不使用事务 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 目标方法名为batchImport时,启用声明式事务,成功提交,运行时异常回滚 --> <tx:method name="batchImport" propagation="REQUIRED"/> <tx:method name="batch*" propagation="REQUIRED"/> <!-- 设置全部findXXX方法不须要使用事务 --> <tx:method name="find*" propagation="NOT_SUPPORTED" read-only="true"/> <tx:method name="get*" propagation="NOT_SUPPORTED" read-only="true"/> <tx:method name="importJob1" propagation="REQUIRES_NEW"/> <tx:method name="importJob2" propagation="REQUIRES_NEW"/> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!--3. 定义声明式事务的做用范围--> <aop:config> <aop:pointcut id="pointcut" expression="execution(* com.item..*Service.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> </aop:config> </beans>
2:service文件
public class EmployeeService { public EmployeeDao getEmployeeDao() { return employeeDao; } public void setEmployeeDao(EmployeeDao employeeDao) { this.employeeDao = employeeDao; } public BatchService getBatchService() { return batchService; } public void setBatchService(BatchService batchService) { this.batchService = batchService; } private EmployeeDao employeeDao; private BatchService batchService; public void batchImport() { for (int i = 1; i <= 10; i++) {
//此处代码为测试事务使用 // if(i==3){ // throw new RuntimeException("意料以外的异常"); // } Employee employee = new Employee(); employee.setEno(8000 + i); employee.setEname("员工" + i); employee.setSalary(4000f); employee.setDname("市场部"); employee.setHiredate(new Date()); employeeDao.insert(employee); } } public void startImportJob(){ batchService.importJob1(); if(1==1){ throw new RuntimeException("意料以外的异常"); } batchService.importJob2(); System.out.println("批量导入成功"); } }
applicationContext.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="com.item"/> <!--数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:33068/imooc?useSSL=false&useUnicode=true& characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true"/> <property name="username" value="root"/> <property name="password" value="Ee123"/> </bean> <!--JdbcTemplate--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <!--事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 启用注解形式声明式事务 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
service:
@Service //声明式事务核心注解 //放在类上,将声明式事务配置应用于当前类全部方法,默认事务传播为 REQUIRED @Transactional public class EmployeeService { @Resource private EmployeeDao employeeDao; @Resource private BatchService batchService; @Transactional(propagation = Propagation.NOT_SUPPORTED , readOnly = true) public Employee findById(Integer eno){ return employeeDao.findById(eno); } public void batchImport() { for (int i = 1; i <= 10; i++) { if(i==3){ throw new RuntimeException("意料以外的异常"); } Employee employee = new Employee(); employee.setEno(8000 + i); employee.setEname("员工" + i); employee.setSalary(4000f); employee.setDname("市场部"); employee.setHiredate(new Date()); employeeDao.insert(employee); } } public void startImportJob(){ batchService.importJob1(); if(1==1){ throw new RuntimeException("意料以外的异常"); } batchService.importJob2(); System.out.println("批量导入成功"); } public EmployeeDao getEmployeeDao() { return employeeDao; } public void setEmployeeDao(EmployeeDao employeeDao) { this.employeeDao = employeeDao; } public BatchService getBatchService() { return batchService; } public void setBatchService(BatchService batchService) { this.batchService = batchService; } }
至关于只有一个事务
即使多个事务放在一块儿执行,也是多个事务。每一个事务间互不影响