Hibernate和Spring的都是轻量级的框架,现在用的也比较多,既可以开发web应用,也可以开发独立的应用,他们都非官方框架,但对EJB都有重要的影响,下面,举个简单的列子(班级学生为例),将他们俩进行整合
环境:myeclipse
架包版本:Hibernate(3.3)+Spring(3.0)
班级类(Clazz)
@Entity
@Table(name = "class100", catalog = "test100")
public class Clazz implements java.io.Serializable {
// Fields
private Integer id;
private String name;
private Set<Student> students = new HashSet<Student>(0);
// Constructors
/** default constructor */
public Clazz() {
}
/** full constructor */
public Clazz(String name, Set<Student> students) {
this.name = name;
this.students = students;
}
// Property accessors
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name", length = 20)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "clazz")
public Set<Student> getStudents() {
return this.students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
}
学生类(Student)
@Entity
@Table(name = "student100", catalog = "test100")
public class Student implements java.io.Serializable {
// Fields
private Integer sno;
private Clazz clazz;
private String name;
// Constructors
/** default constructor */
public Student() {
}
/** full constructor */
public Student(Clazz clazz, String name) {
this.clazz = clazz;
this.name = name;
}
// Property accessors
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "sno", unique = true, nullable = false)
public Integer getSno() {
return this.sno;
}
public void setSno(Integer sno) {
this.sno = sno;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "class_id")
public Clazz getClazz() {
return this.clazz;
}
public void setClazz(Clazz clazz) {
this.clazz = clazz;
}
@Column(name = "name", length = 20)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
如果用的myeclipse的反向工程生成的类,则会产生一个hibernate.cfg.xml文件,这个可以可以和Spring的配置文件applicationContext.xml混合使用,也可以将其配置在Spring中
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- 一、Hibernate中采用配置文件的方式,class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" -->
<!-- 二、Hibernate中采用注解的方式,class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" -->
<!--
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation"
value="file:src/hibernate.cfg.xml">
</property>
</bean>
-->
<!-- 上面采用是利用原有的Hibernate配置文件,但是在插入数据时候,显示了sql语句,没有报错,但数据库中没有记录 -->
<!-- 下面在Spring的配置文件中配置数据库的相关信息,没有问题 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test100?useunicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="niit"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="show_sql">true</prop>
<prop key="format_sql">true</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.sunny.entity</value>
</list>
</property>
</bean>
<bean id="dao" class="com.sunny.service.impl.DaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
以上配置将Hibernate配置在Spring中,Hibernate配置文件可以删除,笔者也试过利用Hibernate原有的配置文件,但在插入的时候插入没有成功,显示的sql语句,原因不明,所以就采用了在Spring中配置Hibernate的方式。
运行代码:
简单起见,笔者没有用接口,但推荐大家采用接口进行编程,一下的类为Dao类
public class DaoImpl extends HibernateDaoSupport{
public Serializable add(Object o){
Serializable ret = this.getHibernateTemplate().save(o);
return ret;
}
public Object get(Class clazz, Serializable id) {
Object ret = this.getHibernateTemplate().get(clazz, id);
return ret;
}
}
测试用例
public class Demo{
public static void main(String[] args) {
fun1();
}
public static void fun2(){
ApplicationContext apptc = new ClassPathXmlApplicationContext("applicationContext.xml");
DaoImpl dao = (DaoImpl)apptc.getBean("dao");
Student stu = (Student)dao.get(Student.class,21);
System.out.println(stu.getName());
}
/*
* Hibernate+Spring的方式对数据库进行操作,session的打开关闭全部交给Spring来维护
*/
public static void fun1(){
ApplicationContext apptc = new ClassPathXmlApplicationContext("applicationContext.xml");
DaoImpl dao = (DaoImpl)apptc.getBean("dao");
Clazz c = new Clazz();
c.setName("三年二班");
Student s1 = new Student();
s1.setClazz(c);
s1.setName("JBoss");
Student s2 = new Student();
s2.setName("Tomcat");
s2.setClazz(c);
Set< Student> stus = new HashSet<Student>();
stus.add(s1);
stus.add(s2);
c.setStudents(stus);
int i = (Integer)dao.add(c);
System.out.println(i);
}
}