Spring+Hibernate 简单例子
2015-04-18 20:34
453 查看
一、使用map文件:
有一个类,student需要存在数据库中:
创建studentDAO(这里只定义了一个方法,当然,可以定义更多)
实现studentDAO:
据库中建表:
create table student(ID INT NOT NULL PRIMARY KEY,
name VARCHAR(20),
age INT);
配置映射文件:
beans.xml文件
测试:
二、使用注解
当然,也可以使用注解。如果使用注解,需要在student类中添加相应的注解:
同时,HibernateStudentDAO也需要增加相应注解:
当然,bean.xml也需要改变:
这里有个地方需要特别注意,在Hibernate3中,如果使用注解的方式定义持久化,需要使用org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean这个类创建bean,即使用该类代替org.springframework.orm.hibernate3.LocalSessionFactoryBean,但是在Hibernate4中,只需要使用org.springframework.orm.hibernate4.LocalSessionFactoryBean一个类即可。
当然,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文件在这里就不需要了
相关文章推荐
- spring中对hibernate的支持是非常强大的,从一个简单的例子就看得出来,从这个例子中我们还将对所谓的轻量级容器做一些讨论。
- 整合Struts2,Hibernate和Spring的一个简单例子
- Spring中配合hibernate使用的简单例子
- Eclipse + MyEclipse整合Struts+Spring+Hibernate简单例子开发
- Spring中配合hibernate使用的简单例子
- Spring+struts2+hibernate框架的Java Web项目利用POI导出Excel简单例子
- Spring中配合hibernate使用的简单例子
- spring+struts+hibernate简单例子(转的)
- gxt CURD ,数据库增删改查简单例子(Spring + Hibernate)
- Spring boot WebSocket stomp简单例子
- spring:quartz 简单例子 分类: spring 2015-01-09 14:23 245人阅读 评论(0) 收藏
- spring中对hibernate的支持例子
- 一个简单的Spring的AOP例子
- spring——控制反转简单例子
- java鬼混笔记:springboot之thymeleaf 1:简单的thymeleaf例子
- Java事务处理全解析(八)——分布式事务入门例子(Spring+JTA+Atomikos+Hibernate+JMS)
- springmvc+monggodb简单小例子
- Spring,Struts2,Hibernate简单搭建
- hibernate级联操 同时向2张表插入数据(一对多的关联,一个简单的例子说明)
- 一个简单的Spring的AOP例子