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

边栏推荐
- Huawei machine learning service speech recognition function enables applications to paint "sound" and color
- js监听页面或元素scroll事件,滚动到底部或顶部
- 安装IBM CPLEX学术版 academic edition | conda 安装 CPLEX
- Hello C (I) -- basics of C language
- QT display RGB data
- Websocket long link pressure test
- Chapter VI skills related to e-learning 5 (super parameter verification)
- 7-6 laying oil well pipeline
- 7-7 solving mode problems
- Modify stm32f030 clock source to internal crystal oscillator (HEI)
猜你喜欢

Go language pointer, value reference and pointer reference

还在用 SimpleDateFormat 做时间格式化?小心项目崩掉

2021-2022 China's financial digitalization "new" insight Industry Research Report

Still using simpledateformat for time formatting? Be careful of project collapse

Redis source code analysis skip list

Morris traversal

Continuous soul torture from two MySQL indexes of interviewers
![[JS] - [linked list - application] - learning notes](/img/e1/76d2a347b05212de349322f43e0b3a.png)
[JS] - [linked list - application] - learning notes

Selective sort method

Hydropower project construction scheme based on 3D GIS Development
随机推荐
372. chessboard coverage
去商场逛街
The R language uses the matchit package for propensity matching analysis and match The data function constructs the matched sample set, and performs Welch double sample t-test analysis and double inde
华为机器学习服务语音识别功能,让应用绘“声”绘色
Scala IO reads data from URLs and other data sources
7-9 treasure hunt route
Idea creation module prompt already exists
Modify stm32f030 clock source to internal crystal oscillator (HEI)
Ethernet ARP Protocol
Record a Webflux application memory leak troubleshooting
7-7 求解众数问题
376. 機器任務
Use of types, values, namespaces, combinations, etc. in typescript
Andersen Global借助巴勒斯坦成员公司加强中东平台
7-6 laying oil well pipeline
SAP PA certificate for no birds, which can be tested by new peers
R语言dplyr包select函数将dataframe数据中的指定数据列移动到dataframe数据列中的第一列(首列)
Design and practice of vivo server monitoring architecture
2021-2022 China's financial digitalization "new" insight Industry Research Report
Why is it that the "Zhongtai" that was originally eaten by civil engineering is no longer fragrant?