1.导入jar包
2.配置xml文件
<context:component-scan base-package="com.xxx.sh.*"></context:component-scan>
<!-- SpringHibernate整合 -->
<!-- 配置数据源DataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/ski_db?useUnicode=true&characterEncoding=UTF-8"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</bean>
<!-- 配置sessionFactory实例,在orm包中 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 配置Hibernate配置文件以及映射文件 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="mappingLocations" value="com/xxx/sh/entity/*.hbm.xml"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置通知 -->
<tx:advice id="advices" transaction-manager="transManager">
<tx:attributes>
<tx:method name="purchase" />
</tx:attributes>
</tx:advice>
<!-- 配置切点、配置切面 -->
<aop:config>
<aop:pointcut expression="execution(* com.xxx.sh.service.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="advices" pointcut-ref="pointcut"/>
</aop:config>
<!-- <aop:aspectj-autoproxy ></aop:aspectj-autoproxy> -->
</beans>
3.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--表明解析本XML文件的DTD文档位置,DTD是Document Type Definition 的缩写,即文档类型的定义,
XML解析器使用DTD文档来检查XML文件的合法性。hibernate.sourceforge.net/hibernate-configuration-3.0dtd可以在Hibernate3.1.3软件包中的src\org\hibernate目录中找到此文件-->
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!--Hibernate配置文件的根元素,其他文件要包含在其中-->
<hibernate-configuration>
<!--SessionFactory是Hibernate中的一个类,这个类主要负责保存Hibernate的配置信息,以及对session的操作-->
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.connection.characterEncoding">UTF-8</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</property>
</session-factory>
</hibernate-configuration>
4.类与表映射,属性与列映射,对象与记录映射
book.xml
<hibernate-mapping package="com.xxx.sh.entity">
<class name="Book" table="book" >
<id name="id" column="id" >
<generator class="identity">
</generator>
</id>
<property name="bookName" column="bookName" />
<property name="isbn" column="isbn"></property>
<property name="price" column="price"></property>
<property name="stock" column="stock"></property>
</class>
</hibernate-mapping>
Account.xml
<hibernate-mapping package="com.xxx.sh.entity">
<class name="Account" table="account" >
<id name="id" column="id" >
<generator class="identity">
</generator>
</id>
<property name="name" column="name" />
<property name="money" column="money"></property>
</class>
</hibernate-mapping>
5.dao层
public interface BookDao {
public double findBookPriceByIsbn(String isbn);
public void updateBookStock(String isbn);
public void updateMoney(String name,double price);
}
@Repository
public class BookDaoImpl implements BookDao{
@Autowired
private SessionFactory sessionFactory;
public Session getSession(){
return sessionFactory.getCurrentSession();//得到session对象
}
@Override
public double findBookPriceByIsbn(String isbn) {
String hql="select price from Book where isbn=?";
Query query = this.getSession().createQuery(hql);
query.setParameter(0, isbn);
double result =(double) query.uniqueResult();
// double price = (double) this.getSession().createQuery(hql).setParameter(0, isbn).uniqueResult();
return result;
}
@Override
public void updateBookStock(String isbn) {
//验证书的库存是否充足
String hql2="select stock from Book where isbn=?";
int stock=(int) this.getSession().createQuery(hql2).setParameter(0, isbn).uniqueResult();
if(stock==0){
System.out.println("书的库存不足");
}
String hql="update Book set stock=stock-1 where isbn=?";
this.getSession().createQuery(hql).setParameter(0, isbn).executeUpdate();
}
@Override
public void updateMoney(String name, double price) {
//验证用户余额是否充足
String hql2="select money from Account where name=?";
double money=(double) this.getSession().createQuery(hql2).setParameter(0, name).uniqueResult();
if(money<price && money>0){
//throw new Exception();
}
String hql="update Account set money=money-? where name=?";
this.getSession().createQuery(hql).setParameter(0, price).setParameter(1, name).executeUpdate();
}
}
6.service层
public interface BookService {
public void purchase(String name,String isbn);
}
@Service("bservice")
public class BookServiceImpl implements BookService{
@Autowired
private BookDao bd;
@Override
public void purchase(String name, String isbn) {// purchase方法处于事务管理,在beans.xml文件配置
double price=bd.findBookPriceByIsbn(isbn);
bd.updateBookStock(isbn);
bd.updateMoney(name, price);
}
}
7.测试类
public class TestSh {
public static void main(String[] args) {
test();
}
public static void test() {
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
BookService bs=(BookService) ac.getBean("bservice");
bs.purchase("小明", "002");
System.out.println("买书成功");
}
}