Hibernate Struts2 整合配置全解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LiuY521/article/details/79874606

Hibernate 配置全解

Hibernate 的意思是“冬眠”
在Java中的作用和它本来的意思没有太大的关联。

在MVC开发模式中,Struts是视图层的框架,
Hibernate是帮助我们更容易地和数据库打交道的,是属于模型层的框架。

ORMapping的概念
Object:程序对象
Relationship:数据库表(关系型数据库)RDBMS
Mapping:映射
ORMapping,像操作对象一样,来操作数据库

举例:
Student student=new Student();
student.name="Mike";
student.age="20";
session.save(student);

其他的ORMapping框架:TopLink,JDO

数据库其他框架:mybatis
================================================
讲课思路和环境搭建

Hibernate 2,3,4,版本差异比较大

学习方法介绍,项目中学习,学生选课管理系统

搭建struts2和hibernate开发环境

struts 2.3.20
hibernate 4.3.8 Final

hibernate官网
http://hibernate.org/
org = 组织 = orgnization
1.添加hibernate的jar包 lib/required
2.添加struts2的jar包     apps/解压一个项目blank/lib
3.添加mysql数据库连接驱动
4.添加struts2的配置文件struts.xml
5.添加hibernate的配置文件hibernate.cfg.xml


<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/curricula</property> 

<property name="connection.username">root</property> 

<property name="connection.password">12345678</property>  

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property> //自动创建数据库
 hbm2ddl.auto 是否自动创建数据库

6.编写初始化SessionFactory和获取Session的代码。
  SessionFactory:连接池(很多连接)  根据  Hibernate  获取数据库
  Session:连接

public class HibernateUtil {  //连接测试
private static SessionFactory sessionFactory;
private static SessionFactory buildSessionFactory() {
try {
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration
.buildSessionFactory(serviceRegistry);
return sessionFactory;
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
if (sessionFactory == null)
sessionFactory = buildSessionFactory();
return sessionFactory;
}
public static Session openSession() {
return getSessionFactory().openSession();
}
}

==========================================================================

HibernateUtil.java

package common;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {

		private static SessionFactory sessionFactory;

		private static SessionFactory buildSessionFactory() {
			try {
				Configuration configuration = new Configuration();
				configuration.configure("hibernate.cfg.xml");

				ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
						.applySettings(configuration.getProperties()).build();

				SessionFactory sessionFactory = configuration
						.buildSessionFactory(serviceRegistry);

				return sessionFactory;
			} catch (Throwable ex) {
				throw new ExceptionInInitializerError(ex);
			}
		}

		public static SessionFactory getSessionFactory() {
			if (sessionFactory == null)
				sessionFactory = buildSessionFactory();
			return sessionFactory;
		}

		public static Session openSession() {
			return getSessionFactory().openSession();
		}

}

Student.java

package model;

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

@Entity
public class Student {
	private int id;
	private String name;
	private String pwd;
	private String phone;
	private String grade;
	private String photo;
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getGrade() {
		return grade;
	}
	public void setGrade(String grade) {
		this.grade = grade;
	}
	public String getPhoto() {
		return photo;
	}
	public void setPhoto(String photo) {
		this.photo = photo;
	}
	
}

Student.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="model">
<class name="Student" table="student">
        <id name="id" column="id">
            <generator class="native"/>
        </id>
        <property name="name"></property>
        <property name="pwd" column="pwd" type="string" length="50"></property>
        <property name="phone" length="50"></property>
        <property name="photo" length="150"></property>
    </class>
</hibernate-mapping>

hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory>
			<!-- Database connection settings -->
		       <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="connection.url">jdbc:mysql://localhost:3306/curricula</property> 
				
		<property name="connection.username">root</property> 
				
		<property name="connection.password">12345678</property>  
				
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 		
		<property name="show_sql">true</property>
 
        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>
		<mapping class="model.Student"/>
	</session-factory>
</hibernate-configuration>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <constant name="struts.devMode" value="true" />                                  
</struts>

==========================================================================





猜你喜欢

转载自blog.csdn.net/LiuY521/article/details/79874606
今日推荐