Hibernate简单查询实现流程
1.配置环境
首先,导入依赖包,并且写好Hibernate配置文件hibernate.hbm.xml,以及对象映射配置文件User.hbm.xml。
hibernate.hbm.xml:
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!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="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</property>
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?serverTimezone=Asia/Shanghai</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">643383shallwe</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="User.hbm.xml"/>
<mapping resource="Group.hbm.xml"/>
<mapping resource="IdCard.hbm.xml"/>
</session-factory>
</hibernate-configuration>
<!-- #hibernate.dialect org.hibernate.dialect.MySQLDialect-->
<!-- #hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect-->
<!-- #hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect-->
<!-- #hibernate.connection.driver_class com.mysql.jdbc.Driver-->
<!-- #hibernate.connection.url jdbc:mysql:///test-->
<!-- #hibernate.connection.username gavin-->
<!-- #hibernate.connection.password-->
User.hbm.xml:
<?xml version="1.0"?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!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.sw.bean.User" table="t_user" select-before-update="true">
<id name="id" type="int" column="id">
<generator class="identity"/>
</id>
<property name="username" length="10"/>
<property name="password" length="10"/>
<property name="sex"/>
<property name="age"/>
</class>
</hibernate-mapping>
2.编写测试代码
这里使用Junit进行测试,编写mainTest方法,并在其中使用Hibernate实现简单查询。查询可以按照以下步骤进行:
(1)调用Configuration类的configure方法,使配置文件生效。
Configuration configure = new Configuration().configure();
(2)调用Configuration的buildSessionFactory方法,来创建一个session工厂。
SessionFactory sessionFactory = configure.buildSessionFactory();
(3)通过session工厂来获取一个session。session即可以对数据库进行CURD操作。
Session session = sessionFactory.openSession();
(4)开始事务
Transaction transaction = session.beginTransaction();
(5)使用session对数据库中user进行查询。这里使用HQL方式进行查询。
Query query = session.createQuery("from User");
(6)打印结果。结果出Query中的结果。调用Query的list()方法得到list,然后遍历list进行打印。
List<User> list = query.list();
list.forEach(System.out::println);
(7)提交事务。
transaction.commit();
(7)结果如下。可以看到数据库中的user数据被全部输出了。
代码
User.java
package com.sw.bean;
import java.io.Serializable;
/**
* Created by shaowei on 2021/5/10.
*/
public class User implements Serializable {
private Integer id;
private String username;
private String password;
private String sex;
private Integer age;
private Group group;
private IdCard idCard;
public User(){}
public User(String o1, String o2){
this.username = (String)o1;
this.password = (String)o2;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", group=" + group +
", idCard=" + idCard +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
public IdCard getIdCard() {
return idCard;
}
public void setIdCard(IdCard idCard) {
this.idCard = idCard;
}
}
MainTest.java
import com.sw.bean.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import org.junit.Test;
import java.util.List;
/**
* Created by shaowei on 2021/6/10.
*/
public class MainTest {
@Test
public void mainTest(){
try{
Configuration configure = new Configuration().configure();
SessionFactory sessionFactory = configure.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Query query = session.createQuery("from User");
List<User> list = query.list();
list.forEach(System.out::println);
transaction.commit();
}catch (Exception e){
e.printStackTrace();
}
}
}