五.Spring-jdbc的实现
第一步:导jar包 pom.xml
<!--引入Spring-beans节点-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
<!--引入Spring-context节点-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<!--引入Spring-JDBC节点-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.wisdom-framework</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34_1</version>
</dependency>
第二步:实体层
public class User {
private Integer bookid;
private String bookname;
private Integer bookprice;
public Integer getBookid() {
return bookid;
}
public void setBookid(Integer bookid) {
this.bookid = bookid;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public Integer getBookprice() {
return bookprice;
}
public void setBookprice(Integer bookprice) {
this.bookprice = bookprice;
}
}
第三步:搭建分层
A:dao层
接口:
//01.查询素有图书
public List<User> findAll();
接口实现:需要实现接口,继承JdbcDaoSupport 植入jdbcTemplate:简化jdbc操作
public class userdaoimp extends JdbcDaoSupport implements userdao {
// 需要植入 jdbcTemplate
public List<User> findAll() {
String sql="select * from book";
//接口的一个实现类 匿名内部类
List<User> list=this.getJdbcTemplate().query(sql, new RowMapper<User>() {
public User mapRow(ResultSet rs, int i) throws SQLException {
User book=new User();
book.setBookid(rs.getInt("bookid"));
book.setBookname(rs.getString("bookname"));
book.setBookprice(rs.getInt("bookprice"));
return book;
}
});
return list;
}
}
第四步:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!--00.识别jdbc.properties文件-->
<context:property-placeholder location="jdbc.properties"></context:property-placeholder>
<!--01.建立数据源 Spring 内置的一个数据源 DriverManager-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--02.jdbcTemplate 配置-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--03.dao配置-->
<bean id="bookDao" class="cn.dao.imp.userdaoimp">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!--04.service bookService-->
<bean id="bookService" class="cn.service.impl.BookDAOImpl">
<property name="dao" ref="bookDao"></property>
</bean>
</beans>
第五步:service层
实现层:
public class BookDAOImpl implements IBookService {
//植入dao
private userdao dao;
public List<User> findAll() {
return dao.findAll();
}
public userdao getDao() {
return dao;
}
public void setDao(userdao dao) {
this.dao = dao;
}
}
接口层:
//01.查询素有图书
public List<User> findAll();
第六步:单测类
@Test
// 01.jdbctemplate
public void test02(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
IBookService service = (IBookService) ctx.getBean("bookService");
List<User> list = service.findAll();
for (User item:list) {
System.out.println(item.getBookname());
}
}
七:事务实现(买股票案例)
A:dao层
a:接口层
Account类:
public interface Accountdao {
//开户方法
public boolean add(Account account);
//修改账户金额 为true
public boolean update(int aid,double blance,boolean isbuy);
}
股票接口:
Account接口实现类:
public interface Stockdao {
public boolean addstock(stock st);
public boolean update(int sid,int count,boolean isUser);
}
b:接口实现层:
public class Accountimp extends JdbcDaoSupport implements Accountdao
{
public boolean add(Account account) {
return false;
}
public boolean update(int aid, double blance, boolean isbuy) {
boolean flag=false;
String sql=null;
if (isbuy){ //购买股票
sql="update acount set blance=blance-? where aid=?";
}else{
sql="update account set blance=blance+? where aid=?";
}
int count = this.getJdbcTemplate().update(sql, blance, aid);
if (count>0){
flag=true;
}
return flag;
}
}
股票接口实现类 stock:
public class Stockdaoimp extends JdbcDaoSupport implements Stockdao{
public boolean addstock(stock st) {
return false;
}
public boolean update(int sid, int count, boolean isUser) {
boolean flag=false;
String sql=null;
if (isUser){
sql="update stock set count=count+? where sid=?";
}else{
sql="update stock set count=count-? where sid=?";
}
int str = this.getJdbcTemplate().update(sql, count, sid);
if (str>0){
flag=true;
}
return flag;
}
Service层:
接口:
public interface Accountdaoservicedao {
//开户方法
public boolean add(Account account);
public boolean addstock(stock st);
public void stock(int aid,double blance,int sid,int count) throws Exception;
}
接口实现:
public class Accountserviceimp implements Accountdaoservicedao {
private Accountdao accountDAO;
private Stockdao stockDAO;
public void stock(int aid, double blance, int sid, int count) throws Exception {
//默认是购买股票
boolean isBuy=true;
accountDAO.update(aid,blance,isBuy);
//编译时异常 SPring处理 方式
//编译时 SPring事务自动提交
//运行时 SPring 自动回滚
if(1==1) {
throw new StockException();
}
//02.股票表
stockDAO.update(sid,count,isBuy);
}
public Accountdao getAccountDAO() {
return accountDAO;
}
public void setAccountDAO(Accountdao accountDAO) {
this.accountDAO = accountDAO;
}
public Stockdao getStockDAO() {
return stockDAO;
}
public void setStockDAO(Stockdao stockDAO) {
this.stockDAO = stockDAO;
}
public boolean add(Account account) {
return false;
}
public boolean addstock(stock st) {
return false;
}
异常类:
public class StockException extends Exception {
public StockException() {
}
public StockException(String message) {
super(message);
}
}
实体类:
Xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--00.识别jdbc.properties文件-->
<context:property-placeholder location="jdbc.properties"></context:property-placeholder>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--03.dao配置-->
<bean id="Accountdao" class="Spring01tx.cn.dao.imp.Accountimp">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="Stockdao" class="Spring01tx.cn.dao.imp.Stockdaoimp">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!--04.service bookService-->
<bean id="bookService" class="Spring01tx.cn.service.imp.Accountserviceimp">
<property name="accountDAO" ref="Accountdao"></property>
<property name="stockDAO" ref="Stockdao"/>
</bean>
<!--事务管理器-->
<bean id="transcationManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置事务 拦截器业务方法-->
<bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="target" ref="bookService"></property>
<property name="transactionManager" ref="transcationManger"></property>
<!--增强-->
<property name="transactionAttributes">
<props>
<prop key="buy*">ISOLATION_DEFAULT,PROPAGATION_REQUIRED,-StockException</prop>
</props>
</property>
</bean>
</beans>
单测:
public class test {
@Test
// 01.jdbctemplate
public void test02() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext01.xml");
Accountserviceimp service = (Accountserviceimp) ctx.getBean("accountServiceProxy");
try {
//数据不匹配时的情况 一个变,一个不变
service.stock(1,1000,1,8);
} catch (Exception e){
e.printStackTrace();
}
}