© 版权声明:本文为博主原创文章,转载请注明出处


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&amp;useUnicode=true&amp;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

最新文章

  1. Android自定义控件之自定义属性
  2. 想一想social VR might just work
  3. ios--时间格式化(cell业务逻辑处理)
  4. 【PSR规范专题(2)】PSR-1 基本代码规范
  5. C语言结构体和联合体
  6. WINCE下进程间通信(一)
  7. 使用SQL存储过程有什么好处 用视图有什么好处
  8. Java工程中使用Mybatis (工程结合Mybatis,数据可以结合Swing使用)
  9. MQTT——控制报文格式
  10. Linux 笔记 #03# 在 Debian远程服务器上运行 Java socket程序
  11. window.history.back(-1);与window.go(-1);的区别
  12. Django学习手册 - ORM数据类型
  13. iptables 分析(1)
  14. Perhaps you are running on a JRE rather than a JDK
  15. Java NIO -- 管道 (Pipe)
  16. 2018.9青岛网络预选赛(C)
  17. Unity触发器有时失效的原因
  18. phpmyadmin登录提示mysqli_real_connect(): (HY000/2002): No such file or directory和mysql8登录失败的问题
  19. Anaconda 简单介绍 -- 环境管理
  20. MySQl数据约束练习

热门文章

  1. MVC5的坑
  2. codeforces-505B
  3. 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛)F 猴子排序的期望【Java/高精度/组合数学+概率论】
  4. #420 Div2 D
  5. 洛谷—— P1680 奇怪的分组
  6. 网络防嗅探工具SniffJoke
  7. python查询mangodb
  8. Binary Tree Maximum Path Sum - LeetCode
  9. hdu 4826 Labyrinth DP
  10. Mac上Git的安装与简单使用