JavaWeb学习之Spring框架(二)
spring整合JDBC
spring提供了很多模板整合Dao技术
spring中提供了一个可以操作数据库的对象,对象封装了jdbc技术
JDBCTemplate----JDBC模板技术
与DButils中的QueryRunner非常相似
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | package com.jdbcdemo; import java.beans.PropertyVetoException; import org.junit.Test; import org.springframework.jdbc.core.JdbcTemplate; import com.mchange.v2.c3p0.ComboPooledDataSource; public class JDBCDemo { @Test public void method() throws PropertyVetoException{ //创建连接池对象 ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setDriverClass( "com.mysql.jdbc.Driver" ); dataSource.setJdbcUrl( "jdbc:mysql:///spring_demo?characterEncoding=utf-8" ); dataSource.setUser( "root" ); dataSource.setPassword( "123456" ); //创建JDBC模板对象 JdbcTemplate jdbcTemplate= new JdbcTemplate(); jdbcTemplate.setDataSource(dataSource); //书写sql语句并执行 String sql= "insert into s_user(name) values('李四')" ; jdbcTemplate.update(sql); } } |
步骤:导包:4个基础包+2个日志包+spring-test、spring-aop、junit4类库+c3p0连接池、spring-tx事务
准备数据库
书写Dao层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | package com.dao; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.support.JdbcDaoSupport; import com.domain.User; public class UserDao extends JdbcDaoSupport { // private JdbcTemplate jdbcTemplate; // // public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { // this.jdbcTemplate = jdbcTemplate; // } public void save(User user) { String sql = "insert into s_user(name) values(?)" ; getJdbcTemplate().update(sql, user.getName()); } public void update(User user) { String sql = "update s_user set name=? where id=?" ; getJdbcTemplate().update(sql, user.getName(), user.getId()); } public void delete(Integer id) { String sql = "delete from s_user where id=?" ; getJdbcTemplate().update(sql, id); } // 单个对象查询 public User findById(Integer id) { String sql = "select * from s_user where id=?" ; return getJdbcTemplate().queryForObject(sql, new RowMapper<User>() { public User mapRow(ResultSet rs, int arg1) throws SQLException { // TODO Auto-generated method stub User user = new User(); user.setId(rs.getInt( "id" )); user.setName(rs.getString( "name" )); return user; } }, id); } // 查询单个值 public int getCount() { String sql = "select count(*) from s_user" ; return getJdbcTemplate().queryForObject(sql, Integer. class ); } // 查询List<User> public List<User> getAll() { String sql = "select * from s_user" ; return getJdbcTemplate().query(sql, new RowMapper<User>() { public User mapRow(ResultSet rs, int arg1) throws SQLException { // TODO Auto-generated method stub User user = new User(); user.setId(rs.getInt( "id" )); user.setName(rs.getString( "name" )); return user; } }); } } |
spring配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <?xml version= "1.0" encoding= "UTF-8" ?> <beans xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "http://www.springframework.org/schema/beans" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd " > <!-- datasource --> <context:property-placeholder location= "classpath:db.properties" /> <bean name= "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name= "dataSource" ref= "dataSource" ></property> </bean> <bean name= "dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource" > <property name= "driverClass" value= "${jdbc.driverClass}" ></property> <property name= "jdbcUrl" value= "${jdbc.jdbcUrl}" ></property> <property name= "user" value= "${jdbc.user}" ></property> <property name= "password" value= "${jdbc.password}" ></property> </bean> <!-- jdbcTemplate --> <!-- <bean name= "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate" > <property name= "dataSource" ref= "dataSource" ></property> </bean>--> <!-- UserDao --> <bean name= "userDao" class = "com.dao.UserDao" > <property name= "dataSource" ref= "dataSource" ></property> </bean> <!-- AccountDao --> <bean name= "accountDao" class = "com.dao.AccountDao" > <property name= "dataSource" ref= "dataSource" ></property> </bean> <bean name= "accountService" class = "com.service.AccountService" > <property name= "accountDao" ref= "accountDao" ></property> </bean> <!-- 注解配置事务 --> <tx:annotation-driven/> <!-- XML配置事务 --> <!-- <tx:advice id= "txAdvice" transaction-manager= "transactionManager" > <tx:attributes> <tx:method name= "transfer" isolation= "REPEATABLE_READ" propagation= "REQUIRED" read-only= "false" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression= "execution(* com.service.*Service.*(..))" id= "txpc" /> <aop:advisor advice-ref= "txAdvice" pointcut-ref= "txpc" /> </aop:config>--> </beans> |
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | package com.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.dao.UserDao; import com.domain.User; import com.service.AccountService; @RunWith (SpringJUnit4ClassRunner. class ) @ContextConfiguration ( "classpath:applicationContext.xml" ) public class Demo { // 引用注入 @Autowired // 自动装配 @Qualifier ( "userDao" ) // 使用Qualifier注解告诉spring容器自动装配哪个名称的对象 private UserDao userDao; @Autowired // 自动装配 @Qualifier ( "accountService" ) private AccountService accountService; @Test public void method1() { User user = new User(); user.setName( "qwerty" ); userDao.save(user); } @Test public void method2() { accountService.transfer( 1 , 2 , 2000d); } } |
进阶内容
JDBCDaoSupport
读取外部的properties配置
1 2 3 4 | jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql: ///spring_demo?characterEncoding=utf-8 jdbc.user=root jdbc.password= 123456 |
spring中的aop事务
事务:事务特性:acid
事务并发问题:脏读、不可重复读、幻读
事务的隔离级别:1 读未提交 2 读已提交 4 可重复读 8 串行化
spring封装了事务管理代码
事务操作:打开事务、提交事务、回滚事务
事务操作对象:因为在不同平台,操作事务的代码各不相同,spring提供了一个接口
PlatformTransactionManager接口:DataSourceTransactionManager
HibernateTransitionmanager
注:在spring中玩事务管理,最为核心的对象就是TransactionManager
spring管理事务的属性介绍:事务的隔离级别:1 读未提交 2 读已提交 4 可重复读 8 串行化
是否只读:true 只读 false 可操作
事务的传播行为
spring管理事务的方式
XML配置(aop)
注解配置(aop)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <?xml version= "1.0" encoding= "UTF-8" ?> <beans xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "http://www.springframework.org/schema/beans" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd " > <!-- datasource --> <context:property-placeholder location= "classpath:db.properties" /> <bean name= "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name= "dataSource" ref= "dataSource" ></property> </bean> <bean name= "dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource" > <property name= "driverClass" value= "${jdbc.driverClass}" ></property> <property name= "jdbcUrl" value= "${jdbc.jdbcUrl}" ></property> <property name= "user" value= "${jdbc.user}" ></property> <property name= "password" value= "${jdbc.password}" ></property> </bean> <!-- jdbcTemplate --> <!-- <bean name= "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate" > <property name= "dataSource" ref= "dataSource" ></property> </bean>--> <!-- UserDao --> <bean name= "userDao" class = "com.dao.UserDao" > <property name= "dataSource" ref= "dataSource" ></property> </bean> <!-- AccountDao --> <bean name= "accountDao" class = "com.dao.AccountDao" > <property name= "dataSource" ref= "dataSource" ></property> </bean> <bean name= "accountService" class = "com.service.AccountService" > <property name= "accountDao" ref= "accountDao" ></property> </bean> <!-- 注解配置事务 --> <tx:annotation-driven/> <!-- XML配置事务 --> <!-- <tx:advice id= "txAdvice" transaction-manager= "transactionManager" > <tx:attributes> <tx:method name= "transfer" isolation= "REPEATABLE_READ" propagation= "REQUIRED" read-only= "false" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression= "execution(* com.service.*Service.*(..))" id= "txpc" /> <aop:advisor advice-ref= "txAdvice" pointcut-ref= "txpc" /> </aop:config>--> </beans> |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步