spring整合JDBCjava
spring提供了不少模板整合Dao技術mysql
spring中提供了一個能夠操做數據庫的對象,對象封裝了jdbc技術spring
JDBCTemplate----JDBC模板技術sql
與DButils中的QueryRunner很是類似數據庫
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事務express
準備數據庫併發
書寫Dao層app
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配置dom
<?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>
測試測試
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配置
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)
<?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>