学习Spring第六天
hibernate框架使用
首先引入gradle依赖:
testCompile group: 'junit', name: 'junit', version: '4.12'
// https://mvnrepository.com/artifact/org.hibernate/hibernate-core
compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.12.Final'
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.8-dmr'
然后创建一个实体类 Employee:
package com.demo.Model;
public class Employee {
private int id;
private String firstName;
private String lastName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
然后创建持久化类 employee.hbm.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.demo.Model.Employee" table="employee">
<id name="id" column="ID">
<generator class="assigned"/>
</id>
<property name="firstName" column="FIRST_NAME"/>
<property name="lastName" column="LAST_NAME" />
</class>
</hibernate-mapping>
创建hibernate配置文件 hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">19970819wy</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping resource="hibernate/employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
编写测试类 StoreData
package com.demo;
import com.demo.Model.Employee;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class StoreData {
public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure("hibernate/hibernate.cfg.xml");
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
Employee employee = new Employee();
employee.setId(1);
employee.setFirstName("Wang");
employee.setLastName("Yu");
session.persist(employee);
transaction.commit();
session.clear();
System.out.println("successful saved");
}
}
运行结果如下:
使用注解的形式来创建持久化实体类
我们首先用注解的形式来修改上面的Employee实体类
package com.demo.Model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "tb_employee")
public class Employee {
@Id
@Column(name = "Em_ID")
private int id;
@Column(name = "Em_FirstName")
private String firstName;
@Column(name = "Em_LastName")
private String lastName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
@Id表示主键
@Column表示对应数据表的列名称
@Entity是必须的,把Employee注册成实体类
@Table指定对应的数据表的名称
在hibernate.cfg.xml中修改一下mapping
<mapping class="com.demo.Model.Employee"/>
运行结果一致
坚持原创技术分享,您的支持将鼓励我继续创作!