首页 > 数据库 >学习笔记——Mybatis动态SQL

学习笔记——Mybatis动态SQL

时间:2023-01-12 20:12:55浏览次数:34  
标签:salary mybatis 笔记 id employee SQL Mybatis email

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 %d{MM-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>

 

标签:salary,mybatis,笔记,id,employee,SQL,Mybatis,email
From: https://www.cnblogs.com/isDaHua/p/17045760.html

相关文章

  • Redis 6 学习笔记1 ——NoSQL数据库介绍,Redis常用数据类型
    NoSQL数据库介绍(了解)技术的分类1、解决功能性的问题:Java、Jsp、RDBMS、Tomcat、HTML、Linux、JDBC、SVN,2、进一步地,解决系统功能扩展性的问题:Struts、Spring、SpringMVC......
  • Windows Server 2008 R2安装Sqlserver 2008的步骤和设置跨网远程访问SQL server​
    WindowsServer2008R2安装Sqlserver2008的步骤和设置跨网远程访问SQLserver最近学习sql数据库,所以捣鼓一下安装sqlserver数据库的教程;​安装SQLServer2008R2需要.NE......
  • ASP.NET Core学习笔记3
    ASP.NETCore学习笔记3      结论:n AmbiguousHTTPmethodforaction,翻译后是“不明确的HTTP操作方法”。n 有可能是没写HTTP方法,如[HttpGet]、......
  • Math学习笔记
    最近几天全在做OI数论题,写个blog记一下套路。例如要求\(\operatornameg(n)=\sum_{d|n}d\cdot\varphi(\frac{n}{d})\)尽管你会一个叫做\(\text{LCMSUM}\)(可跳转)......
  • mybatis的主键自增以及如何获得自增
    转载:https://juejin.cn/post/7168638701784793124#comment获得插入后获得主键:https://juejin.cn/post/7121713604667113485......
  • ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost:3306' (10061)
    ERROR2003(HY000):Can'tconnecttoMySQLserveron'localhost:3306'(10061)报错原因:电脑之前有个5.0.2版本的mysql。忘记了并且版本太低,使用管理员身份执行mysqlr......
  • Windows中sqlmap搭建(问题)和使用
    sqlmap搭建本次是win11和python11注意:SQLmap使用python写的,所以需要提前搭建好python环境.点击​​https://sqlmap.org/​​进入SQLmap的官网.点击下载压缩包,......
  • MySQL中的COUNT(*)和COUNT(col)
    ​另一篇:differencebetweencount(1)andcount(*) 看看人们是如何使用COUNT(*)和COUNT(col)的,看起来大多数人都认为它们是同义词,只是使用他们喜欢的,而在性能甚至查询......
  • MySQL
    MySQL安装数据库Mysql5.7地址:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.19-winx64.zip如果安装过Mysql过程中,出错了scdeletemysql【删除已经安装......
  • Java项目生成电脑桌面快捷脚本(Mysql数据)
    一、场景说明在项目中,可能有些同事需要查询线上数据库的数据,但又不能泄露密码给他们,手写一个程序方便他们查询。二、Java代码需要引入mysql驱动包:downloads......