当前位置:网站首页>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();
}

边栏推荐
- Tiktok practice ~ upload and release app video
- 点的螺旋距离
- R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用exp函数、confint函数、coef函数获取模型中每个变量(自变量改变一个单位)对应的优势比的置信区间
- The living standards of ordinary people
- 372. chessboard coverage
- Idea creation module prompt already exists
- 2021-2022 China's financial digitalization "new" insight Industry Research Report
- 379. hide and seek
- 7-3 maximum sub segment and
- Simple use of libnum Library (hexadecimal string conversion)
猜你喜欢

Why is it that the "Zhongtai" that was originally eaten by civil engineering is no longer fragrant?

企业数据防泄露解决方案分享

选择类排序法

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

Tiktok practice ~ upload and release app video

How to use stm32subeide SWV function

Morris遍曆

Hello C (III) - pointer

Huawei machine learning service speech recognition function enables applications to paint "sound" and color

一文理解OpenStack网络
随机推荐
Volcano becomes spark default batch scheduler
Design and practice of vivo server monitoring architecture
Installing IBM CPLEX academic edition | CONDA installing CPLEX
都2022年了,你还不了解什么是性能测试?
The living standards of ordinary people
Assessment of public finance of Jilin University in March of the 22nd spring -00091
抖音实战~项目关联UniCloud
Sword finger offer merges two sorted lists
R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses exp function and coef function to obtain the corresponding odds rat
R语言使用MatchIt包进行倾向性匹配分析、使用match.data函数构建匹配后的样本集合、对匹配后的样本的不同分组对应的目标变量的均值进行Welch双样本t检验分析、双独立样本t检验
js监听页面或元素scroll事件,滚动到底部或顶部
7-2 求解买股票问题
Hello C (two) -- use of bit operation
R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用exp函数和coef函数获取模型中每个变量(自变量改变一个单位)对应的优势比(odds ratio)
如何化解35岁危机?华为云数据库首席架构师20年技术经验分享
2021-2022中国金融数字化“新”洞察行业研究报告
Start QT program
QT display RGB data
Using ADC to control brushless motor source program STM32 library function
7-8 循环日程安排问题