Creating a database
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
)
Copy the code
I sorted out some information, and friends in need can click to get it directly.
Java Basics collection
One hundred Core Java architect books
Standard Ali P7 Java architect learning roadmap
The latest interview questions in 2021
Maven import
<? The XML version = "1.0" encoding = "utf-8"? > < 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" > The < modelVersion > 4.0.0 < / modelVersion > < groupId > org. Example < / groupId > < artifactId > hibernate < / artifactId > < version > 1.0 - the SNAPSHOT < / version > < properties > < maven.com piler. Source > 8 < / maven.com piler 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 > < the dependency > < the groupId > org. Hibernate < / groupId > < artifactId > hibernate - core < / artifactId > < version > 5.4.31. The Final < / version > < / dependency > </dependencies> </project>Copy the code
Creating an entity Class
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; } @Override public 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); } @Override public int hashCode() { return Objects.hash(id, name, gender, age, birthday); }}Copy the code
Create a master profile
Paying Attention to database Configuration
<? The XML version = '1.0' encoding = "utf-8"? > <! DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" > < hibernate configuration - > < session - factory > < property name="connection.url">jdbc:mysql://localhost:3306/hibernate?serverTimezone=Asia/Shanghai& 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>Copy the code
Creating a Mapping File
<? The 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 = "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>Copy the code
The test class
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.s etName (" * * "); U.s etGender (" male "); 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(); }}}Copy the code
The test results
Problems encountered
Add the characterEncoding= UTf8 parameter to the database URL. Note that the & in the XML is “&”
The last
Everybody see here to give a thumbs up, thank you!