版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/QuietHRH/article/details/82533683
JDBCTemplate
测试类
package com.hrh.a;
import com.hrh.pojo.Book;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import java.util.List;
public class BookDao extends JdbcDaoSupport{
public void insert(Book book){
String sql="insert into book (id,name,price)values(null,?,?);";
Object[] params={book.getName(),book.getPrice()};
super.getJdbcTemplate().update(sql,params);
}
public void update(Book book){
String sql="update book set name=?, price=? where id=?";
Object[] param={book.getName(),book.getPrice(),book.getId()};
super.getJdbcTemplate().update(sql,param);
}
public void delete(Book book){
String sql="delete from book where id=?";
super.getJdbcTemplate().update(sql,book.getId());
}
public String queryById(int id){
String sql="select name from book where id=?;";
return super.getJdbcTemplate().queryForObject(sql,String.class,id);
}
public int queryCount(){
String sql="select count(*) from book";
return super.getJdbcTemplate().queryForObject(sql,Integer.class);
}
public Book queryBookById(int id){
String sql="select * from book where id=?;";
Book book = super.getJdbcTemplate().queryForObject(sql, ParameterizedBeanPropertyRowMapper.newInstance(Book.class), id);
return book;
}
public List<Book> queryAll(){
String sql="select * from book";
List<Book> bookList = super.getJdbcTemplate().query(sql, ParameterizedBeanPropertyRowMapper.newInstance(Book.class));
return bookList;
}
}
xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--spring的jdbc-->
<bean id="springJDBC" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///spring_day03"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
</bean>
<!--apache的dbcp-->
<bean id="DBCP" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///spring_day03"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
</bean>
<!--c3p0-->
<bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--JDBC模板-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="c3p0"/>
</bean>
<bean id="bookDao" class="com.hrh.a.BookDao2">
<property name="dataSource" ref="c3p0"/>
</bean>
</beans>
事务管理 XML版
Dao和Service
package com.hrh.dao;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class AccountDao extends JdbcDaoSupport{
public void in(String in,double money){
String sql="update account set money=money+? where name=?";
super.getJdbcTemplate().update(sql,money,in);
}
public void out(String out,double money){
String sql="update account set money=money-? where name=?";
super.getJdbcTemplate().update(sql,money,out);
}
}
package com.hrh.service;
import com.hrh.dao.AccountDao;
public class AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao){
this.accountDao=accountDao;
}
public void transfer(String in,String out,double money){
accountDao.out(out,money);
int a=1/0;
accountDao.in(in,money);
}
}
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 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="accountDao" class="com.hrh.dao.AccountDao">
<property name="dataSource" ref="c3p0"/>
</bean>
<bean id="accountService" class="com.hrh.service.AccountService">
<property name="accountDao" ref="accountDao"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="c3p0"/>
</bean>
<tx:advice id="myAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="transfer"/>
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="false">
<aop:advisor advice-ref="myAdvice" pointcut="bean(*Service)"/>
</aop:config>
</beans>
测试
package com.hrh.service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class AccountServiceTest {
@Autowired
private AccountService accountService;
@Test
public void transfer() throws Exception {
accountService.transfer("aaa","bbb",100);
}
}
事务管理 注解版
Dao和Service
package com.hrh.dao;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;
@Repository
public class AccountDao extends JdbcDaoSupport{
public void out(String out,double money){
String sql="update account set money=money-? where name=?";
super.getJdbcTemplate().update(sql,money,out);
}
public void in(String in,double money){
String sql="update account set money=money+? where name=?";
super.getJdbcTemplate().update(sql,money,in);
}
}
package com.hrh.service;
import com.hrh.dao.AccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class AccountService {
@Autowired
private AccountDao accountDao;
@Transactional
public void transfer(String in,String out,double money){
accountDao.in(in,money);
//int a=1/0;
accountDao.out(out,money);
}
}
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"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="accountDao" class="com.hrh.dao.AccountDao">
<property name="dataSource" ref="c3p0"/>
</bean>
<context:component-scan base-package="com.hrh"/>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="c3p0"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
测试
package com.hrh.service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class AccountServiceTest {
@Value("#{accountService}")
private AccountService accountService;
@Test
public void transfer() throws Exception {
accountService.transfer("aaa","bbb",200);
}
}
Spring和mybatis的整合
Mapper和Service
package com.hrh.mapper;
import org.apache.ibatis.annotations.Param;
public interface AccountMapper {
//入账
public void in(@Param("name") String name, @Param("money") double money);
//出账
public void out(@Param("name") String name, @Param("money") double money);
}
package com.hrh.service;
import com.hrh.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class AccountService {
@Autowired
private AccountMapper accountMapper;
@Transactional //给方法添加事务
public void transfer(String inName,String outName,double money){
accountMapper.in(inName,money);
//int a=1/0;
accountMapper.out(outName,money);
}
}
mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hrh.mapper.AccountMapper">
<update id="in">
update account set money=money+#{money} where name=#{name};
</update>
<update id="out">
update account set money=money-#{money} where name=#{name};
</update>
</mapper>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
<setting name="autoMappingBehavior" value="FULL"/>
</settings>
</configuration>
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"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--导入jdbc配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置连接池-->
<bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--注解扫描包-->
<context:component-scan base-package="com.hrh"/>
<!--扫描mapper包-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com/hrh/*"/>
</bean>
<!--mybatis的sqlsessionFactory放到IOC容器 并进行配置-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="c3p0"/>
<property name="typeAliasesPackage" value="com.hrh"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="c3p0"/>
</bean>
<!--注册事务驱动-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>