Hibernate学习3 - 自定义SQL

x33g5p2x  于2022-06-27 转载在 其他  
字(4.8k)|赞(0)|评价(0)|浏览(568)

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配置去掉

Query设置setFirstResult:起始坐标setMaxResults:截取数据个数

@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();

    }

相关文章