MyBatis mapping file (xxxMapper.xml), log4j.properties (where, if, foreach, sql, include)

Insert picture description here

Insert picture description here
Let's talk about the last four:

1.if、where

Insert picture description here
Insert picture description here
Insert picture description here

2.foreach

Insert picture description here

Insert picture description here
Insert picture description here

3.sql、include

Insert picture description hereInsert picture description here

Added log4j.properties: (import coordinates in pom.xml first)

   <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

log4j.properties:

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{
    
    ABSOLUTE} %5p %c{
    
    1}:%L - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c:/mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{
    
    ABSOLUTE} %5p %c{
    
    1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.rootLogger=debug, stdout

com.itheima.mapper/UserMapper.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.UserMapper">
    <!--抽取sql片段简化编写-->
    <sql id="selectSys_user">SELECT * FROM sys_user</sql>
    
    
    <!--动态sql,演示if,用了sqlMapConfig的别名。代替SELECT * FROM sys_user where 1=1 and id=#{
    
    id} and username=#{
    
    username} and password=#{
    
    password};-->
    <select id="findByCondition" resultType="user" parameterType="user">
        /*SELECT * FROM sys_user*/
        <include refid="selectSys_user"></include>
        <where>
            <if test="id!=0">
                and id=#{
    
    id}
            </if>

            <if test="username!=null">
                and username=#{
    
    username}
            </if>

            <if test="password!=null">
                and password=#{
    
    password}
            </if>
        </where>
    </select>

    <!--演示foreach,主要代替多个条件满足一个就行 SELECT * FROM USER WHERE id IN (1,2,5)-->
    <select id="findByids" resultType="user" parameterType="list">
        /*SELECT * FROM sys_user*/
        <include refid="selectSys_user"></include>
        <where>
            <foreach collection="list" open="id in(" close=")" item="id" separator=",">
                #{
    
    id}
            </foreach>
        </where>
    </select>




</mapper>

com.itheima.service.ServiceDemo:

package com.itheima.service;

import com.itheima.domain.Sys_user;
import com.itheima.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author QLBF
 * @version 1.0
 * @date 2021/1/30 11:07
 */
public class ServiceDemo {
    
    

    @Test
    //条件查询动态sql,演示if
    public void test1() throws IOException {
    
    
        //模拟
        Sys_user sys_user=new Sys_user();
        sys_user.setId(2);
        //sys_user.setUsername("wangwu");
        //sys_user.setPassword("123");
        InputStream resourceAsStream= Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession=sqlSessionFactory.openSession(true);

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<Sys_user> sys_userList = mapper.findByCondition(sys_user);
        System.out.println(sys_userList);
    }




    @Test
    //演示foreach
    public void test3() throws IOException {
    
    

        InputStream resourceAsStream= Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession=sqlSessionFactory.openSession(true);

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //模拟ids数据
        List<Integer> ids=new ArrayList<Integer>();
        ids.add(2);
        ids.add(7);

        List<Sys_user> sys_userList  = mapper.findByids(ids);
        System.out.println(sys_userList);
    }
}

Other codes are basically the same as the previous one: https://blog.csdn.net/GLOAL_COOK/article/details/113414561
Insert picture description here

Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/GLOAL_COOK/article/details/113444347