MyBatis--学习第二天

使用接口,MyBatis的执行流程

 

 

 搭建项目:

 

 

 mybatis核心配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--引入properties 属性配置文件 使用相对路径方式-->
    <properties resource="jdbc.properties"></properties>
    <!--设置日志的处理方式 默认 是log4j 也可以写出来-->
    <settings>
        <setting name="logImpl" value="LOG4J"/>
    </settings>
    <!--扫描实体类包 给所有实体类自动生成别名  生成的别名就是 类名首字母小写-->
    <typeAliases>
        <package name="com.bjsxt.entity"></package>
    </typeAliases>
    <!--environment 代表mybatis框的配置信息-->
    <environments default="mysql"> <!--指定默认使用哪个连接信息 如果指定的不存在 那么就使用第一个-->
        <environment id="mysql"><!--可以配置mysql连接信息-->
            <transactionManager type="JDBC"/><!--事务管理器-->
            <dataSource type="POOLED"><!--以连接池形式管理连接对象 连接池配置信息-->
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>

    </environments>
    <mappers>
       <!-- <mapper class="com.bjsxt.mapper.EmpMapper"></mapper>-->
        <package name="com.bjsxt.mapper"></package>
    </mappers>
</configuration>

注意:<package name="com.bjsxt.mapper"></package>

mybatis核心配置文件中扫描接口和映射文件

准备接口

 

 

 

import com.bjsxt.entity.Emp;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface EmpMapper {
    List<Emp> finAll();

    /**
     * 根据编号查询
     *
     * @param empno 要查找的员工编号
     * @return 根据员工查到的信息封装到Emp对象
     */
    Emp getEmpByEmpno(int empno);

    /**
     * @param empno 查询的员工编号
     * @param ename 查询的员工姓名
     * @return 根据员工姓名和编号返回的员工对象
     */
    Emp getEmpByEmpnoAndEname(int empno, String ename);

    /**
     * @param empno
     * @param ename
     * @return
     */
    Emp getEmpByEmpnoAndEname2(@Param("a") int empno, @Param("b") String ename);


    /**
     * @param emp
     * @return
     */
    Emp getEmpByEmpnoAndEname3(Emp emp);

    /**
     * @param empa
     * @param empb
     * @return
     */
    Emp getEmpByEmpnoAndEname4(@Param("a") Emp empa, @Param("b") Emp empb);
}

mapper映射文件内容

<?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">
<!--注意
这里放入的namespace从今天 开始不能随便写 必须是接口名
接口名和 mapper映射 文件名必须相同
-->
<mapper namespace="com.bjsxt.mapper.EmpMapper">
    <select id="finAll" resultType="emp">
        select * from emp
    </select>

    <select id="getEmpByEmpno" resultType="emp">
        select * from emp where empno=#{empno}
    </select>

    <select id="getEmpByEmpnoAndEname" resultType="emp">
        select * from emp where empno=#{0} and ename=#{1}
    </select>

    <select id="getEmpByEmpnoAndEname2" resultType="emp">
        select * from emp where empno=#{a} and ename=#{b}
    </select>

    <select id="getEmpByEmpnoAndEname3" resultType="emp">
         select * from emp where empno=#{empno} and ename=#{ename}
    </select>

    <select id="getEmpByEmpnoAndEname4" resultType="emp" parameterType="emp">
        select * from emp where empno=#{a.empno} and ename=#{b.ename}
    </select>
</mapper>

实体类,测试类:

 

 

 

 

 

 

package com.bjsxt.entity;

import java.util.Date;

/*
    对应数据库实体类的要求
    1所有属性必须是私有化的
    2每个属性必须有get和set方法
    3必须存在空参数构造方法
     */
public class Emp {
    private int empno;
    private String ename;
    private String job;
    private int mgr;
    private Date hiredate;
    private double sal;
    private double comm;
    private int deptno;

    public Emp() {
    }

    public Emp(int empno, String ename, String job, int mgr, Date hiredate, double sal, double comm, int deptno) {
        this.empno = empno;
        this.ename = ename;
        this.job = job;
        this.mgr = mgr;
        this.hiredate = hiredate;
        this.sal = sal;
        this.comm = comm;
        this.deptno = deptno;
    }

    public int getEmpno() {
        return empno;
    }

    public void setEmpno(int empno) {
        this.empno = empno;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public int getMgr() {
        return mgr;
    }

    public void setMgr(int mgr) {
        this.mgr = mgr;
    }

    public Date getHiredate() {
        return hiredate;
    }

    public void setHiredate(Date hiredate) {
        this.hiredate = hiredate;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public double getComm() {
        return comm;
    }

    public void setComm(double comm) {
        this.comm = comm;
    }

    public int getDeptno() {
        return deptno;
    }

    public void setDeptno(int deptno) {
        this.deptno = deptno;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "empno=" + empno +
                ", ename='" + ename + '\'' +
                ", job='" + job + '\'' +
                ", mgr=" + mgr +
                ", hiredate=" + hiredate +
                ", sal=" + sal +
                ", comm=" + comm +
                ", deptno=" + deptno +
                '}';
    }
}
package com.bjsxt.test;

import com.bjsxt.entity.Emp;
import com.bjsxt.mapper.EmpMapper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class Test {
    public static void main(String[] args) {
        SqlSession sqlSession = TestUtils.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> emps = mapper.finAll();
        for (Emp e : emps
        ) {
            System.out.println(e);
        }
        System.out.println("---------------------------------------");
        Emp empno = mapper.getEmpByEmpno(7566);
        System.out.println(empno);

        System.out.println("---------------------------------------");
        Emp martin = mapper.getEmpByEmpnoAndEname(7654, "MARTIN");
        System.out.println(mapper);

        System.out.println("---------------------------------------");
        Emp ename = mapper.getEmpByEmpnoAndEname(7654, "MARTIN");
        System.out.println(ename);

        System.out.println("---------------------------------------");
        Emp emp=new Emp();
        emp.setEmpno(7844);
        emp.setEname("TURNER");
        Emp ename3 = mapper.getEmpByEmpnoAndEname3(emp);
        System.out.println(ename3);

        System.out.println("---------------------------------------");
        Emp empa=new Emp();
        empa.setEmpno(7900);
        Emp empb=new Emp();
        empb.setEname("JAMES");
        Emp ename4 = mapper.getEmpByEmpnoAndEname4(empa, empb);
        System.out.println(ename4);

        sqlSession.close();
    }
}

代理模式:

注意命名问题
接口和映射 文件名必须相同
映射文件名中的namespace 必须是接口的全限定名
mapper映射文件中的id 必须是接口中的方法名

多参数

1。传入单个基础数据类型参数  基础数据类型=八种基本数据类型+String

package com.bjsxt.mapper;
import com.bjsxt.entity.Emp;
import java.util.List;
public interface EmpMapper {
    List<Emp> findAll();
    Emp getEmpByEmpno(int empno);
}

映射文件

<mapper namespace="com.bjsxt.mapper.EmpMapper">
    <!--id属性必须是接口中的方法名-->
    <select id="findAll" resultType="emp">
        SELECT * from emp
    </select>
    <!--单个基础数据类型参数 #中相当于形参列表的参数名
    可以随便写 但是尽量和接口中的形参 名保持一致-->
    <select id="getEmpByEmpno" resultType="emp">
        SELECT *  FROM   emp where empno =#{empno}
    </select>
</mapper>

2传入多个基础数据类型参数
#中要使用参数的索引(使用数组封装)
或者 param1 param2 ... paramN(使用map集合封装)

接口

import com.bjsxt.entity.Emp;
import java.util.List;
public interface EmpMapper {
    List<Emp> findAll();
    Emp getEmpByEmpno(int empno);
    Emp getEmpByEmpnoAndEname(int empno,String ename);
}

映射文件

<?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">
<!--注意
这里放入的namespace从今天 开始不能随便写 必须是接口名
接口名和 mapper映射 文件名必须相同
-->
<mapper namespace="com.bjsxt.mapper.EmpMapper">
    <!--id属性必须是接口中的方法名-->
    <select id="findAll" resultType="emp">
        SELECT * from emp
    </select>
    <!--单个基础数据类型参数 #中相当于形参列表的参数名
    可以随便写 但是尽量和接口中的形参 名保持一致-->
    <select id="getEmpByEmpno" resultType="emp">
        SELECT *  FROM   emp where empno =#{empno}
    </select>
    <!--
    多个基础数据类型参数 mybatis会将参数放入一个数组
    #中放的是参数列表中参数的索引
    索引从0开始
    -->
    <select id="getEmpByEmpnoAndEname" resultType="emp">
        <!--select * FROM   emp where empno = #{0} and ename= #{1}-->
        select * FROM   emp where empno = #{param1} and ename= #{param2}
    </select>
</mapper>

测试代码

EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = mapper.getEmpByEmpnoAndEname(7566,"JONES");
        System.out.println(emp);
        sqlSession.close();

3。传入多个基础数据类型参数 通脱@Param来指定 #中要使用参数的名字(使用map集合封装 相当于指定集合键名)

接口

package com.bjsxt.mapper;
import com.bjsxt.entity.Emp;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface EmpMapper {
    List<Emp> findAll();
    Emp getEmpByEmpno(int empno);
    Emp getEmpByEmpnoAndEname(int empno,String ename);
    Emp getEmpByEmpnoAndEname2(@Param("a") int empno,@Param("b") String ename);
}

映射文件

<select id="getEmpByEmpnoAndEname2" resultType="emp">
        
        select * FROM   emp where empno = #{a} and ename= #{b}
    </select>

测试代码

 EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = mapper.getEmpByEmpnoAndEname2(7566,"JONES");
        System.out.println(emp);
        sqlSession.close();

4。单个引用类型参数

接口

import com.bjsxt.entity.Emp;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface EmpMapper {
    List<Emp> findAll();
    Emp getEmpByEmpno(int empno);
    Emp getEmpByEmpnoAndEname(int empno,String ename);
    Emp getEmpByEmpnoAndEname2(@Param("a") int empno,@Param("b") String ename);
    Emp getEmpByEmpnoAndEname3(Emp emp);
}

映射文件

 <!--parameterType="emp" 可以不写  单个引用类型 # 中直接写属性名-->
    <select id="getEmpByEmpnoAndEname3" resultType="emp" parameterType="emp">
       select * FROM   emp where empno = #{empno} and ename= #{ename}
    </select>

测试代码

EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp =new Emp();
        emp.setEmpno(7566);
        emp.setEname("JONES");
        Emp res = mapper.getEmpByEmpnoAndEname3(emp);
        System.out.println(res);
        sqlSession.close();

5。多个引用类型参数(使用不多)
接口

import com.bjsxt.entity.Emp;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface EmpMapper {
    List<Emp> findAll();
    Emp getEmpByEmpno(int empno);
    Emp getEmpByEmpnoAndEname(int empno,String ename);
    Emp getEmpByEmpnoAndEname2(@Param("a") int empno,@Param("b") String ename);
    Emp getEmpByEmpnoAndEname3(Emp emp);
    Emp getEmpByEmpnoAndEname4(@Param("a") Emp empa,@Param("b") Emp empb);
}

映射文件

 <!--parameterType="emp" 可以不写 多个引用类型
    # 中使用param*.对象属性名
    或者使用 别名.属性名
    -->
    <select id="getEmpByEmpnoAndEname4" resultType="emp" parameterType="emp" >
        select * FROM   emp where empno = #{param1.empno} and ename= #{param2.ename}
        <!-- select * FROM   emp where empno = #{a.empno} and ename= #{b.ename} -->
    </select>

测试代码

EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp empa =new Emp();
        empa.setEmpno(7566);
        Emp empb =new Emp();
        empb.setEmpno(7566);
        empb.setEname("JONES");
        Emp res = mapper.getEmpByEmpnoAndEname4(empa,empb);
        System.out.println(res);
        sqlSession.close();

增删改实现

接口

public interface EmpMapperX {
    int deleteEmpByEmpno(int empno);
    int updateEmpByEmpno(Emp emp);
    int addEmp(Emp emp);
}

映射文件

<?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">
<!--注意
这里放入的namespace从今天 开始不能随便写 必须是接口名
接口名和 mapper映射 文件名必须相同
-->
<mapper namespace="com.bjsxt.mapper.EmpMapperX">
    <insert id="addEmp">
         insert into emp values (default,#{ename},#{job},#{mgr},#{hiredate},#{sal},#{comm},#{deptno})
    </insert>
    <update id="updateEmpByEmpno" parameterType="emp">
        update emp set
        ename =#{ename},
        job=#{job},
        mgr=#{mgr},
        hiredate=#{hiredate},
        sal=#{sal},
        comm=#{comm},
        deptno=#{deptno}
        where empno=#{empno}
    </update>
    <delete id="deleteEmpByEmpno" parameterType="emp">
         delete from emp where empno =#{empno}
    </delete>
</mapper>

测试类

public class TestX {
    public static void main(String[] args) {
        SqlSession sqlSession = TestUtil.getSqlSession();
        EmpMapperX ex=sqlSession.getMapper(EmpMapperX.class);
      /*  int i = ex.deleteEmpByEmpno(7369);
        System.out.println(i);*/
        /*Emp emp =new Emp();
        emp.setEmpno(7521);
        emp.setEname("XiaoMing");
        emp.setMgr(7839);
        emp.setJob("SALESMAN");
        emp.setHiredate(new Date());
        emp.setSal(2000);
        emp.setComm(200);
        emp.setDeptno(30);
        int i = ex.updateEmpByEmpno(emp);*/
        Emp emp =new Emp();
        emp.setEname("XiaoHong");
        emp.setMgr(7839);
        emp.setJob("SALESMAN");
        emp.setHiredate(new Date());
        emp.setSal(3000);
        emp.setComm(100);
        emp.setDeptno(10);
        int i = ex.addEmp(emp);
        System.out.println(i);
        sqlSession.commit();
        sqlSession.close();
    }

动态sql

借助的标签如下

if   where    choose    set    trim    foreach    bind    sql      include

1.if   where    choose    

接口

public interface EmpMapper2 {
    List<Emp> getEmpByConditionx(int empno,String ename);
    List<Emp> getEmpByCondition1(Emp emp);
    List<Emp> getEmpByCondition2(Emp emp);
    List<Emp> getEmpByCondition3(Emp emp);
}

 映射文件

<?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.bjsxt.mapper.EmpMapper2">
    <!--测试if语句  如果只有一个参数必须使用别名处理 但是一个参数还用动态sql么?-->
    <select id="getEmpByConditionx"  resultType="emp" >
        select  * from emp where 1=1
        <if test="param1 != 0">
            and empno =  #{param1}
        </if>
        <if test="param2 != null and param2 !=  ''">
            and ename =  #{param2}
        </if>
    </select>
    <!--测试if语句-->
    <select id="getEmpByCondition1"  resultType="emp" parameterType="emp">
        select * from emp WHERE 1=1
        <if test="empno != 0 ">
            and  empno =#{empno}
        </if>
        <if test="ename != null and  ename !='' ">
            and  ename =#{ename}
        </if>
        <if test="job != null and  job !='' ">
            and  job =#{job}
        </if>
        <if test="mgr != 0  ">
            and  mgr =#{mgr}
        </if>
        <if test="sal != 0    ">
            and  sal =#{sal}
        </if>
        <if test="comm != 0    ">
            and  comm = #{comm}
        </if>
        <if test="deptno != 0    ">
            and  deptno =#{deptno}
        </if>
    </select>
    <!--测试whereif-->
    <select id="getEmpByCondition2"  resultType="emp" parameterType="emp">
        select * from emp
        <where>
            <if test="empno != 0 ">
                and  empno =#{empno}
            </if>
            <if test="ename != null and  ename !='' ">
                and  ename =#{ename}
            </if>
            <if test="job != null and  job !='' ">
                and  job =#{job}
            </if>
            <if test="mgr != 0  ">
                and  mgr =#{mgr}
            </if>
            <if test="hiredate != null">
                                                                                                                                        hiredate = #{hiredate}
                                                                                                    </if>
            <if test="sal != 0    ">
                and  sal =#{sal}
            </if>
            <if test="comm != 0    ">
                and  comm = #{comm}
            </if>
            <if test="deptno != 0    ">
                and  deptno =#{deptno}
            </if>
        </where>
    </select>
    <!--测试whereif-->
    <select id="getEmpByCondition3"  resultType="emp" parameterType="emp">
        select * from emp
        <where>
            <choose>
                <when test="empno != 0 ">
                    and  empno =#{empno}
                </when>
                <when test="ename != null and  ename !='' ">
                    and  ename =#{ename}
                </when>
                <when test="job != null and  job !='' ">
                    and  job =#{job}
                </when>
                <when test="mgr != 0  ">
                    and  mgr =#{mgr}
                </when>
               <when test="hiredate != null">
                                                                                                                                        hiredate = #{hiredate}
                                                                                                     </when>
                <when test="sal != 0    ">
                    and  sal =#{sal}
                </when>
                <when test="comm != 0    ">
                    and  comm = #{comm}
                </when>
                <when test="deptno != 0    ">
                    and  deptno =#{deptno}
                </when>
                <otherwise>
                    1=1
                </otherwise>
            </choose>
        </where>
    </select>
</mapper>

测试代码

public class Test2 {
    public static void main(String[] args) {
        SqlSession sqlSession = TestUtil.getSqlSession();
        EmpMapper2 mapper = sqlSession.getMapper(EmpMapper2.class);
        Emp emp =new Emp();
        /*emp.setEname("XiaoMing");
        emp.setJob("SALESMAN");*/
       /* emp.setMgr(7839);*/
       /*emp.setDeptno(20);*/
       /*emp.setHiredate(new java.sql.Date(119,8,3));*/
        List<Emp> emps = mapper.findEmpByCondition(emp);
        for (Emp e:emps
             ) {
            System.out.println(e);
        }
    }

2. set   trim

接口

public interface EmpMapper3 {
    int updateEmpByEmpno(Emp emp);
    int updateEmpByEmpno2(Emp emp);
}

映射文件

<?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.bjsxt.mapper.EmpMapper3">
    <update id="updateEmpByEmpno" parameterType="emp">
        update emp
        <!-- set 只能用于修改语句
        自动处理 set关键字和 逗号 问题-->
        <set>
            <if test="ename != null and ename != ''">
                ename =#{ename},
            </if>
            <if test="job != null and ename != ''">
                job =#{job},
            </if>
            <if test="sal !=0 ">
                sal =#{sal},
            </if>
            <if test="mgr !=0 ">
                mgr =#{mgr},
            </if>
            <if test="comm !=0 ">
                comm =#{comm},
            </if>
            <if test="deptno !=0 ">
                deptno =#{deptno},
            </if>
            <if test="hiredate != null ">
                hiredate =#{hiredate},
            </if>
        </set>
        where empno =#{empno}
    </update>
    <!--
        prefix:增加前缀
        prefixOverrides:去除前缀
        suffix:增加后缀
        suffixOverrides:去除后缀
        trim 比set功能更强大
        set可以看做 是 set的一种特殊情况
    -->
    <update id="updateEmpByEmpno2" parameterType="emp">
        update emp
        <trim prefix="set"  suffixOverrides=",">
            <if test="ename != null and ename != ''">
                ename =#{ename},
            </if>
            <if test="job != null and ename != ''">
                job =#{job},
            </if>
            <if test="sal != 0 ">
                sal =#{sal},
            </if>
            <if test="mgr != 0 ">
                mgr =#{mgr},
            </if>
            <if test="comm != 0 ">
                comm =#{comm},
            </if>
            <if test="deptno !=0 ">
                deptno =#{deptno},
            </if>
            <if test="hiredate != null ">
                hiredate =#{hiredate},
            </if>
        </trim>
        where empno =#{empno}
    </update>
</mapper>

测试类

 public static void main(String[] args) {
        SqlSession sqlSession = TestUtil.getSqlSession();
        EmpMapper3 m = sqlSession.getMapper(EmpMapper3.class);
        Emp emp =new Emp();
        emp.setEmpno(7566);
        emp.setEname("SUN");
        emp.setComm(1000);
        emp.setDeptno(30);
        emp.setJob("CLERK");
        System.out.println(new java.sql.Date(119,5,8));
        emp.setHiredate(date);
        int i = m.updateEmpByEmpno2(emp);
        System.out.println(i);
        sqlSession.commit();
        sqlSession.close();
    }

3. foreach   bind   include

接口

 public List<Emp> getEmpsByDeptnos(List<Integer> deptnos);
    public List<Emp> getEmpsByDeptnos2(int  ... deptnos);
    /*因为参数只有一个映射文件中使用 param1 或者使用@Param 定义别名*/
    List<Emp> getEmpsByEnameLike(@Param("ename") String  ename);

映射文件

<?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.bjsxt.mapper.EmpMapper4">
    <sql id="baseEmpSelect">
        select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp
    </sql>
    <!--查看API文档 看foreach集合和数组作为方法参数时候的处理
    map.put("list",数组或者集合)
    -->
    <select id="getEmpsByDeptnos" resultType="emp">
        <include refid="baseEmpSelect"/> where deptno IN
        <foreach collection="list" item="deptno" open="(" close=")" separator=",">
            #{deptno}
        </foreach>
    </select>
    <select id="getEmpsByDeptnos2" resultType="emp">
        <include refid="baseEmpSelect"/> where deptno IN
        <foreach collection="array" item="deptno" open="(" close=")" separator=",">
            #{deptno}
        </foreach>
    </select>
    <!--参数只有一个 可以使用param1 或者在接口上的别名-->
    <select id="getEmpsByEnameLike"  resultType="emp">
        <bind name="likePattern" value="'%'+param1+'%'"></bind>
        <include refid="baseEmpSelect"/>
        where ename like #{likePattern}
    </select>
</mapper>

测试类

public static void main(String[] args) {
        SqlSession sqlSession = TestUtil.getSqlSession();
        EmpMapper4 m = sqlSession.getMapper(EmpMapper4.class);
        /*List<Integer> deptnos  =new LinkedList<Integer>();
        deptnos.add(10);
        deptnos.add(20);
        List<Emp> emps = m.getEmpsByDeptnos(deptnos);*/
        int[] deptnos ={10,20};
        List<Emp> emps = m.getEmpsByDeptnos2(deptnos);
        for (Emp emp:emps
             ) {
            System.out.println(emp);
        }
        emps = m.getEmpsByEnameLike("s");
        for (Emp emp:emps
                ) {
            System.out.println(emp);
        }
        sqlSession.close();
    }

MyBatis工具类

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 java.io.IOException;
import java.io.InputStream;

public class TestUtils {
    public static SqlSession getSqlSession() {
        SqlSession sqlSession = null;
        try {
            InputStream is = Resources.getResourceAsStream("mybatis.xml");
            SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
            SqlSessionFactory sqlSessionFactory = factoryBuilder.build(is);
            sqlSession = sqlSessionFactory.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sqlSession;
    }
}

 

动态SQL总结
if
where
处理where语句


choose when otherwise 多条件分支 多选一


set 更新语句中处理 set关键字 和, 问题


trim 补充前缀 覆盖指定前缀 补充后缀 覆盖指定后缀


foreach 遍历数组或者 集合


foreach collection list array open 以什么开始 close 以什么结束 seprator 中间以什么分隔 item 中间临时变量的名字


bind 对传过来的参数 先做一个格式的处理 然后再在SQL语句中使用 name 定义引用名 value 定义处理数据的模板


sql 定义多个SQL语句中共同的部分 include在其他SQL语句中 引用SQL片段

转载于:https://www.cnblogs.com/zimo-bwl1029-s/p/11455226.html

mybatis-plus报空指针异常可能是因为在普通类调用Mapper接口时,使用了mybatis-plus的功能,但在泛型类中找不到id属性,导致查找到的值为空。这会引发空指针异常(NullPointerException)。为了解决这个问题,可以通过深入学习MyBatis并了解其核心对象、配置文件和映射文件的使用来修复。确保在泛型类中定义了正确的id属性,并正确配置映射文件中的语句映射。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringBoot:在普通类调用 Mapper 接口使用 MybatisPlus 报错:空指针异常(NullPointerException)](https://blog.csdn.net/weixin_43642521/article/details/124491271)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [MyBatis-plus使用出现空指针异常或者表不纯在](https://blog.csdn.net/gtq1061414470/article/details/121019792)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [SSM框架的学习与应用JavaEE(第二天Mybatis的深入学习](https://download.csdn.net/download/m0_53659738/88241458)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值