当前位置: 首页 > news >正文

南京企业网站设计建设/淘宝的17种免费推广方法

南京企业网站设计建设,淘宝的17种免费推广方法,IT男做网站,取名字网站如何做文章目录 一、如何使用二、具体操作1、创建 Maven 工程,pom.xml2、hibernate.cfg.xml3、创建实体类4、创建实体关系映射文件5、实体关系映射文件注册到 Hibernate 的配置文件中。6、使用 Hibernate API 完成数据操作。7、pom.xml 中需要配置 resource 三、Hibernate…

文章目录

  • 一、如何使用
  • 二、具体操作
    • 1、创建 Maven 工程,pom.xml
    • 2、hibernate.cfg.xml
    • 3、创建实体类
    • 4、创建实体关系映射文件
    • 5、实体关系映射文件注册到 Hibernate 的配置文件中。
    • 6、使用 Hibernate API 完成数据操作。
    • 7、pom.xml 中需要配置 resource
  • 三、Hibernate 级联操作
    • 1、一对多关系
    • 2、多对多关系
  • 四、Hibernate 实现一对多
  • 五、Hibernate 实现多对多
  • 六、Hibernate 延迟加载
    • 1、一对多
    • 2、多对多
  • 七、Hibernate 配置文件
    • 1、Hibernate.cfg.xml
      • <1>、数据库的基本信息。
      • <2>、集成 C3P0,设置数据库连接池信息。
      • <3>、Hibernate 基本信息
      • <4>、注册实体关系映射文件
    • 2、hbm.xml
      • <1>、hibernate-mapping 属性
      • <2>、class 属性
      • <3>、id 属性
      • <4>、property 属性
      • <5>、实体关系映射文件属性
        • 1、inverse
        • 2、cascade
  • 八、Hibernate HQL
    • 1、查询对象
    • 2、分页查询
    • 3、where 条件查询
    • 4、模糊查询
    • 5、order by
    • 6、查询实体对象的属性
    • 7、占位符
    • 8、级联查询

主流 ORM 框架 Object Relation Mapping 对象关系映射,将面向对象映射成面向关系。

一、如何使用

1、导入相关依赖
2、创建 Hibernate 配置文件
3、创建实体类
4、创建实体类-关系映射文件
5、调用 Hibernate API 完成操作

二、具体操作

1、创建 Maven 工程,pom.xml

<dependencies><!--简化实体类的开发--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.10</version></dependency><!--java连接mysql依赖--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.16</version></dependency><!--Hibernate依赖--><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-core</artifactId><version>5.4.10.Final</version></dependency>
</dependencies>

2、hibernate.cfg.xml

核心配置:session-factory

SessionFactory:针对单个数据库映射经过编译的内存镜像文件,将数据库转换为⼀个 Java 可以识别的镜像文件。

构建 SessionFactory 非常耗费资源,所以通常⼀个工程只需要创建⼀个 SessionFactory。

<?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.username">root</property><property name="connection.password">password</property><property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property><property name="connection.url">jdbc:mysql://localhost:3306/hibernate?serverTimezone=Asia/Shanghai&amp;useUnicode=true&amp;characterEncoding=UTF-8</property><!--C3P0连接池--><property name="hibernate.c3p0.acquire_increment">10</property><property name="hibernate.c3p0.idle_test_period">10000</property><property name="hibernate.c3p0.timeout">5000</property><property name="hibernate.c3p0.max_size">30</property><property name="hibernate.c3p0.min_size">5</property><property name="hibernate.c3p0.max_statements">10</property><!--是否输出底层SQL语句--><property name="hibernate.show_sql">true</property><!--输出底层SQL语句是否格式化--><property name="hibernate.format_sql">true</property><!--是否自动生成数据库--><!--update:如果已经有表,更新;如果没有,创建。(一般用这个)--><property name="hibernate.hbm2ddl.auto">update</property><!--数据库方言--><!--如果使用的mysql是5.x,写:org.hibernate.dialect.MySQL5Dialect。--><!--如果使用的mysql是8.x,写:org.hibernate.dialect.MySQL8Dialect。--><property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property><!--注册实体关系映射文件--><mapping resource="com/htl/entity/People.hbm.xml"/><mapping resource="com/htl/entity/Customer.hbm.xml"/><mapping resource="com/htl/entity/Orders.hbm.xml"/><mapping resource="com/htl/entity/Account.hbm.xml"/><mapping resource="com/htl/entity/Course.hbm.xml"/></session-factory></hibernate-configuration>

3、创建实体类

package com.htl.entity;import lombok.Getter;
import lombok.Setter;import java.util.Set;@Getter
@Setter
public class Customer {private Integer id;private String name;private Set<Orders> orders;@Overridepublic String toString() {return "Customer{" +"id=" + id +", name='" + name + '\'' +'}';}
}
package com.htl.entity;import lombok.Getter;
import lombok.Setter;@Getter
@Setter
public class Orders {private Integer id;private String name;private Customer customer;@Overridepublic String toString() {return "Orders{" +"id=" + id +", name='" + name + '\'' +'}';}
}

4、创建实体关系映射文件

package com.htl.entity;import lombok.Data;@Data
public class People {private Integer id;private String name;private Double money;
}

在这里插入图片描述

People.hbm.xml

<?xml version="1.0"?><!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.htl.entity.People" table="people"><id name="id" type="java.lang.Integer"><column name="id"></column><generator class="identity"></generator></id><property name="name" type="java.lang.String"><column name="name"></column></property><property name="money" type="java.lang.Double"><column name="money"></column></property></class></hibernate-mapping>

5、实体关系映射文件注册到 Hibernate 的配置文件中。

<!--注册实体关系映射文件-->
<mapping resource="com/htl/entity/People.hbm.xml"></mapping>

6、使用 Hibernate API 完成数据操作。

package com.htl.test;import com.htl.entity.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class Test {public static void main(String[] args) {// 创建ConfigurationConfiguration configuration = new Configuration().configure();/*如果默认是 hibernate.cfg.xml ,configure()里面就不用写。如果xml名字改了,里面就写修改的名字。*/System.out.println(configuration);// 获取SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();// 获取SessionSession session = sessionFactory.openSession();// 向People表中插入数据People people = new People();people.setName("李四");people.setMoney(200.0);session.save(people);/*保存*/session.beginTransaction().commit();/*提交事务*/session.close();/*关闭session*/}
}

7、pom.xml 中需要配置 resource

<!--配置 resource-->
<!--能够识别读取java里面的xml文件-->
<build><resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes></resource></resources>
</build>

三、Hibernate 级联操作

1、一对多关系

客户和订单:每个客户可以购买多个产品,生成多个订单,但是一个订单只能属于一个客户,所以客户
是一,订单是多。

在这里插入图片描述

数据库中一的一方是主表,多的一方时候从表,通过主外键关系来维护。

面向对象中

package com.htl.entity;import lombok.Getter;
import lombok.Setter;import java.util.Set;@Getter
@Setter
public class Customer {private Integer id;private String name;private Set<Orders> orders;@Overridepublic String toString() {return "Customer{" +"id=" + id +", name='" + name + '\'' +'}';}
}
package com.htl.entity;import lombok.Getter;
import lombok.Setter;@Getter
@Setter
public class Orders {private Integer id;private String name;private Customer customer;@Overridepublic String toString() {return "Orders{" +"id=" + id +", name='" + name + '\'' +'}';}
}

2、多对多关系

学生选课:一门课程可以被多个学生选择,一个学生可以选择多门课程,学生是多,课程也是多。

数据库中是通过两个一对多关系来维护的,学生和课程都是主表,额外增加⼀张中间表作为从表,两张
主表和中间表都是一对多关系。

在这里插入图片描述

面向对象中

package com.htl.entity;import lombok.Getter;
import lombok.Setter;import java.util.Set;@Getter
@Setter
public class Account {private Integer id;private String name;private Set<Course> courses;@Overridepublic String toString() {return "Account{" +"id=" + id +", name='" + name + '\'' +'}';}
}
package com.htl.entity;import lombok.Getter;
import lombok.Setter;import java.util.Set;@Getter
@Setter
public class Course {private Integer id;private String name;private Set<Account> accounts;@Overridepublic String toString() {return "Course{" +"id=" + id +", name='" + name + '\'' +'}';}
}

Java 和数据库对于这两种关系的体现完全是两种不同的方式,Hibernate 框架的作用就是将这两种方式进行转换和映射。

四、Hibernate 实现一对多

Customer.hbm.xml

<?xml version="1.0"?><!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.htl.entity.Customer" table="customer"><id name="id" type="java.lang.Integer"><column name="id"></column><generator class="identity"></generator></id><property name="name" type="java.lang.String"><column name="name"></column></property><set name="orders" table="orders"><key column="cid"></key><one-to-many class="com.htl.entity.Orders"></one-to-many></set></class> </hibernate-mapping>
  • set 标签来配置实体类中的集合属性 orsers
  • name 实体类属性名
  • table 表名
  • key 外键
  • one-to-many 与集合泛型的实体类对应

Orders.hbm.xml

<?xml version="1.0"?><!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.htl.entity.Orders" table="orders"><id name="id" type="java.lang.Integer"><column name="id"></column><generator class="identity"></generator></id><property name="name" type="java.lang.String"><column name="name"></column></property><many-to-one name="customer" class="com.htl.entity.Customer" column="cid"></many-to-one></class> </hibernate-mapping>
  • many-to-one 配置实体类对应的对象属性
  • name 属性名
  • class 属性对应的类
  • column 外键

需要在 Hibernate 配置文件中进行注册

<!-- 注册实体关系映射⽂件 -->
<mapping resource="com/htl/entity/Customer.hbm.xml"></mapping>
<mapping resource="com/htl/entity/Orders.hbm.xml"></mapping>

Hibernate API

package com.htl.test;import com.htl.entity.Customer;
import com.htl.entity.Orders;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class Test2 {public static void main(String[] args) {//创建ConfigurationConfiguration configuration = new Configuration().configure();/*如果默认是 hibernate.cfg.xml ,configure()里面就不用写。如果xml名字改了,里面就写修改的名字。*///获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();//创建CustomerCustomer customer = new Customer();customer.setName("张三11");//创建OrdersOrders orders = new Orders();orders.setName("订单11");//建立关联关系orders.setCustomer(customer);//保存session.save(customer);session.save(orders);//提交事务session.beginTransaction().commit();//关闭session.close();}
}

五、Hibernate 实现多对多

Account.hbm.xml

<?xml version="1.0"?><!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.htl.entity.Account" table="t_account"><id name="id" type="java.lang.Integer"><column name="id"></column><generator class="identity"></generator></id><property name="name" type="java.lang.String"><column name="name"></column></property><set name="courses" table="account_course"><key column="aid"></key><many-to-many class="com.htl.entity.Course" column="cid"></many-to-many></set></class> </hibernate-mapping>

Course.hbm.xml

<?xml version="1.0"?><!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.htl.entity.Course" table="t_course"><id name="id" type="java.lang.Integer"><column name="id"></column><generator class="identity"></generator></id><property name="name" type="java.lang.String"><column name="name"></column></property><set name="accounts" table="account_course"><key column="cid"></key><many-to-many class="com.htl.entity.Account" column="aid"></many-to-many></set></class> </hibernate-mapping>
  • name 实体类对应的集合属性名
  • table 中间表名
  • key 外键
  • many-to-many 与集合泛型的实体类对应
  • column 属性与中间表的外键字段名对应

注册 Hibernate 配置文件中

<!--注册实体关系映射文件-->
<mapping resource="com/htl/entity/Account.hbm.xml"></mapping>
<mapping resource="com/htl/entity/Course.hbm.xml"></mapping>

Hibernate API

package com.htl.test;import com.htl.entity.Account;
import com.htl.entity.Course;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;import java.util.HashSet;
import java.util.Set;public class Test3 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();/*如果默认是 hibernate.cfg.xml ,configure()里面就不用写。如果xml名字改了,里面就写修改的名字。*///获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();Course course = new Course();course.setName("C语言");Account account = new Account();account.setName("李四");Set<Course> courses = new HashSet<Course>();courses.add(course);account.setCourses(courses);session.save(course);session.save(account);session.beginTransaction().commit();session.close();}
}

六、Hibernate 延迟加载

延迟加载、惰性加载、懒加载

使用延迟加载可以提⾼程序的运行效率,Java 程序与数据库交互的频次越低,程序运⾏的效率就越高,所以我们应该尽量减少 Java 程序与数据库的交互次数,Hibernate 延迟加载就很好的做到了这一点。

客户和订单,当我们查询客户对象时,因为有级联设置,所以会将对应的订单信息一并查询出来,这样就需要发送两条 SQL 语句,分别查询客户信息和订单信息。

延迟加载的思路是:当我们查询客户的时候,如果没有访问订单数据,只发送一条 SQL 语句查询客户信息,如果需要访问订单数据,则发送两条 SQLL。

延迟加载可以看作是⼀种优化机制,根据具体的需求,⾃动选择要执⾏的 SQL 语句数量。

1、一对多

1、查询 Customer,对 orders 进行延迟加载设置,在 customer.hbm.xml 进行设置,延迟加载默认开启。

<set name="orders" table="orders" lazy="true"><key column="cid"></key><one-to-many class="com.htl.entity.Orders"></one-to-many>
</set>

2、查询 Customer

package com.htl.test;import com.htl.entity.Customer;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class Test4 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();/*如果默认是 hibernate.cfg.xml ,configure()里面就不用写。如果xml名字改了,里面就写修改的名字。*///获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();Customer customer = session.get(Customer.class,6);System.out.println(customer);session.close();}
}

在这里插入图片描述

package com.htl.test;import com.htl.entity.Customer;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class Test4 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();/*如果默认是 hibernate.cfg.xml ,configure()里面就不用写。如果xml名字改了,里面就写修改的名字。*///获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();Customer customer = session.get(Customer.class,6);System.out.println(customer.getOrders());session.close();}
}

在这里插入图片描述

在 customer.hbm.xml 中设置延迟加载关闭后

<set name="orders" table="orders" lazy="false"><key column="cid"></key><one-to-many class="com.htl.entity.Orders"></one-to-many>
</set>
package com.htl.test;import com.htl.entity.Customer;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class Test4 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();/*如果默认是 hibernate.cfg.xml ,configure()里面就不用写。如果xml名字改了,里面就写修改的名字。*///获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();Customer customer = session.get(Customer.class,6);System.out.println(customer);session.close();}
}

在这里插入图片描述

lazy 除了可以设置 true 和 false 之外,还可以设置 extra,extra 是比 true 更加懒惰的⼀种加载方式,或者说是更加智能的⼀种加载方式,通过例子看区别:

查询 Customer 对象,打印该对象对应的 orders 集合的长度。

在 customer.hbm.xml 中设置延迟加载为 extra 后。

<set name="orders" table="orders" lazy="extra"><key column="cid"></key><one-to-many class="com.htl.entity.Orders"></one-to-many>
</set>
package com.htl.test;import com.htl.entity.Customer;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class Test4 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();/*如果默认是 hibernate.cfg.xml ,configure()里面就不用写。如果xml名字改了,里面就写修改的名字。*///获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();Customer customer = session.get(Customer.class,6);System.out.println(customer.getOrders().size());session.close();}
}

在这里插入图片描述

也可以通过 Orders 来设置 Customer 的延迟加载,orders.hbm.xml 中进⾏设置。

<many-to-one name="customer" class="com.htl.entity.Customer" column="cid" lazy="proxy"></many-to-one>
package com.htl.test;import com.htl.entity.Orders;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class Test5 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();Orders orders = session.get(Orders.class,2);System.out.println(orders);session.close();}
}

在这里插入图片描述

package com.htl.test;import com.htl.entity.Orders;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class Test5 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();Orders orders = session.get(Orders.class,2);System.out.println(orders.getCustomer());session.close();}
}

在这里插入图片描述

no-proxy:当调用方法需要访问 customer 的成员变量时,发送 SQL 语句查询 Customer,否则不查询。

proxy:无论调用方法是否需要访问 customer 的成员变量,都会发送 SQL 语句查询 Customer。

2、多对多

1、查询 Course,加载对应的 Account,默认延迟加载开启。

在 Course.hbm.xml 里面,lazy 设置为 true。不写默认为 true。

<set name="accounts" table="account_course" lazy="true"><key column="cid"></key><many-to-many class="com.htl.entity.Account" column="aid"></many-to-many>
</set>
package com.htl.test;import com.htl.entity.Course;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class Test6 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();Course course = session.get(Course.class,1);System.out.println(course);session.close();}
}

在这里插入图片描述

package com.htl.test;import com.htl.entity.Course;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class Test6 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();Course course = session.get(Course.class,1);System.out.println(course.getAccounts());session.close();}
}

在这里插入图片描述

在 Course.hbm.xml 里面,lazy 关闭,设置为 false 后,查看结果。

<set name="accounts" table="account_course" lazy="false"><key column="cid"></key><many-to-many class="com.htl.entity.Account" column="aid"></many-to-many>
</set>
package com.htl.test;import com.htl.entity.Course;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class Test6 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();Course course = session.get(Course.class,1);System.out.println(course);session.close();}
}

在这里插入图片描述

2、查询 Account,加载对应的 Course,默认延迟加载开启。

在 Account.hbm.xml 里面,lazy 设置为 true。不写默认为 true。

<set name="courses" table="account_course" lazy="true"><key column="aid"></key><many-to-many class="com.htl.entity.Course" column="cid"></many-to-many>
</set>
package com.htl.test;import com.htl.entity.Account;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class Test7 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();Account account = session.get(Account.class,1);System.out.println(account);session.close();}
}

在这里插入图片描述

package com.htl.test;import com.htl.entity.Account;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class Test7 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();Account account = session.get(Account.class,1);System.out.println(account.getCourses());session.close();}
}

在这里插入图片描述

在 Account.hbm.xml 里面,lazy 关闭,设置为 false 后,查看结果。

<set name="courses" table="account_course" lazy="false"><key column="aid"></key><many-to-many class="com.htl.entity.Course" column="cid"></many-to-many>
</set>
package com.htl.test;import com.htl.entity.Account;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class Test7 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();Account account = session.get(Account.class,1);System.out.println(account);session.close();}
}

在这里插入图片描述

七、Hibernate 配置文件

  • hibernate.cfg.xml
  • hbm.xml

1、Hibernate.cfg.xml

hibernate.cfg.xml 配置 Hibernate 的全局环境。

<?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.username">root</property><property name="connection.password">password</property><property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property><property name="connection.url">jdbc:mysql://localhost:3306/hibernate?serverTimezone=Asia/Shanghai&amp;useUnicode=true&amp;characterEncoding=UTF-8</property><!--C3P0连接池--><property name="hibernate.c3p0.acquire_increment">10</property><property name="hibernate.c3p0.idle_test_period">10000</property><property name="hibernate.c3p0.timeout">5000</property><property name="hibernate.c3p0.max_size">30</property><property name="hibernate.c3p0.min_size">5</property><property name="hibernate.c3p0.max_statements">10</property><!--是否输出底层SQL语句--><property name="hibernate.show_sql">true</property><!--输出底层SQL语句是否格式化--><property name="hibernate.format_sql">true</property><!--是否自动生成数据库--><!--update:如果已经有表,更新;如果没有,创建。(一般用这个)--><property name="hibernate.hbm2ddl.auto">update</property><!--数据库方言--><!--如果使用的mysql是5.x,写:org.hibernate.dialect.MySQL5Dialect。--><!--如果使用的mysql是8.x,写:org.hibernate.dialect.MySQL8Dialect。--><property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property><!--注册实体关系映射文件--><mapping resource="com/htl/entity/People.hbm.xml"/><mapping resource="com/htl/entity/Customer.hbm.xml"/><mapping resource="com/htl/entity/Orders.hbm.xml"/><mapping resource="com/htl/entity/Account.hbm.xml"/><mapping resource="com/htl/entity/Course.hbm.xml"/></session-factory></hibernate-configuration>

<1>、数据库的基本信息。

<property name="connection.username">root</property>
<property name="connection.password">password</property>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate?serverTimezone=Asia/Shanghai&amp;useUnicode=true&amp;characterEncoding=UTF-8</property>

<2>、集成 C3P0,设置数据库连接池信息。

<property name="hibernate.c3p0.acquire_increment">10</property>
<property name="hibernate.c3p0.idle_test_period">10000</property>
<property name="hibernate.c3p0.timeout">5000</property>
<property name="hibernate.c3p0.max_size">30</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_statements">10</property>

<3>、Hibernate 基本信息

<!--是否输出底层SQL语句-->
<property name="hibernate.show_sql">true</property>
<!--输出底层SQL语句是否格式化-->
<property name="hibernate.format_sql">true</property>
<!--是否自动生成数据库-->
<!--update:如果已经有表,更新;如果没有,创建。(一般用这个)-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!--数据库方言-->
<!--如果使用的mysql是5.x,写:org.hibernate.dialect.MySQL5Dialect。-->
<!--如果使用的mysql是8.x,写:org.hibernate.dialect.MySQL8Dialect。-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>

<-是否自动生成数据库->里面可填的几个值:

  • update:动态创建表,如果表存在,则直接使用,如果表不存在,则创建。
  • create:无论表是否存在,都会重新创建。
  • create-drop:初始化创建表,程序结束时删除表。
  • validate:校验实体关系映射文件和数据表是否对应,不能对应直接报错。

<4>、注册实体关系映射文件

<mapping resource="com/htl/entity/People.hbm.xml"></mapping>
<mapping resource="com/htl/entity/Customer.hbm.xml"></mapping>
<mapping resource="com/htl/entity/Orders.hbm.xml"></mapping>
<mapping resource="com/htl/entity/Account.hbm.xml"></mapping>
<mapping resource="com/htl/entity/Course.hbm.xml"></mapping>

2、hbm.xml

称为:实体关系映射文件

<?xml version="1.0"?><!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.htl.entity.Course" table="t_course"><id name="id" type="java.lang.Integer"><column name="id"></column><generator class="identity"></generator></id><property name="name" type="java.lang.String"><column name="name"></column></property><set name="accounts" table="account_course" lazy="true"><key column="cid"></key><many-to-many class="com.htl.entity.Account" column="aid"></many-to-many></set></class> </hibernate-mapping>

<1>、hibernate-mapping 属性

  • package:给 class 节点对应的实体类统一设置包名,此处设置包名,class 的 name 属性就可以省略包名。

如上面的实体映射文件修改为下面的:

  <?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.htl.entity"><class name="Course" table="t_course"><id name="id" type="java.lang.Integer"><column name="id"></column><generator class="identity"></generator></id><property name="name" type="java.lang.String"><column name="name"></column></property><set name="accounts" table="account_course" lazy="true"><key column="cid"></key><many-to-many class="Account" column="aid"></many-to-many></set></class> </hibernate-mapping>
  • schema:数据库 schema 的名称。

  • catalog:数据库 catalog 的名称。

  • default-cascade:默认的级联关系,默认为 none。

  • default-access:Hibernate 用来访问属性的策略。

  • default-lazy:指定了未明确注明 lazy 属性的 Java 属性和集合类,Hibernate 会采用什么样的加载风格,默认为 true 。

  • auto-import:指定我们是否可以在查询语句中使用非全限定类名,默认为 true,如果项目中有两个同名的持久化类,最好在这两个类的对应映射文件中配置为 false。

<2>、class 属性

  • name:实体类名
  • table:数据表名
  • schema:数据库 schema 的名称,会覆盖 hibernate-mapping 的 schema
  • catalog:数据库 catalog 的名称,会覆盖 hibernate-mapping 的 catalog
  • proxy:指定⼀个接口,在延迟加载时作为代理使用
  • dynamic-update:动态更新(默认关闭)
  • dynamic-insert:动态添加(默认关闭)

当没有开启动态加载(dynamic-insert)时,只设置 name 的值,没有赋值的 money 也会默认赋 null 存入。两个都存。(有多少属性存多少属性)

package com.htl.test;import com.htl.entity.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class Test8 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();People people = new People();people.setName("张三");session.save(people);session.close();}
}

在这里插入图片描述

当开启动态加载(dynamic-insert)后,只设置 name 的值,则只存 name ,其余不存:

<class name="com.htl.entity.People" table="people" dynamic-insert="true">

在这里插入图片描述

当没有开启动态更新(dynamic-update),更新 money 时,update语句连同 name 一块更新了。

package com.htl.test;import com.htl.entity.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class Test8 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();People people = session.get(People.class,6);people.setMoney(2000.0);session.update(people);session.beginTransaction().commit();session.close();}
}

在这里插入图片描述

当开启动态更新(dynamic-update)后,更新 money 时,update语句不会连同 name 一块更新。

<class name="com.htl.entity.People" table="people" dynamic-insert="true" dynamic-update="true">
package com.htl.test;import com.htl.entity.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class Test8 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();People people = session.get(People.class,7);people.setMoney(7000.0);session.update(people);session.beginTransaction().commit();session.close();}
}

在这里插入图片描述

  • where:查询时给 SQL 添加 where 条件(默认关闭)

当没加 where 时:

package com.htl.test;import com.htl.entity.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;import java.util.List;public class Test8 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();String hql = "from People";Query query = session.createQuery(hql);List<People> list = query.list();for (People people :list) {System.out.println(people);}session.beginTransaction().commit();session.close();}
}

在这里插入图片描述

加了 where 后,可以用 where 约束查询结果。

<class name="com.htl.entity.People" table="people" dynamic-insert="true" dynamic-update="true" where="id = 6">

运行语句同上。

在这里插入图片描述

<3>、id 属性

  • name:实体类属性名
  • type:实体类属性数据类型

此处可以设置两种类型的数据:Java 数据类型或者 Hibernate 映射类型。

实体类的属性数据类型必须与数据表对应的字段数据类型一致:

int 对应 int,String 对应 varchar

如何进行映射?

Java 数据类型映射到 Hibernate 映射类型,再由 Hibernate 映射类型映射到 SQL 数据类型

Java —》Hibernate —〉SQL

  • column:数据表的主键字段名
  • generator:主键生成策略

1、hilo 算法

2、increment:Hibernate 自增

3、identity:数据库自增

4、native:本地策略,根据底层数据库自动选择主键的生成策略

5、uuid.hex 算法

6、select 算法

<4>、property 属性

  • name:实体类的属性名
  • column:数据表字段名
  • type:数据类型
  • update:该字段是否可以修改,默认为 true
  • insert:该字段是否可以添加,默认为 true
  • lazy:延迟加载策略

<5>、实体关系映射文件属性

1、inverse

inverse 属性是用来设置是否将维护权交给对方,默认是 false,不交出维护权,双方都在维护,将它设置为 true,表示放弃维护。

Customer 和 Orders 是一对多关系,一个 Customer 对应多个 Orders,实体类中用一个 set 集合来表示对应的 Orders。

package com.htl.entity;import lombok.Getter;
import lombok.Setter;import java.util.Set;@Getter
@Setter
public class Customer {private Integer id;private String name;private Set<Orders> orders;@Overridepublic String toString() {return "Customer{" +"id=" + id +", name='" + name + '\'' +'}';}
}

Customer.hbm.xml 中使用 set 标签来配置映射关系。

<?xml version="1.0"?><!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.htl.entity.Customer" table="customer"><id name="id" type="java.lang.Integer"><column name="id"></column><generator class="identity"></generator></id><property name="name" type="java.lang.String"><column name="name"></column></property><set name="orders" table="orders" lazy="true"><key column="cid"></key><one-to-many class="com.htl.entity.Orders"></one-to-many></set></class> </hibernate-mapping>

实体类

package com.htl.entity;import lombok.Getter;
import lombok.Setter;@Getter
@Setter
public class Orders {private Integer id;private String name;private Customer customer;@Overridepublic String toString() {return "Orders{" +"id=" + id +", name='" + name + '\'' +'}';}
}

Orders.hbm.xml

<?xml version="1.0"?><!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.htl.entity.Orders" table="orders"><id name="id" type="java.lang.Integer"><column name="id"></column><generator class="identity"></generator></id><property name="name" type="java.lang.String"><column name="name"></column></property><many-to-one name="customer" class="com.htl.entity.Customer" column="cid" lazy="proxy"></many-to-one></class> </hibernate-mapping>
package com.htl.test;import com.htl.entity.Customer;
import com.htl.entity.Orders;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class Test9 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();Customer customer = new Customer();customer.setName("李四");Orders orders1 = new Orders();orders1.setName("订单4");orders1.setCustomer(customer);Orders orders2 = new Orders();orders2.setName("订单44");orders2.setCustomer(customer);session.save(customer);session.save(orders1);session.save(orders2);session.beginTransaction().commit();session.close();}
}

3条SQL执行语句。

在这里插入图片描述

当添加Customer维护后,(此时Customer和Orders双方都在维护)

package com.htl.test;import com.htl.entity.Customer;
import com.htl.entity.Orders;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;import java.util.HashSet;
import java.util.Set;public class Test9 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();Customer customer = new Customer();customer.setName("王5");Orders orders1 = new Orders();orders1.setName("5");orders1.setCustomer(customer);Orders orders2 = new Orders();orders2.setName("55");orders2.setCustomer(customer);//添加Customer维护Set<Orders> orders = new HashSet<Orders>();orders.add(orders1);orders.add(orders2);customer.setOrders(orders);session.save(customer);session.save(orders1);session.save(orders2);session.beginTransaction().commit();session.close();}
}

5条SQL执行语句。

在这里插入图片描述

在这里插入图片描述

因为 Customer 和 Orders 都在维护一对多关系,所以会重复设置主外键约束关系。

如何避免这种情况?
1、在 Java 代码中去掉一方维护关系代码。
2、通过配置来解决。

Customer.hbm.xml

<set name="orders" table="orders" lazy="true" inverse="true"><key column="cid"></key><one-to-many class="com.htl.entity.Orders"></one-to-many>
</set>

将 inverse 置为 true,表示 Customer 放弃维护。

再运行:

package com.htl.test;import com.htl.entity.Customer;
import com.htl.entity.Orders;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;import java.util.HashSet;
import java.util.Set;public class Test9 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();Customer customer = new Customer();customer.setName("王5");Orders orders1 = new Orders();orders1.setName("5");orders1.setCustomer(customer);Orders orders2 = new Orders();orders2.setName("55");orders2.setCustomer(customer);//添加Customer维护Set<Orders> orders = new HashSet<Orders>();orders.add(orders1);orders.add(orders2);customer.setOrders(orders);session.save(customer);session.save(orders1);session.save(orders2);session.beginTransaction().commit();session.close();}
}

3条SQL执行语句。

在这里插入图片描述

2、cascade

用来设置级联操作。

正常删除有外键约束的表里面的数据时,会报错。

package com.htl.test;import com.htl.entity.Customer;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class Test10 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();//删除Customer customer = session.get(Customer.class,7);session.delete(customer);session.beginTransaction().commit();session.close();}
}

在这里插入图片描述

想要删除有外键约束的表里面的数据的方法如下,

方法一:

package com.htl.test;import com.htl.entity.Customer;
import com.htl.entity.Orders;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;import java.util.Iterator;public class Test10 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();//删除Customer customer = session.get(Customer.class,7);Iterator<Orders> iterator = customer.getOrders().iterator();while (iterator.hasNext()){session.delete(iterator.next());}session.delete(customer);session.beginTransaction().commit();session.close();}
}

在这里插入图片描述

在这里插入图片描述

方法二:

实体关系映射⽂件中设置 cascade 值完成级联删除。

Customer.hbm.xml

<set name="orders" table="orders" lazy="true" inverse="true" cascade="delete"><key column="cid"></key><one-to-many class="com.htl.entity.Orders"></one-to-many>
</set>

然后不需要再迭代,直接就能删除了。

package com.htl.test;import com.htl.entity.Customer;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class Test10 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();//删除Customer customer = session.get(Customer.class,7);session.delete(customer);session.beginTransaction().commit();session.close();}
}

在这里插入图片描述

在这里插入图片描述

八、Hibernate HQL

HQL:Hibernate Query Language,是 Hibernate 框架提供的一种查询机制,它和 SQL 类似,不同的是 HQL 是面向对象的查询语句,让开发者能够以面向对象的思想来编写查询语句,对 Java 编程是一种很友好的方式。

HQL 不能直接参与数据库的交互,它是中间层语⾔。

Java —》HQL —〉Hibernate —》SQL —〉DB

HQL 只能完成查询修改删除新增是无法操作的

1、查询对象

查询表中所有数据,自动完成对象的封装,返回 List 集合。

HQL 进行查询,from 关键字后面不能写表名,必须写表对应的实体类名。

package com.htl.test;import com.htl.entity.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;import java.util.List;public class Test11 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();//查询对象String hql = "from People ";/* 写的是所查询表对应的实体类的名字!尽量写实体类的全类名。也就是 from com.htl.entity.People */Query query = session.createQuery(hql);List<People> list = query.list();for (People people:list){System.out.println(people);}session.close();}
}

注意:People.hbm.xml 里面的 where=“id = 6” 要去掉。这样才能查询出完整的 People 表里面的数据。

在这里插入图片描述

2、分页查询

HQL 分页查询可以通过调用 query 的方法来完成。

1、setFirstResult() 设置起始下标

2、setMaxResults() 设置截取长度

package com.htl.test;import com.htl.entity.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;import java.util.List;public class Test12 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();//分页查询String hql = "from com.htl.entity.People";Query query = session.createQuery(hql);query.setFirstResult(1);query.setMaxResults(3);/* 截取People表里面第2个到第4个的数据。*/List<People> list = query.list();for (People people:list){System.out.println(people);}session.close();}
}

在这里插入图片描述

3、where 条件查询

HQL 直接追加 where 关键字作为查询条件,与 SQL 没有区别。

package com.htl.test;import com.htl.entity.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;public class Test13 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();//where条件查询String hql = "from People where id = 6";Query query = session.createQuery(hql);People people = (People) query.list().get(0);System.out.println(people);session.close();}
}

query.list() 返回一个集合,此时集合中只有一个对象,通过下标 0 取出该对象。

在这里插入图片描述

当表里面没有符合where查询条件的数据时,query调用uniqueResult()方法时,就不会抛出异常,而是返回null 。

String hql = "from People where id = 0";
Query query = session.createQuery(hql);
People people = (People) query.uniqueResult();
System.out.println(people);

在这里插入图片描述

4、模糊查询

查询名称包含“三”的所有记录。

package com.htl.test;import com.htl.entity.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;import java.util.List;public class Test14 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();//模糊查询String hql = "from People where name like '%三%'";Query query = session.createQuery(hql);List<People> list = query.list();for (People people:list){System.out.println(people);}session.close();}
}

在这里插入图片描述

5、order by

按照 id 进行排序。

asc 是生序排列,desc 是降序排列。

package com.htl.test;import com.htl.entity.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;import java.util.List;public class Test15 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();String hql = "from People order by id";/* 默认是 asc 升序。*/Query query = session.createQuery(hql);List<People> list = query.list();for (People people:list){System.out.println(people);}session.close();}
}

在这里插入图片描述

降序:

String hql = "from People order by id desc ";

在这里插入图片描述

6、查询实体对象的属性

package com.htl.test;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;public class Test16 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();//查询实体对象的属性String hql = "select name from People where id = 6";Query query = session.createQuery(hql);String name = (String) query.uniqueResult();System.out.println(name);session.close();}
}

在这里插入图片描述

7、占位符

package com.htl.test;import com.htl.entity.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;import java.util.List;public class Test17 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();//占位符String hql = "from People where name = :name";Query query = session.createQuery(hql);query.setString("name","张三0");List<People> list = query.list();for (People people:list){System.out.println(people);}session.close();}
}

在这里插入图片描述

8、级联查询

package com.htl.test;import com.htl.entity.Customer;
import com.htl.entity.Orders;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;import java.util.List;public class Test18 {public static void main(String[] args) {//创建 ConfigurationConfiguration configuration = new Configuration().configure();//获取 SessionFactorySessionFactory sessionFactory = configuration.buildSessionFactory();//获取 SessionSession session = sessionFactory.openSession();//级联查询String hql = "from Customer where name = :name";Query query1 = session.createQuery(hql);query1.setString("name","李四");Customer customer = (Customer) query1.uniqueResult();String hql2 = "from Orders where customer = :customer";Query query2 = session.createQuery(hql2);query2.setEntity("customer",customer);List<Orders> list = query2.list();for (Orders orders:list){System.out.println(orders);}session.close();}
}

在这里插入图片描述

相关文章:

学习笔记068——Hibernate框架介绍以及使用方法

文章目录 一、如何使用二、具体操作1、创建 Maven 工程&#xff0c;pom.xml2、hibernate.cfg.xml3、创建实体类4、创建实体关系映射文件5、实体关系映射文件注册到 Hibernate 的配置文件中。6、使用 Hibernate API 完成数据操作。7、pom.xml 中需要配置 resource 三、Hibernate…...

Maven 安装配置(详细教程)

文章目录 一、Maven 简介二、下载 Maven三、配置 Maven3.1 配置环境变量3.2 Maven 配置3.3 IDEA 配置 四、结语 一、Maven 简介 Maven 是一个基于项目对象模型&#xff08;POM&#xff09;的项目管理和自动化构建工具。它主要服务于 Java 平台&#xff0c;但也支持其他编程语言…...

虚幻开发中的MYPROJECTFORPLUG_API

百度网盘-免费云盘丨文件共享软件丨超大容量丨存储安全 在虚幻引擎5&#xff08;Unreal Engine 5&#xff09;中&#xff0c;以及许多其他使用C的项目中&#xff0c;__declspec(dllexport) 和 __declspec(dllimport) 是用来处理动态链接库&#xff08;DLL&#xff09;的宏定义…...

顺序栈及其实现过程

目录 一、概念 二、顺序栈 2.1顺序栈的结构模型 2.2顺序栈的实现 2.2.1创建 2.2.2判断栈是否为空 2.2.3判断栈是否为空 2.2.4入栈 2.2.5出栈 2.2.6查看栈顶 2.2.7清空栈 2.2.8释放栈 一、概念 栈是限制在某一端进行插入、删除操作的线性表&#xff0c;俗称堆栈&…...

内圆弧转子泵绘制工具开发

接着上期的Gerotor 泵的话题继续。最近有小伙伴找我开发一个内圆弧摆线泵的计算绘制工具&#xff0c;也就是把上次计算绘制的过程做成一个桌面应用工具&#xff0c;这样用起来会更方便、效率更高。那究竟是什么样的工具呢&#xff1f;一起来看看&#xff1a; 前面不是已经有了上…...

linux网络编程 | c | 多进程并发服务器实现

多进程并发服务器 基于该视频完成 11-多进程并发服务器思路分析_哔哩哔哩_bilibili 通过的是非阻塞忙轮询的方式实现的 和阻塞等待的区别就是&#xff0c;阻塞是真的阻塞了&#xff0c;而这个方式是一直在问有没有请求有没有请求 文章目录 多进程并发服务器1.核心思路&…...

Vins_Fusion_gpu中source setup.bash

文章目录 source setup.bashsetup.bashsetup.sh脚本的主要功能脚本的详细解释1. **初始化和检查**2. **检测操作系统**3. **设置环境变量**4. **记住 shell 类型**5. **调用 Python 脚本生成环境变量**6. **加载环境钩子**7. **清理** 总结 _setup_util.py_setup_util.py 的完整…...

怎么理解大模型推理时的Top_P参数?

本篇博客介绍一下大模型推理时的Top_P参数&#xff0c;Top_P与Top_K&#xff0c;Beamsearch&#xff0c;temperature 都是什么关系以及该如何选择Top_P参数。 文章目录 一、什么是Top_P参数&#xff1f;二、工作原理三、top_p和top_k是什么关系&#xff1f;四、Top_P和BeamSea…...

hive+hadoop架构数仓使用问题记录

使用问题记录 问题1&#xff1a;5条数据的表执行count(*)函数&#xff0c;很慢&#xff0c;43s才出结果&#xff1f; 该数仓的分析计算是基于hadoop的mapreduce分布式计算框架运行的&#xff0c;适用于大量/海量数据&#xff0c;少量数据&#xff0c;还是使用单体数据库快。也…...

前端的 Python 入门指南(三):数据类型对比 - 彻底的一切皆对象实现和包装对象异同

《前端的 Python 入门指南》系列文章&#xff1a; &#xff08;一&#xff09;&#xff1a;常用语法和关键字对比&#xff08;二&#xff09;&#xff1a;函数的定义、参数、作用域对比&#xff08;三&#xff09;&#xff1a;数据类型对比 - 彻底的一切皆对象实现和包装对象异…...

Axios结合Typescript 二次封装完整详细场景使用案例

Axios 是一个基于 promise 的 HTTP 客户端&#xff0c;用于浏览器和 node.js。二次封装 Axios 主要是为了统一管理 HTTP 请求&#xff0c;例如设置统一的请求前缀、头部、超时时间&#xff0c;统一处理请求和响应的格式&#xff0c;以及错误处理等。 以下是一个使用 TypeScrip…...

基于Kubesphere实现微服务的CI/CD——部署微服务项目(三)

目录 一、kubesphere安装 1、安装本地持久存储 1.1、default-storage-class.yaml 1.2、 openebs-operator.yaml 1.3、安装 Default StorageClass 2、安装kubesphere 2.1、安装Helm 2.2、安装kubesphere 二、配置kubesphere 1、安装插件 2、创建devops项目 3、配置…...

【使用webrtc-streamer解析rtsp视频流】

webrtc-streamer WebRTC (Web Real-Time Communications) 是一项实时通讯技术&#xff0c;它允许网络应用或者站点&#xff0c;在不借助中间媒介的情况下&#xff0c;建立浏览器之间点对点&#xff08;Peer-to-Peer&#xff09;的连接&#xff0c;实现视频流和&#xff08;或&a…...

element左侧导航栏

由element组件搭建的左侧导航栏 预览: html代码: <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>首页</title><style> /*<!-- 调整页面背景颜色-->*/body{background-colo…...

【金融贷后】贷后运营精细化管理

文章目录 一、贷后专业术语讲解① 什么是贷后&#xff0c;贷后部是干什么的&#xff1f;② 贷后部门常见组织架构&#xff1f;③ 贷后专业术语有哪些&#xff1f; 二、贷后常用作业手段介绍① 贷后产品形态介绍&#xff1f;② 催收常用的方法&#xff1f; 三、贷后策略岗位介绍…...

学习CSS第七天

学习文章目录 一.交集选择器 一.交集选择器 使用多个条件符合的元素&#xff0c;可提高区分的精准度 元素配合类名是使用场景最多的 &#xff08;元素必须是第一位&#xff0c;ID一般不写&#xff09; <!DOCTYPE html> <html lang"zh-CN"> <head>…...

Image Stitching using OpenCV

文章目录 简介图像拼接管道特征检测和提取特征检测特征提取 特征匹配强力匹配FLANN&#xff08;近似最近邻快速库&#xff09;匹配 单应性估计扭曲和混合结论 使用opencv进行图像拼接 原为url: https://medium.com/paulsonpremsingh7/image-stitching-using-opencv-a-step-by-s…...

CentOS7 安装Selenium(使用webdriver_manager自动安装ChromeDriver)

在 CentOS 7 上安装 Selenium 通常涉及几个步骤&#xff0c;包括安装 Python、安装 Selenium 库、安装 WebDriver 以及配置环境。以下是详细的步骤&#xff1a; 1. 安装 Python 和 pip 如果你的系统中还没有安装 Python 和 pip&#xff0c;可以使用以下命令进行安装&#xff…...

鸿蒙手机文件目录

最近在开发鸿蒙&#xff0c;想把文件从电脑上发送到鸿蒙上我的手机APP的根目录&#xff0c;但是试了几次目录都不对&#xff0c;最后终于找到了&#xff0c;在这里记录一下 鸿蒙手机路径: /storage/media/100/local/files/Docs 将文件从电脑发送到手机&#xff1a;hdc file s…...

泷羽Sec学习笔记-Bp中ip伪造、爬虫审计

ip伪造与爬虫审计 ip伪造 下载插件&#xff1a;burpFakeIP 地址&#xff1a;GitHub - TheKingOfDuck/burpFakeIP: 服务端配置错误情况下用于伪造ip地址进行测试的Burp Suite插件 python版需要配置jython&#xff1a;下载地址&#xff1a;Maven Central: org.python:jython-…...

电子电工一课一得

首语 在现代社会中&#xff0c;电子电工技术已经渗透到我们生活的方方面面&#xff0c;从家用电器到工业自动化&#xff0c;从通信设备到智能系统&#xff0c;无一不依赖于电子电工技术。因此&#xff0c;掌握电子电工的基础知识&#xff0c;不仅对理工科学生至关重要&#xf…...

Cesium 限制相机倾斜角(pitch)滑动范围

1.效果 2.思路 在项目开发的时候&#xff0c;有一个需求是限制相机倾斜角&#xff0c;也就是鼠标中键调整视图俯角时&#xff0c;不能过大&#xff0c;一般 pitch 角度范围在 0 至 -90之间&#xff0c;-90刚好为正俯视。 在网上查阅了很多资料&#xff0c;发现并没有一个合适的…...

配置ssh-key连接github

GitHub 通过在 2022 年 3 月 15 日删除旧的、不安全的密钥类型来提高安全性。 具体内容参考如下链接 https://docs.github.com/zh/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent mac配置 ssh-keygen -t ed25519 -C …...

Linux——进程控制模拟shell

1.进程创建 我们在之前的文章中介绍过进程创建的方法&#xff0c;可以通过系统调用接口fork来创建新的进程。 fork在创建完新的子进程之后&#xff0c;返回值是一个pid&#xff0c;对于父进程返回子进程的pid&#xff0c;对于子进程返回0。fork函数后父子进程共享代码&#xff…...

【HarmonyOS】鸿蒙应用实现手机摇一摇功能

【HarmonyOS】鸿蒙应用实现手机摇一摇功能 一、前言 手机摇一摇功能&#xff0c;是通过获取手机设备&#xff0c;加速度传感器接口&#xff0c;获取其中的数值&#xff0c;进行逻辑判断实现的功能。 在鸿蒙中手机设备传感器ohos.sensor (传感器)的系统API监听有以下&#xf…...

Kael‘thas Sunstrider Ashes of Al‘ar

Kaelthas Sunstrider 凯尔萨斯逐日者 <血精灵之王> Kaelthas Sunstrider - NPC - 魔兽世界怀旧服TBC数据库_WOW2.43数据库_70级《燃烧的远征》数据库 Ashes of Alar 奥的灰烬 &#xff08;凤凰 310%速度&#xff09; Ashes of Alar - Item - 魔兽世界怀旧服TBC数据…...

CNCF云原生生态版图

CNCF云原生生态版图 概述什么是云原生生态版图如何使用生态版图 项目和产品&#xff08;Projects and products&#xff09;会员&#xff08;Members&#xff09;认证合作伙伴与提供商&#xff08;Certified partners and providers&#xff09;无服务&#xff08;Serverless&a…...

渐冻症:真的无药可治?

“渐冻症”&#xff0c;这个令人闻之色变的疾病&#xff0c;仿佛是生命的冷酷冰封者。一提到渐冻症&#xff0c;很多人脑海中立刻浮现出绝望的画面&#xff0c;认为它无药可治。但事实真的如此吗&#xff1f; 渐冻症&#xff0c;医学上称为肌萎缩侧索硬化症&#xff0c;是一种渐…...

`pg_wal` 目录

在 PostgreSQL 中&#xff0c;自动清理 pg_wal 目录主要通过配置参数 min_wal_size、max_wal_size 和 wal_keep_size 来实现。以下是如何配置 PostgreSQL 以自动清理 WAL 文件的详细步骤和建议&#xff1a; 配置 min_wal_size 和 max_wal_size&#xff1a; min_wal_size&#x…...

【信息系统项目管理师】论文:论信息系统项目的整合管理

文章目录 正文一、制定项目章程二、指定项目管理计划三、指导与管理项目工作四、管理项目知识五、监控项目工作六、实施整体变更控制七、结束项目或阶段 正文 根据省自然资源厅的总体部署&#xff0c;XX市决定于2023年8月开始全市不动产登记系统建设&#xff0c;要求在2024年8…...