Hibernate Struts2 整合配置全解
2018-04-09 23:44
344 查看
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.javapackage 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 {
Co
97d2
nfiguration 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.javapackage 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>
==========================================================================
相关文章推荐
- struts2,hibernate,spring三大框架整合的注册程序(xml 配置,mysql数据库)
- Struts2 Hibernate Spring 整合的基本流程和步骤及其相关配置细节
- spring+hibernate3+struts2整合完整配置
- spring+hibernate3+struts2整合完整配置
- struts2、Spring、Hibernate的整合-配置文件 2017年01月05日 17:23:10 118人阅读 评论
- Spring整合Struts2和Hibernate+Maven(二)之SSH的配置文件
- Spring3.0、Hibernate3.0、Struts2整合文件配置
- Spring3.x_Struts2.x_Hibernate3.x整合之声明式事务配置
- spring整合struts2(hibernate的配置和spring配置文件综合在一起)
- Struts2 + Spring + Hibernate 整合 web.xml的一般配置
- 学习三大框架Struts2 、 Hibernate 、 Spring时,涉及到三大框架的配置文件以及整合。
- Spring_hibernate_struts整合(集成配置)
- Struts2和Hibernate整合
- struts2和spring3零配置整合的思考
- Spring3.3 整合 Hibernate3、MyBatis3.2 配置多数据源/动态切换数据源 方法
- 基于maven 管理下Spring,Struts2,hibernate的pom配置
- Spring托管Struts2和Hibernate的配置文件
- Struts2,Spring3,Hibernate4整合--SSH框架
- ssh整合思想 Spring与Hibernate的整合ssh整合相关JAR包下载 .MySQLDialect添加了第57区的方言解决没有异常,hibernate.dialect核心配置文件就是无法自动update创建表
- Struts,hibernate,Spring整合开发各层之间联系的配置说明(初学者值得看下)