? 版权声明:本文为博主原创文章,转载请注明出处
[TOC]
1. 项目结构
- 本项目使用maven进行项目管理,搭建一个简单的Hibernate框架需要四步。
- 本项目未使用注解方式进行搭建
1.1 pom.xml
- 使用maven引入依赖jar包,若未使用maven,则自己下载所需jar包放到lib目录下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.imooc</groupId>
<artifactId>Hibernate_001</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.1.5.Final</version>
</dependency>
<!-- mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.22</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<target>1.7</target>
<source>1.7</source>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
1.2 hibernate.cfg.xml
- 在hibernate.cfg.xml中配置三方面的信息。
- 连接数据库的信息,比如驱动、url、连接名、密码等。
- hibernate本身的配置,比如方言、是否展示SQL,SQL格式化、建表策略等。
- 引入映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!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>
<!-- 数据库连接名 -->
<property name="connection.username">root</property>
<!-- 数据库连接密码 -->
<property name="connection.password">20121221</property>
<!-- 数据库驱动 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 数据库连接url -->
<property name="connection.url">
jdbc:mysql://localhost:3306/hibernate?useSSL=true&useUnicode=true&characterEncoding=UTF-8
</property>
<!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 展示SQL -->
<property name="show_sql">true</property>
<!-- 格式化SQL -->
<property name="format_sql">false</property>
<!-- 建表策略 -->
<property name="hbm2ddl.auto">update</property>
<!-- 指定映射文件 -->
<mapping resource="hbm/student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
1.3 entity
- entity是实体类,对应数据库中的表结构,仅实现setter和getter方法即可
package com.imooc.hibernate.model;
import java.util.Date;
public class Student {
private int sid; // 学号
private String sname; // 姓名
private String gender; // 性别
private Date birthday; // 出生日期
private String address; // 地址
public Student() {
}
public Student(int sid, String sname, String gender, Date birthday, String address) {
this.sid = sid;
this.sname = sname;
this.gender = gender;
this.birthday = birthday;
this.address = address;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", gender="
+ gender + ", birthday=" + birthday + ", address=" + address + "]";
}
}
1.4 entity.hbm.xml
- entity.hbm.xml是映射文件,将数据库表与entity进行对应
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.imooc.hibernate.model.Student">
<id name="sid" type="int">
<column name="SID"/>
<generator class="assigned"/> <!-- 主键生成策略 -->
</id>
<property name="sname" type="java.lang.String">
<column name="SNAME"/>
</property>
<property name="gender" type="java.lang.String">
<column name="GENDER"></column>
</property>
<property name="birthday" type="java.util.Date">
<column name="BIRTHDAY"/>
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS"></column>
</property>
</class>
</hibernate-mapping>
2. 测试
- 使用junit进行测试
package com.imooc.hibernate.test;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.imooc.hibernate.model.Student;
public class StudentTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Test
public void testSaveStudent() {
// 生成学生对象
Student s = new Student(3, "张三丰", "男", new Date(), "武当山");
session.save(s);
}
@Before
public void init() {
// 创建会话工厂对象
sessionFactory = new Configuration().configure().buildSessionFactory();
// 创建会话对象
session = sessionFactory.openSession();
// 开始事务
transaction = session.beginTransaction();
}
@After
public void destory () {
// 提交事务
transaction.commit();
// 关闭会话
session.close();
// 关闭会话工厂
sessionFactory.close();
}
}
3. 总结
Hibernate初步搭建很简单,主要是使用hibernate.cfg.xml进行数据库连接的配置,使用entity.hbm.xml进行数据库表结构与实体类进行映射。
参考:Hibernate初探之单表映射
更好的markdown体验:https://www.zybuluo.com/chy282/note/970569
时间: 2024-10-11 21:29:51