您的位置:首页 > 编程语言 > Java开发

Spring+Hibernate 简单例子

2015-04-18 20:34 453 查看
一、使用map文件:

有一个类,student需要存在数据库中:

public class Student {
private Integer age;
private String name;
private Integer id;

public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
return age;
}

public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}

public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
}


创建studentDAO(这里只定义了一个方法,当然,可以定义更多)

public interface StudentDAO {
public void insert(Student student);
}


实现studentDAO:

public class HibernateStudentDao implements StudentDAO{
private SessionFactory sessionFactory;

public HibernateStudentDao() {
}

public HibernateStudentDao(SessionFactory sessionFactory) {
this.setSessionFactory(sessionFactory);
}

//注入sessionFactory
public void setSessionFactory(
SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

public void insert(Student student) {
Session session = sessionFactory.openSession();
Transaction tx= session.beginTransaction();
session.save(student);
tx.commit();
session.close();
}
}


据库中建表:

create table student(ID INT NOT NULL PRIMARY KEY,

name VARCHAR(20),

age INT);

配置映射文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2015-4-18 19:42:24 by Hibernate Tools 3.4.0.CR1 -->

<hibernate-mapping>

<class name = "example.Student" table="student">
<id name="id" type="java.lang.Integer">
<column name="id"></column>
<generator class="increment"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20"></column>
</property>

<property name="age" type="java.lang.Integer">
<column name="age"></column>
</property>

</class>

</hibernate-mapping>


beans.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-3.0.xsd ">

<!-- Initialization for data source -->
<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/TEST"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>

<bean id="sessionFacotry" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<list>
<value>example/Student.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
</bean>

<bean id="hibernateStudentDao" class="example.HibernateStudentDao">
<constructor-arg name="sessionFactory" ref="sessionFacotry"></constructor-arg>
</bean>

</beans>


测试:

package example;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
//        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
HibernateStudentDao hibernateStudentDao=(HibernateStudentDao) applicationContext.getBean("hibernateStudentDao");
Student student=new Student();
student.setAge(222);
student.setName("shabi");
hibernateStudentDao.insert(student);

}

}


二、使用注解

当然,也可以使用注解。如果使用注解,需要在student类中添加相应的注解:

package example;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="student")
public class Student {
private Integer age;
private String name;
private Integer id;

public void setAge(Integer age) {
this.age = age;
}
@Column(name="age")
public Integer getAge() {
return age;
}
@Column(name="name")
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}

public void setId(Integer id) {
this.id = id;
}
@Id
@GeneratedValue
public Integer getId() {
return id;
}
}


同时,HibernateStudentDAO也需要增加相应注解:

package example;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository//(1)可以被<context: compenent-scan>扫描到,从而不必声明bean
//(2)异常相关。。。。
public class HibernateStudentDao implements StudentDAO{
public SessionFactory sessionFactory;

@Autowired //自动装配
public HibernateStudentDao(SessionFactory sessionFactory) {
this.sessionFactory=sessionFactory;
}

public void insert(Student student) {
Session session = sessionFactory.openSession();
Transaction tx= session.beginTransaction();
session.save(student);
tx.commit();
session.close();
}
}


当然,bean.xml也需要改变:

这里有个地方需要特别注意,在Hibernate3中,如果使用注解的方式定义持久化,需要使用org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean这个类创建bean,即使用该类代替org.springframework.orm.hibernate3.LocalSessionFactoryBean,但是在Hibernate4中,只需要使用org.springframework.orm.hibernate4.LocalSessionFactoryBean一个类即可。

<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

<context:component-scan base-package="example"/>

<!-- Initialization for data source -->
<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/TEST"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="packagesToScan" value="example"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
</bean>
</beans>


当然,map文件在这里就不需要了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: