基于Hibernate对数据库表的单表查询
1.依赖
1.1jar包
1.2配置文件。persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<!-- 配置持久化单元-->
<persistence-unit name="PZMYSQL" transaction-type="RESOURCE_LOCAL">
<!-- 指定实体-->
<class>entity.User</class>
<class>entity.Person</class>
<properties>
<!-- 标准配置方法,适用性高 -->
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/xxx?useSSL=false&serverTimezone=GMT%2B8"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<!-- hibernate 的配置方法-->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update"/> <!--create,create-drop,update,validate -->
</properties>
</persistence-unit>
</persistence>
1.3目录结构
2.增删改查代码
2.1实体类
- Entity注解,指定实体名称
- NamedQuery,命名的查询语句
- Table注解是映射的数据库表
- Id是主键,GeneratedValue是主键生成方式
- 属性私有,有get和set以及无参构造等
- Transient,表示注解对应的属性不会持久化到数据库中
package entity;
import javax.persistence.*;
import java.io.Serializable;
@Entity(name = "User")
@NamedQuery(
name="queryAll",
query = "select user from User user"
)
@Table(name = "user") //映射的表名称
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
// @Transient //不会持久化,不会保存到数据库中
private String name;
// ===============构造和get set方法====================
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
public User(Integer id, String name) {
this.id = id;
this.name = name;
}
public User() {
}
}
2.2Person类
package entity;
import javax.persistence.*;
import java.io.Serializable;
@Entity(name = "Person")
@Table(name = "person") //映射的表名称
public class Person implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Transient
private String name;
// ===============构造和get set方法====================
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
public Person(Integer id, String name) {
this.id = id;
this.name = name;
}
public Person() {
}
}
2.3persist添加数据
public static void main(String[] args) {
//创建工厂
EntityManagerFactory entityManagerFactory= Persistence.createEntityManagerFactory("PZMYSQL");
//创建管理者对象
EntityManager manager=entityManagerFactory.createEntityManager();
//获取事务对象
EntityTransaction transaction=manager.getTransaction();
transaction.begin();
//增删改的方法
User user=new User();
user.setName("陈二");
//保存数据
manager.persist(user);
//提交事务
transaction.commit();
//关闭资源
manager.close();
entityManagerFactory.close();
}
2.4remove删除数据
先find再去remove
public static void main(String[] args) {
//创建工厂
EntityManagerFactory entityManagerFactory= Persistence.createEntityManagerFactory("PZMYSQL");
//创建管理者对象
EntityManager manager=entityManagerFactory.createEntityManager();
//获取事务对象
EntityTransaction transaction=manager.getTransaction();
transaction.begin();
//增删改的方法
User user=new User();
user.setId(1);
//删除数据
user=manager.find(User.class,1);
manager.remove(user);
//提交事务
transaction.commit();
//关闭资源
manager.close();
entityManagerFactory.close();
}
2.5merge修改
先find再去merge
public static void main(String[] args) {
//创建工厂
EntityManagerFactory entityManagerFactory= Persistence.createEntityManagerFactory("PZMYSQL");
//创建管理者对象
EntityManager manager=entityManagerFactory.createEntityManager();
//获取事务对象
EntityTransaction transaction=manager.getTransaction();
transaction.begin();
//增删改的方法
User user=new User();
//删除数据
user=manager.find(User.class,2);
user.setName("王五");
manager.merge(user);
//提交事务
transaction.commit();
//关闭资源
manager.close();
entityManagerFactory.close();
}
2.6createQuery基于原生的查询
查询全部基于createQuery(sql);基于JPQL 基于对象的查询
public static void main(String[] args) {
//创建工厂
EntityManagerFactory entityManagerFactory= Persistence.createEntityManagerFactory("PZMYSQL");
//创建管理者对象
EntityManager manager=entityManagerFactory.createEntityManager();
//获取事务对象
EntityTransaction transaction=manager.getTransaction();
transaction.begin();
String sql="select user from User user";
Query query= manager.createQuery(sql);
List<User> list=query.getResultList();
System.out.println(list.toString());
transaction.commit();
//关闭资源
manager.close();
entityManagerFactory.close();
}
2.7createNativeQuery原生查询
createNativeQuery基于原生的sql查询,需要指定查询的结果的类型
public static void main(String[] args) {
//创建工厂
EntityManagerFactory entityManagerFactory= Persistence.createEntityManagerFactory("PZMYSQL");
//创建管理者对象
EntityManager manager=entityManagerFactory.createEntityManager();
//获取事务对象
EntityTransaction transaction=manager.getTransaction();
transaction.begin();
String sql="select * from user";
Query query= manager.createNativeQuery(sql,User.class);
List<User> list=query.getResultList();
System.out.println(list.toString());
transaction.commit();
//关闭资源
manager.close();
entityManagerFactory.close();
}
2.8createNamedQuery命名查询
基于createNamedQuery的查询,User中写的也是JPQL语句
public static void main(String[] args) {
//创建工厂
EntityManagerFactory entityManagerFactory= Persistence.createEntityManagerFactory("PZMYSQL");
//创建管理者对象
EntityManager manager=entityManagerFactory.createEntityManager();
//获取事务对象
EntityTransaction transaction=manager.getTransaction();
transaction.begin();
Query query= manager.createNamedQuery("queryAll",User.class);
List<User> list=query.getResultList();
System.out.println(list.toString());
transaction.commit();
//关闭资源
manager.close();
entityManagerFactory.close();
}
2.9基于原生插入
add 基于原生插入createNativeQuery,executeUpdate,需要指定返回的结果的类型
参考:https://betheme.net/houduan/1480.html?action=onClick
public static void main(String[] args) {
//创建工厂
EntityManagerFactory entityManagerFactory= Persistence.createEntityManagerFactory("PZMYSQL");
//创建管理者对象
EntityManager manager=entityManagerFactory.createEntityManager();
//获取事务对象
EntityTransaction transaction=manager.getTransaction();
transaction.begin();
Integer x= manager.createNativeQuery("insert into user(name) values ('张三')",Integer.class).executeUpdate();
System.out.println(x);
transaction.commit();
//关闭资源
manager.close();
entityManagerFactory.close();
}
2.10不设置注解自增的插入
自己设置主键的值的话需要注释掉 @GeneratedValue(strategy = GenerationType.AUTO)
并在插入数据的时候指定主键
public static void main(String[] args) {
//创建工厂
EntityManagerFactory entityManagerFactory= Persistence.createEntityManagerFactory("PZMYSQL");
//创建管理者对象
EntityManager manager=entityManagerFactory.createEntityManager();
//获取事务对象
EntityTransaction transaction=manager.getTransaction();
transaction.begin();
User user=new User();
user.setId(10);
user.setName("赵六");
manager.persist(user);
transaction.commit();
//关闭资源
manager.close();
entityManagerFactory.close();
}