Spring笔记⑤--整合hibernate代码测试
String整合hibernate代码测试
在上节生成的表中插入数据:
注意:使用myeclipse2014生成的整合项目可能存在问题需要我们自己导入。
第一步 我们写dao接口
package com.ssh.spring_hibernate.dao;
public interface BookShopDao { //根据书号获取数的单价 public int findBookPriceByIsbn(String isbn);
//更新书的库存,使书号对应的库存-1 public void updataBookStock(String isbn);
//更新用户的账户余额:使username的balance-price public void updateUserAccount(String username,int price); } |
写好其实现类
package com.ssh.spring_hibernate.dao;
import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @Repository public class BookShopDaoImpl implements BookShopDao { /** * 怎么用hibernate * 从SessionFactory中拿到跟当前线程绑定的Session */
@Autowired private SessionFactory sessionFactory;
public Session getSession(){ return sessionFactory.getCurrentSession(); } @Override public int findBookPriceByIsbn(String isbn) { String hql="select b.price from Book b where b.isbn=?"; Query q=getSession().createQuery(hql).setString(0, isbn); return (Integer) q.uniqueResult(); }
@Override public void updataBookStock(String isbn) { //验证书的库存是否足够 String hql2="select b.stock from Book b where b.isbn=?"; int stock=(Integer) getSession().createQuery(hql2).setString(0, isbn).uniqueResult(); if (stock==0) { System.out.println("库存不足!"); } String hql="update Book b set b.stock=b.stock-1 where b.isbn=?"; getSession().createQuery(hql).setString(0, isbn).executeUpdate(); }
@Override public void updateUserAccount(String username, int price) { //验证余额是否足够 String hql2="select a.balance from Account a where a.username=?"; int balance=(Integer) getSession().createQuery(hql2).setString(0, username).uniqueResult(); System.out.println(balance); if (balance<price) { System.out.println("余额不足"); } int result=balance-price; String hql="update Account a set a.balance=? where a.username=?"; getSession().createQuery(hql).setInteger(0, result).setString(1, username).executeUpdate(); System.out.println("余额为"+result); }
} |
注意:需要在spring的配置文件中添加自动扫描的路径
<!-- 设置自动扫描的包--> <context:component-scan base-package="com.ssh.spring_hibernate"></context:component-scan> |
第二步写好service
public interface BookShopService {
public void purchase(String username,String isbn);
}
public interface Cashier {
public void checkout(String username,List<String>isbn);
}
其实现类
package com.ssh.spring_hibernate.service;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
import com.ssh.spring_hibernate.dao.BookShopDao;
@Service public class BookShopServiceImpl implements BookShopService{ @Autowired private BookShopDao bookShopDao;
@Override public void purchase(String username, String isbn) { int price =bookShopDao.findBookPriceByIsbn(isbn); bookShopDao.updataBookStock(isbn); bookShopDao.updateUserAccount(username, price); }
} |
package com.ssh.spring_hibernate.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
@Service public class CashierImpl implements Cashier { @Autowired private BookShopService bookShopService;
@Override public void checkout(String username, List<String> isbn) { for (String is : isbn) { bookShopService.purchase(username, is); }
}
} |
第三步写我们的测试类
public class Go {
private ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); private BookShopService bookShopService=null; { bookShopService=ctx.getBean(BookShopService.class); }
public void testDataSource () throws SQLException{ DataSource d=ctx.getBean(DataSource.class); System.out.println(d.getConnection()); }
public void testBookShopService(){ bookShopService.purchase("aa", "1002"); } public static void main(String[] args) throws SQLException { new Go().testBookShopService(); } } |
控制台打印
Hibernate: select book0_.PRICE as col_0_0_ from SH_BOOK book0_ where book0_.ISBN=? Hibernate: select book0_.STOCK as col_0_0_ from SH_BOOK book0_ where book0_.ISBN=? Hibernate: update SH_BOOK set STOCK=STOCK-1 where ISBN=? Hibernate: select account0_.BALANCE as col_0_0_ from SH_ACCOUNT account0_ where account0_.USER_NAME=? 230 Hibernate: update SH_ACCOUNT set BALANCE=? where USER_NAME=? 余额为160 |
Spring Hibernate 事务的流程
- 在方法之前
- 获取Session
- 把Session和当前线程绑定,这样就可以在Dao中使用SessionFactory的getCurrentSession()方法来获取Session了
- 开启事务
- 若方法正常结束,即没有出现异常,则
- 提交事务
- 使和当前线程绑定的Session解除绑定
- 关闭Session
3若方法出现异常,则
① 回滚事务
- 使和当前线程绑定的Session解除绑定
- 关闭Session