Hibernate 初始配置

编程入门 行业动态 更新时间:2024-10-28 20:31:00

<a href=https://www.elefans.com/category/jswz/34/1768527.html style=Hibernate 初始配置"/>

Hibernate 初始配置

创建数据库

DROP DATABASE if exists hibernate;
create database hibernate charset=utf8mb4;
use hibernate;
create table user(id int auto_increment primary key,name varchar(10),gender varchar(10),age int,birthday date
)

Maven 导入

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=".0.0"xmlns:xsi=""xsi:schemaLocation=".0.0 .0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>hibernate</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-core</artifactId><version>5.4.31.Final</version></dependency></dependencies>
</project>

创建实体类

package cn.edu.hrbust.entity;import java.sql.Date;
import java.util.Objects;/*** @ClassName UserEntity* @Description* @Author Shen* @Date 2021/5/10 22:28* @Version 1.0**/
public class User {private int id;private String name;private String gender;private Integer age;private Date birthday;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 getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;User that = (User) o;return id == that.id && Objects.equals(name, that.name) && Objects.equals(gender, that.gender) && Objects.equals(age, that.age) && Objects.equals(birthday, that.birthday);}@Overridepublic int hashCode() {return Objects.hash(id, name, gender, age, birthday);}
}

创建主配置文件

注意数据库配置

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD//EN"".0.dtd">
<hibernate-configuration><session-factory><property name="connection.url">jdbc:mysql://localhost:3306/hibernate?serverTimezone=Asia/Shanghai&amp;characterEncoding=utf8</property><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.username">root</property><property name="connection.password">zs2201</property><mapping resource="User.hbm.xml"/><!-- DB schema will be updated if needed --><!-- <property name="hibernate.hbm2ddl.auto">update</property> --></session-factory>
</hibernate-configuration>

创建映射文件

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN"".0.dtd">
<hibernate-mapping><class name="cn.edu.hrbust.entity.User" table="user" schema="hibernate"><id name="id"><column name="id" sql-type="int(11)"/></id><property name="name"><column name="name" sql-type="varchar(10)" length="10" not-null="true"/></property><property name="gender"><column name="gender" sql-type="varchar(10)" length="10" not-null="true"/></property><property name="age"><column name="age" sql-type="int(11)" not-null="true"/></property><property name="birthday"><column name="birthday" sql-type="date" not-null="true"/></property></class>
</hibernate-mapping>

测试类

import cn.edu.hrbust.entity.User;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;import java.sql.Date;public class Main {public static void main(final String[] args) {User u = new User();u.setName("张三");u.setGender("男");u.setAge(21);u.setBirthday(Date.valueOf("2001-1-1"));Configuration cfg = null;SessionFactory sf = null;Session session = null;Transaction ts = null;try {cfg = new Configuration().configure();sf = cfg.buildSessionFactory();session = sf.openSession();ts = session.beginTransaction();session.save(u);ts.commit();} catch (HibernateException e) {e.printStackTrace();if (ts != null) {ts.rollback();}} finally {session.close();sf.close();}}
}

测试结果

期间遇到的问题

保存'张三'时数据库乱码,解决: 数据库URL添加characterEncoding=utf8参数,注意XML中 & 转义为&amp;

更多推荐

Hibernate 初始配置

本文发布于:2023-07-28 19:45:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1291468.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Hibernate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!