学习笔记——mybatis动态sql(代码片段)

isDaHua isDaHua     2023-01-13     730

关键词:

2023-01-12

一、Mybatis动态SQL

即将SQL动态化

同时Mybatis的动态SQL支持OFNL表达式,OGNL(Object Graph Navigation Language)对象图导航语言。

1、先搭建环境

(1)创建一个“maven”模块,命名为“day04_mybatis”

(2)在“day04_mybatis”中的“pom.xml”中的<project>标签内部添加依赖,即添加jar包

<dependencies>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.26</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>


    </dependencies>

(3)在"day04_mybatis.src.main.resources"中添加"db.properties(连接数据库,里面的形式是:key=value),需要设置4个值(driver、url、username、password)"和“log4j.xml”

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
        <param name="Encoding" value="UTF-8" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%5p %dMM-dd HH:mm:ss,SSS %m (%F:%L) \\n" />
        </layout>
    </appender>

    <logger name="java.sql">
        <level value="debug"/>
    </logger>

    <logger name="org.apache.ibatis">
        <level value="info"/>
    </logger>

    <root>
        <level value="debug" />
        <appender-ref ref="STDOUT" />
    </root>

</log4j:configuration>

(4)在“day04_mybatis.src.resources”中添加“mybatis-config.xml”

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--    设置属性-->
    <properties resource="db.properties"></properties>

    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!--        延迟加载-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!--        延迟加载的属性-->
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>

    <typeAliases>
        <package name="com.hh.mybatis.pojo"/>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="$db.driver"/>
                <property name="url" value="$db.url"/>
                <property name="username" value="$db.username"/>
                <property name="password" value="$db.password"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="mapper/EmployeeMapper.xml"></mapper>
    </mappers>
</configuration>

(5)在“day04_mybatis.src.main.java”中创建“com.hh.mybatis.pojo”和“com.hh.mybatis.mapper”。之后在“day04_mybatis.src.main.resources”中创建“mapper”文件夹,在“mapper”文件夹下创建“EmployeeMapper.xml”

(6)在“pojo”文件夹下创建“Dept”、“Employee”。在“mapper”文件夹下创建“EmployeeMapper”接口并添加代码

public interface EmployeeMapper 
    /**
     * 按条件查询员工信息(条件不确定)
     * @return
     */
    public List<Employee> selectEmpByOpr(Employee employee);

(7)EmployeeMapper.xml中的映射文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hh.mybatis.mapper.EmployeeMapper" >
<!-- 按条件查询员工信息(条件不确定)-->
    <select id="selectEmpByOpr" resultType="employee">
        select
            id,
            last_name,
            email,
            salary,
            dept_id,
        from
            tbl_employee
    </select>
</mapper>

(8)在“day04_mybatis.src.test.java"中创建测试类“TestDynamicSql”

public class TestDynamicSql 
    @Test
    public void testDynamicSql() throws Exception
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);

        Employee employee = new Employee();
        List<Employee> employees = employeeMapper.selectEmpByOpr(employee);
        for (Employee employee1 : employees) 
            System.out.println("employee1 = " + employee1);
        
    

(9)注意:

如果出现“java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corres”这种问题报错时,可以将sql语句先在“SQLyog”中先写一遍,运行,如果运行无误,之后将代码再放入“映射文件”中,即可

2、进行动态SQL设置

(1)注意:在动态SQL中“test”中放置的是“属性”

(2)动态SQL<if>标签

用于完成简单的判断

<select id="selectEmpByOpr" resultType="employee">
        SELECT
            id,
            last_name,
            email,
            salary
        FROM
            tbl_employee
        WHERE
            <if test="id != null">
                id = #id
            </if>
            <if test="lastName != null">
                and last_name = #lastName
            </if>
            <if test="email != null">
                and email = #email
            </if>
            <if test="salary != null">
                and salary = #salary
            </if>
    </select>

测试类

public class TestDynamicSql 
    @Test
    public void testDynamicSql() throws Exception
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);

        Employee employee = new Employee();
        //动态标签
        employee.setId(1);
//        employee.setSalary(50.0);
        List<Employee> employees = employeeMapper.selectEmpByOpr(employee);
        for (Employee employee1 : employees) 
            System.out.println("employee1 = " + employee1);
        
    

注意:动态参数中<if>,无参数时报错,没有第一个参数也报错。

(3)动态SQL:<where>标签

where用于解决SQL语句中where关键字以及条件前面的and或者or的问题

<select id="selectEmpByOpr" resultType="employee">
        SELECT
            id,
            last_name,
            email,
            salary
        FROM
            tbl_employee
        <where>
            <if test="id != null">
                and id = #id
            </if>
            <if test="lastName != null">
                and last_name = #lastName
            </if>
            <if test="email != null">
                and email = #email
            </if>
            <if test="salary != null">
                and salary = #salary
            </if>
        </where>
    </select>

(3)动态SQL:<trim>

可以在条件判断完的SQL语句前后添加或者去掉指定的字符

标签中的属性:

①prefix:添加前缀

②prefixOverrides:去掉前缀

③suffix:添加后缀

④suffixOverrides:去掉后缀

例如:

    <select id="selectEmpByOprTrim" resultType="employee">
         SELECT
            id,
            last_name,
            email,
            salary
        FROM
            tbl_employee
        <trim prefix="where" suffixOverrides="and">
            <if test="id != null">
                 id = #id and
            </if>
            <if test="lastName != null">
                last_name = #lastName and
            </if>
            <if test="email != null">
                email = #email and
            </if>
            <if test="salary != null">
                salary = #salary
            </if>
        </trim>
    </select>

(4)动态SQL:<set>

用于解决修改操作中SQL语句中可能多出逗号的问题

在“EmployeeMapper”接口中添加方法

/**
     * 按条件修改员工信息(条件不确定)
     * @param employee
     */
    public void updateEmpByOpr(Employee employee);

映射文件

<update id="updateEmpByOpr">
        update
            tbl_employee
        <set>
            <if test="lastName != null">
                last_name = #lastName,
            </if>
            <if test="email != null">
                email = #email,
            </if>
            <if test="salary != null">
                salary = #salary
            </if>
        </set>
        where
           id = #id
</update>

(5)动态SQL标签:<choose>

类似于java中if-else(switch-case)结构

    <update id="updateEmpByOneOpr">
        SELECT
            id,
            last_name,
            email,
            salary
        FROM
            tbl_employee
        <where>
             <choose>
                 <when test="id != null">
                     id = #id
                 </when>
                 <when test="email != null">
                     email = #email
                 </when>
                 <when test="salary != null">
                     salary = #salary
                 </when>
                 <otherwise>
                     1=1
                 </otherwise>
             </choose>
        </where>
    </update>

(6)动态SQL标签:<foreach>

类似于java中的for循环

collection:要迭代的集合

item:当前从集合中迭代出的元素

separator:元素与元素之间的分隔符

①“EmployeeMapper”接口中的函数

/**
     * 通过多个id获取员工信息
     * @param ids
     * @return
     */
    public List<Employee> selectEmpByIds(@Param("ids") List<Integer> ids);

②映射文件

<select id="selectEmpByIds" resultType="employee">
        SELECT
            id,
            last_name,
            email,
            salary
        FROM
            tbl_employee
        <where>
            id in
            (
            <foreach collection="ids" item="id" separator=",">
                #id
            </foreach>
            )
        </where>
    </select>

 ③测试主要代码

List<Integer> ids = new ArrayList<>();
        ids.add(1);
        ids.add(3);
        ids.add(4);
        List<Employee> employees = employeeMapper.selectEmpByIds(ids);
        for (Employee employee1 : employees) 
            System.out.println("employee1 = " + employee1);
        

(7)sql标签

提取可重用SQL片段

    <sql id="select_employee">
        SELECT
            id,
            last_name,
            email,
            salary
        FROM
            tbl_employee
    </sql>
    
    <sql id="emp_col">
        id,
        last_name,
        email,
        salary
    </sql>
<!-- 按条件查询员工信息(条件不确定)-->
    <select id="selectEmpByOpr" resultType="employee">
        select
            <include refid="emp_col"></include>
        from
            tbl_employee
        <where>
            <if test="id != null">
                and id = #id
            </if>
            <if test="lastName != null">
                and last_name = #lastName
            </if>
            <if test="email != null">
                and email = #email
            </if>
            <if test="salary != null">
                and salary = #salary
            </if>
        </where>
    </select>

 

mybatis框架学习笔记---[动态sql的使用](代码片段)

mybatis3版本的文档-->mybatis文档写在前面Ok,也是进行到动态sql这部分了;需要说明的是,后来我又给员工表添加了一个记录年龄的列,当然在员工类中也添加了属性以及它的getset方法;构造方法;toString();这些也都加上这个属性了.ml1.if... 查看详情

mybatis学习笔记-06(代码片段)

mybatis学习笔记-061.动态SQL环境搭建1.1、实体类1.2、BlogMapper接口1.3、BlogMapper.xml配置文件1.4、mybatis-config.xml核心配置文件1.5、唯一的id标识(Idutil)1.6、测试2、动态SQL之if语句3、choose、when、otherwise4、trimwhereset5、forEach这篇... 查看详情

mybatis学习笔记-06(代码片段)

mybatis学习笔记-061.动态SQL环境搭建1.1、实体类1.2、BlogMapper接口1.3、BlogMapper.xml配置文件1.4、mybatis-config.xml核心配置文件1.5、唯一的id标识(Idutil)1.6、测试2、动态SQL之if语句3、choose、when、otherwise4、trimwhereset5、forEach这篇... 查看详情

mybatis学习笔记-06(代码片段)

mybatis学习笔记-061.动态SQL环境搭建1.1、实体类1.2、BlogMapper接口1.3、BlogMapper.xml配置文件1.4、mybatis-config.xml核心配置文件1.5、唯一的id标识(Idutil)1.6、测试2、动态SQL之if语句3、choose、when、otherwise4、trimwhereset5、forEach这篇... 查看详情

mybatis-动态sql语句-if用法——mysql系列学习笔记(代码片段)

select根据传入的参数来决定:如果用户名不为空,则对用户名进行模糊查询,如果邮箱不为空,则以邮箱为查询条件<selectid="selectByUser"parameterType="com.tgb.mybatis.entity.SysUser"resultType=" 查看详情

mybatis-动态sql语句-if用法——mysql系列学习笔记(代码片段)

select根据传入的参数来决定:如果用户名不为空,则对用户名进行模糊查询,如果邮箱不为空,则以邮箱为查询条件<selectid="selectByUser"parameterType="com.tgb.mybatis.entity.SysUser"resultType=" 查看详情

mybatis学习笔记-动态sql

Mybatis学习笔记(三)-动态SQL动态SQL语句,就是在SQL中执行动态构建的SQL语句,根据不同情况执行不同的SQL语句表属性IDNAMEEMAILAGE第一的接口方法publicinterfaceStudentDAO{//测试动态sqlList<StudentModle>STUDENT_MODLE_LIST(StudentModlestudentModle... 查看详情

mybatis学习笔记——动态sql(代码片段)

10、动态SQLMyBatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是为了解决拼接SQL语句字符串的痛点问题。动态SQL:1、if标签:通过test属性中的表达式判断标签中的内容是否有效(是... 查看详情

mybatis动态sql操作之插入学习笔记

1importjava.util.ArrayList;importjava.util.List;importorg.apache.ibatis.session.SqlSession;importcn.itcast.javaee.mybatis.util.MybatisUtil;/***持久层*@authorAdminTC*/publicclassStudentDao{/***插入学生*/publi 查看详情

《深入浅出mybatis技术原理与实战》读书笔记-动态sql(代码片段)

MyBatis动态SQL这里介绍几个基本的元素,十分简单明了。大量的判断都可以在MyBatis的映射XML文件里面配置1.if元素if元素是我们常用的判断语句。常常与test属性联合使用。将参数roleName传入到映射器中,对roleName进行模糊查... 查看详情

javaweb学习笔记之mybatis实用sql语句汇总(代码片段)

一、批量插入/更新/删除批量操作主要使用的是Mybatis的foreach,遍历参数列表执行相应的操作,所以批量插入/更新/删除的写法是类似的,只是SQL略有区别而已。MySql批量操作需要数据库连接配置allowMultiQueries=true才... 查看详情

mybatis学习总结——多表关联查询与动态sql(代码片段)

一、多表关联查询1.1、一对一关系1.1.1、执行环境假定一个员工(emp)拥有一个登录用户(user),员工与用户表之间是一对一关系: 用户表:员工表:SQL:SETFOREIGN_KEY_CHECKS=0;--------------------------------Tablestructurefor`user`-----------... 查看详情

mybatis学习笔记(代码片段)

ORM框架ORM(ObjectRelationalMapping),即对象关系映射。在面向对象编程语言中,将关系型数据库中的数据与对象建立起映射关系,进而自动的完成数据与对象的互相转换:将输入数据(即传入对象)... 查看详情

mybatis学习笔记(代码片段)

环境:JDK1.8Mysql5.7(Mysql8.0)maven3.6.1IDEA回顾:JDBCMysqlJava基础MavenJunitSSM框架:配置文件的。最好的方式:看官方文档1、简介Mybatis是一款优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。Myb... 查看详情

mybatis学习笔记——特殊sql的执行(代码片段)

7、特殊SQL的执行7.1、模糊查询模糊查询的三种方式:方式1:select*fromt_userwhereusernamelike‘%$mohu%’方式2:select*fromt_userwhereusernamelikeconcat(‘%’,#mohu,‘%’)方式3:select*fromt_userwhereusernamelike“%”#mohu“%”SpecialSQLMapper.... 查看详情

mybatis学习笔记-05(代码片段)

mybatis学习笔记-051、复杂环境搭建步骤2、多对一处理2.1、实体类2.2、按照查询嵌套处理2.3、按照结果嵌套处理3、一对多处理3.1、实体类3.2、按结果嵌套查询3.3、按查询嵌套处理4、区别这一篇来写一些相对复杂的SQL语句。1、复杂... 查看详情

mybatis学习笔记-05(代码片段)

mybatis学习笔记-051、复杂环境搭建步骤2、多对一处理2.1、实体类2.2、按照查询嵌套处理2.3、按照结果嵌套处理3、一对多处理3.1、实体类3.2、按结果嵌套查询3.3、按查询嵌套处理4、区别这一篇来写一些相对复杂的SQL语句。1、复杂... 查看详情

mybatis学习笔记(代码片段)

文章目录1.MyBatis简介1.1MyBatis历史1.2MyBatis特性1.3和其它持久化层技术对比2.搭建MyBatis2.1开发环境2.2创建maven工程2.3创建MyBatis的核心配置文件2.4创建mapper接口2.5创建MyBatis的映射文件2.6通过junit测试功能2.7加入log4j日志功能3.核心配置... 查看详情