20.笔记JAVA Spring框架学习————Spring整合Hibernate
在上篇配置Hibernate的基础上,加入spring,具体参看本系列第一篇。
在上篇基础上,然后加入Spring配置文件(右键,新增XML文件即可)
需要配置数据源,配置Hibernate的SessionFactory,配置Spring声明式事务等。
配置Spring配置文件
app.xml如下(包括数据源,sessionFactory,hibernate事务,):
Hibernate 配置是配置在Hibernate.cfg.xml 中的,但现在由于使用了 Spring 框架来管理 Hibernate,所以直接将 Hibernate 的配置写在 applicationContext.xml(Spring 配置文件)中。
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!--配置数据源 -->
<!--导入资源文,需要导入content命名空间-->
<context:property-placeholderlocation="db.properties"/>
<beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource">
<propertyname="driverClass"value="${jdbc.driverClass}"/>
<propertyname="jdbcUrl"value="${jdbc.jdbcUrl}"/>
<propertyname="user"value="${jdbc.user}"/>
<propertyname="password"value="${jdbc.password}"/>
<propertyname="initialPoolSize"value="${jdbc.initPoolSize}"/>
<propertyname="maxPoolSize"value="${jdbc.maxPoolSize}"/>
</bean>
<!--配置Session Factory,通过LocalSessionFactoryBean配置 -->
<beanid="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!--配置数据源属性-->
<propertyname="dataSource"ref="dataSource"/>
<!--配置hibernate配置文件的位置及名称 -->
<propertyname="configLocation"value="hibernate.cfg.xml"></property>
<!--配置hibernate映射文件的位置及名称,可以使用通配符 -->
<propertyname="mappingLocations"value="*.hbm.xml"></property>
</bean>
<!--配置spring的声明式事务 -->
<!-- 1.设定transactionManager事务管理器 -->
<beanid="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<propertyname="sessionFactory"ref="sessionFactory"/>
</bean>
<!-- 2.配置事务属性,需要导入tx命名空间-->
<tx:adviceid="txAdvice"transaction-manager="transactionManager">
<tx:attributes>
<tx:methodname="get*"read-only="true"/>
<tx:methodname="*"/>
</tx:attributes>
</tx:advice>
</beans>
其中:sessionFactory包括dataSource属性、 hibernateProperties属性和mappingResource属性。
dataSource 属性是给 sessionFactory 注入一个配置好的 dataSource bean。用于设置Hibernate 的数据库连接信息。hibernateProperties 是配置 Hibernate 参数的,可以配置数据库方言、show_sql、等参数。mappingResources 是用于配置 ORM 映射配置文件的路径的,路径可以使一个*.hbm.xml配置文件,也可以使具体的某一个使用了Annotation的Java实体类的路径。
配置数据db.properties资源文件
内容如下:
jdbc.user=root
jdbc.password=1230
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///test
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
实体类
对应于一个Java Web项目来说,比较规范的架构是三层架构,即视图层、业务逻辑层
和数据持久化层。而采用三层架构,每一层之间的数据交互就需要封装成实体类。具体的以
Struts2 Spring Hibernate框架整合开发的项目来说,他们之间的依赖关系如图1
dataSource、SessionFactory,dataSource对应于数据库的连接,SessionFactory 对应于 Hibernate 持久化操作,功能类似于HibernateSessionFactory 类。
而 DAO 对应的就是我们在框架基础上所写的具体对某一张表所做的增删改查方法,里面一般会有add、delete、update、findById、findAll等方法。Biz层对应于业务逻辑类,这里面一般会做事物控制、日志、数据封装、调用 DAO 层等。至于视图层的 Action 和 JSP 主要就是用于显示还有处理页面请求,并调用Biz代码。
这里还有Entity,这个就是实体类,也叫Entity Bean,用于封装数据,并在各层之间传
递数据。
持久化类
和上篇一样还是如下:
public class login {
private int id;
private Stringname;
private Stringpassword;
public int getId() {
returnid;
}
public void setId(intid) {
this.id =id;
}
public String getName() {
returnname;
}
public void setName(String name) {
this.name =name;
}
public String getPassword() {
returnpassword;
}
public void setPassword(String password) {
this.password =password;
}
}
JUnit 测试
import java.sql.SQLException;
import javax.sql.DataSource;
import org.junit.Test;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringHibernateTest {
privateApplicationContext ctx=null;
{
ctx= new ClassPathXmlApplicationContext("app.xml");
}
@Test
publicvoid testDataSource() throws SQLException {
DataSourcedataSource = ctx.getBean(DataSource.class);
System.out.println(dataSource.getConnection());
}
}
剩下的就是代码编写了。
在编辑Spring的配置文件时的自动提示
打开Eclipse—>Windows--->referenecs——>General,选择下面的Keys,这就是快捷键的设置,可将ContentAssist的快捷键改为 Alt+/