当前位置:网站首页>Hibernate学习3 - 自定义SQL
Hibernate学习3 - 自定义SQL
2022-06-24 19:46:00 【嗯嗯**】
HQL - Hibernate Query Language
Hibernate内置的SQL查询语句 = Java代码层级的 – 只能完成SQL的删改查,不能新增
查询
普通
from关键字后面不能跟表名,必须是对应的实体类名或者类全限定名,如果项目中有类名相同的,建议直接写类全限定名,防止hibernate识别失误
添加全局where配置,只有HQL语句时才生效
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- package:类似mybatis的Bean别名type-aliases-package这个属性,用于有些属性不用写全限定名-->
<!-- default-lazy:默认启动懒加载,即一对多,多对多,多对一关系上使用-->
<!-- auto-import:默认true,是否可以在查询语句中使用非全限定名,如果项目中有两个同名的Bean,最好在两个映射文件中设置为false-->
<hibernate-mapping package="top.linruchang.entity" default-lazy="true" auto-import="false" >
<!-- dynamic-insert dynamic-update:默认都为false。true时类似 mybatisplus的代码插入Java语句,只要属性是空则update、insert的SQL语句就不会出现该列 -->
<!-- 这是定义了全局自定义SQL时会携带上where属性的条件,session.createQuery这些生效,像session.get不会生效-->
<class name="Article" table="article" dynamic-insert="true" dynamic-update="true" where="title = 'fdsfds'">
<id name="id" type="java.lang.String">
<column name="id" ></column>
<!--插入时,如果你没有设置ID,会帮你自动添加ID-->
<generator class="uuid"></generator>
</id>
<!--<property name="userId" type="java.lang.String">-->
<!-- <column name="user_id"></column>-->
<!--</property>-->
<property name="title" type="java.lang.String">
<column name="title"></column>
</property>
<property name="content" type="java.lang.String">
<column name="content"></column>
</property>
<property name="likeNum" type="java.lang.Integer">
<column name="like_num"></column>
</property>
<many-to-one lazy="no-proxy" name="sysUser" column="user_id" class="SysUser" />
</class>
</hibernate-mapping>
@Test
public void test7() {
Configuration configure = new Configuration().configure();
SessionFactory sessionFactory = configure.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Query<Article> query = session.createQuery("from top.linruchang.entity.Article", Article.class);
List resultList = query.list();
resultList.stream().forEach(System.out::println);
transaction.commit();
session.close();
}

分页
方式1 == SQL分页
将前面映射文件的where配置去掉
@Test
public void test10() {
Configuration configure = new Configuration().configure();
SessionFactory sessionFactory = configure.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Query<SysUser> query = session.createQuery("from SysUser", SysUser.class);
query.setFirstResult(2);
query.setMaxResults(3);
query.list().stream()
.forEach(System.out::println);
transaction.commit();
session.close();
}

where
@Test
public void test10() {
Configuration configure = new Configuration().configure();
SessionFactory sessionFactory = configure.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
//特别注意 where后面的属性,必须你在该SysUserBean的映射文件中有配置,否则会出错
Query<SysUser> query = session.createQuery("from SysUser where password = 'user'", SysUser.class);
query.setFirstResult(2);
query.setMaxResults(3);
query.list().stream()
.forEach(System.out::println);
transaction.commit();
session.close();
}

占位符
@Test
public void test7() throws InterruptedException {
Configuration configure = new Configuration().configure();
//获取sessionFactory
SessionFactory sessionFactory = configure.buildSessionFactory();
//获取数据库连接session
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Query<Article> query = session.createQuery("from Article where title = :title", Article.class);
query.setParameter("title", "技术面试");
query.list().stream()
.forEach(System.out::println);
transaction.commit();
session.close();
}

级联查询
@Test
public void test8() throws InterruptedException {
Configuration configure = new Configuration().configure();
//获取sessionFactory
SessionFactory sessionFactory = configure.buildSessionFactory();
//获取数据库连接session
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
SysUser sysUser = session.get(SysUser.class, "6d72c93aa292cf2ca2e789919a5e7bdc");
System.out.println(sysUser);
//注意 sysUser必须有在映射文件中有配置,否则直接报找不到映射参数异常
Query<Article> query = session.createQuery("from Article where sysUser = :sysUser1", Article.class);
query.setParameter("sysUser1", sysUser);
query.list().stream()
.forEach(System.out::println);
transaction.commit();
session.close();
}

边栏推荐
- Record a Webflux application memory leak troubleshooting
- 单调栈以及单调栈的应用
- 376. Tâches mécaniques
- 一文理解OpenStack网络
- 抖音實戰~項目關聯UniCloud
- Harmonyos accessing database instances (3) -- use ORM bee to test how good harmonyos is
- 257. detention of offenders
- 7-2 construction of binary tree by post order + middle order sequence
- Uninstall hero League
- 7-6 laying oil well pipeline
猜你喜欢

7-7 digital triangle

Actipro WPF Controls 2022.1.2

JS listens for page or element scroll events, scrolling to the bottom or top

Hyperledger Fabric 2. X dynamic update smart contract

Chapter VI skills related to e-learning 5 (super parameter verification)
HMS core discovery Episode 13 live broadcast Preview - building the real world in mobile games

Volcano becomes spark default batch scheduler

Hello C (III) - pointer
Unveiling the secrets of the Winter Olympics | smartbi's partners supported the "front and back" of the Beijing Winter Olympics

7-6 laying oil well pipeline
随机推荐
Spark's wide dependence and narrow dependence yyds dry goods inventory
From client to server
Morris遍历
Tiktok practice ~ project associated unicloud
Scala IO writes data to a text file
QT cannot be edited with UTF-8
R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用exp函数和coef函数获取模型中每个变量(自变量改变一个单位)对应的优势比(odds ratio)
Scala IO reads data from URLs and other data sources
[introduction to UVM== > episode_8] ~ sequence and sequencer, sequence hierarchy
7-6 铺设油井管道
抖音实战~项目关联UniCloud
还在用 SimpleDateFormat 做时间格式化?小心项目崩掉
Scala IO case
7-3 maximum sub segment and
Sword finger offer merges two sorted lists
R语言dplyr包select函数将dataframe数据中的指定数据列移动到dataframe数据列中的第一列(首列)
Continuous soul torture from two MySQL indexes of interviewers
js监听页面或元素scroll事件,滚动到底部或顶部
Latest development of jetpack compose
Scala IO serialization and deserialization