SSH程序框架的整合
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSH程序框架的整合相关的知识,希望对你有一定的参考价值。
spring整合hibernate 有两种方式 1、注解方式 2、xml方式实现
Spring整合Hibernate有什么好处?
1、由IOC容器来管理Hibernate的SessionFactory
2、让Hibernate使用上Spring的声明式事务
xml:
那什么我们先创建一个账户,再把jar包导一下:
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.3</version> <scope>test</scope> </dependency> <!--jta的jar包--> <dependency> <groupId>javax.transaction</groupId> <artifactId>jta</artifactId> <version>1.1</version> </dependency> <!--mysql数据库驱动--> <dependency> <groupId>org.wisdom-framework</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34_1</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.0.6.Final</version> </dependency> <!--c3p0--> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!--AOP的相关jar--> <dependency> <groupId> org.aspectj</groupId > <artifactId> aspectjweaver</artifactId > <version> 1.8.7</version > </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.0.RELEASE</version> </dependency> <!--Spring-ORM--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version> 4.2.2.RELEASE</version> </dependency> </dependencies>
Stock:
public class Stock { private Integer sid; private String sname; private Integer scount; public Stock(String sname, Integer scount) { this.sname = sname; this.scount = scount; } public Integer getSid() { return sid; } public void setSid(Integer sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public Integer getScount() { return scount; } public void setScount(Integer scount) { this.scount = scount; } }
StockDao:
public interface StockDao { public int addStock(Stock stock); }
StockDaoImpl:
public class StockDaoImpl implements StockDao { private SessionFactory sessionFactory; public int addStock(Stock stock) { Session session = sessionFactory.getCurrentSession(); Serializable count = session.save(stock); return (Integer)count; } public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } }
AccountService:
public interface AccountService { public int addStock(Stock stock); }
service:
public class AccountServiceImpl implements AccountService { private StockDao stockDao; @Transactional public int addStock(Stock stock) { return stockDao.addStock(stock); } public StockDao getStockDao() { return stockDao; } public void setStockDao(StockDao stockDao) { this.stockDao = stockDao; } }
applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!--1.配置数据源c3p0-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
</bean>
<!--jdbc.properties-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--2.SessionFactory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
</props>
</property>
<property name="mappingDirectoryLocations" value="classpath:cn/happy/beans"></property>
</bean>
<!--3.dao-->
<bean id="stockDAO" class="cn.happy.dao.impl.StockDAOImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--4.service-->
<bean id="stockService" class="cn.happy.service.impl.AccountServiceImpl">
<property name="stockDAO" ref="stockDAO"></property>
</bean>
<!--5.事务管理器-->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--6.事务-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>
jdbc.properties:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\\:mysql\\:///bookshop
jdbc.username=root
jdbc.password=1234
Stock.hbm.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.beans">
<class name="Stock" table="Stock">
<id name="sid" column="sid">
<generator class="native"/>
</id>
<property name="sname"/>
<property name="scount" column="count"/>
</class>
</hibernate-mapping>
测试:
public class SSHTest {
@Test
public void tsstAddstock(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
IAccountService service=(IAccountService)ctx.getBean("stockService");
Stock stock=new Stock("昨天的故事",10);
service.addStock(stock);
}
}
好了,如果你是这样做的,那么可以看看数据库你想要的了!!!!
以上是关于SSH程序框架的整合的主要内容,如果未能解决你的问题,请参考以下文章