Hibernate Query
环境
java 12.0.1
Apache Maven 3.6.3
MySQL Server version: 5.7.18-20170830-log 20170531
hibernate-core-5.4.27.Final.jar
mysql-connector-java-8.0.21.jar
IntelliJ IDEA 2020.2.3 (Ultimate Edition)
Hibernate映射文件
src/main/java/com/demo/hibernate/User.hbm.xml
<?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 package="com.demo.hibernate">
<class name="com.demo.hibernate.User" table="user" catalog="web">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="user" column="user" length="128"></property>
<property name="password" column="password" length="128"></property>
</class>
</hibernate-mapping>
Java代码
User类
src/main/java/com/demo/hibernate/User.java
package com.demo.hibernate;
public class User {
private int id;
private String user;
private String password;
public User(String user, String password) {
this.user = user;
this.password = password;
}
public User() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", user='" + user + '\'' +
", password='" + password + '\'' +
'}';
}
};
Hibernate类
src/main/java/com/demo/hibernate/Hibernate.java
package com.demo.hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;
import java.util.ArrayList;
import java.util.List;
public class Hibernate {
private Configuration configuration;
private StandardServiceRegistry standardServiceRegistry;
private SessionFactory sessionFactory;
protected Session session;
protected Transaction transaction;
public Hibernate() {
try {
configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
standardServiceRegistry = new StandardServiceRegistryBuilder().configure().build();
sessionFactory = configuration.buildSessionFactory(standardServiceRegistry);
session = sessionFactory.openSession();
transaction = session.beginTransaction();
} catch (Exception e) {
e.printStackTrace();
}
}
}
UserQueryHibernate类
src/main/java/com/demo/hibernate/UserQueryHibernate.java
package com.demo.hibernate;
import org.hibernate.query.Query;
import java.util.List;
public class UserQueryHibernate<T> extends Hibernate{
public List<T> query(String hql) {
Query query = session.createQuery(hql);
return query.list();
}
public static void main(String[] args) {
UserQueryHibernate<User> userQueryHibernate = new UserQueryHibernate<User>();
// 查询User中的所有记录
List<User> userList = null;
userList = userQueryHibernate.query("from User");
for (User user : userList) {
System.out.println(user);
}
// WHERE条件查询
userList = userQueryHibernate.query("from User U where U.user = 'Tony'");
for (User user : userList) {
System.out.println(user);
}
// SELECT选择指定的列
UserQueryHibernate<String> userNameQueryHibernate = new UserQueryHibernate<String>();
List<String> usernameList = userNameQueryHibernate.query("select U.user from User U");
for (String username : usernameList) {
System.out.println(username);
}
}
}
最后
- 由于博主水平有限,不免有疏漏之处,欢迎读者随时批评指正,以免造成不必要的误解!