MyBatis:动态SQL查询

一、动态SQL

1.创建student表

在这里插入图片描述

2.插入数据

在这里插入图片描述

3.创建工程

下面展示一些 pom.xml代码片

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>chapter04</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- Mybatis核心 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.5</version>
        </dependency>

        <!-- junit测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- MySQL驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.27</version>
        </dependency>
    </dependencies>
</project>

在这里插入图片描述

4.创建POJO类

在这里插入图片描述

5.创建mybatis-config.xml配置文件

下面展示一些 代码片

<?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>
    <!--    配置别名-->
    <typeAliases>
        <package name="com.qfedu.pojo"/>
    </typeAliases>
    <!--设置连接数据库的环境-->
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url"
                          value="jdbc:mysql://localhost:3306/chapter04"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <!--引入映射文件-->
    <mappers>
        <mapper resource="com.qfedu.mapper/StudentMapper.xml"></mapper>
    </mappers>
</configuration>


在这里插入图片描述

6.创建StudentMapper.xml映射文件

(1)< if >元素

下面展示一些 代码片

<?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="student">
    <select id="findStudentBySnameAndCourse" parameterType="student" resultType="student">
            select * from student where sname = #{sname}
           <!--根据条件动态拼装SQL语句-->
           <if test="null!=course and ''!=course"> and course = #{course}</if>
    </select>
</mapper>

在这里插入图片描述

(2)< when >< choose >< otherwise >元素

下面展示一些 代码片

代码中注释掉的内容记得恢复或者删除一下,比对着结果图看

<?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="student">
    <select id="findStudentBySnameAndCourse" parameterType="student" resultType="student">
            select * from student where sname = #{sname}
           <!--根据条件动态拼装SQL语句-->
           <if test="null!=course and ''!=course"> and course = #{course}</if>
    </select>
    <select id="findStudentByChoose" parameterType="student" resultType="student">
<!--        select * from student where 1 = 1-->
<!--        <choose>-->
<!--            &lt;!&ndash;如果sid不为null或者空字符串&ndash;&gt;-->
<!--            <when test="null != sid and '' != sid">-->
<!--                and sid = #{sid}-->
<!--            </when>-->
<!--             &lt;!&ndash;如果sname不为null或者空字符串&ndash;&gt;-->
<!--            <when test="null != sname and '' != sname">-->
<!--                and sname like '%${sname}%'-->
<!--            </when>-->
<!--            &lt;!&ndash;如果上面不满足,则执行下面的&ndash;&gt;-->
<!--            <otherwise>-->
<!--                and course = 'Java'-->
<!--            </otherwise>-->
<!--        </choose>-->
        select * from student
<!--        <where>-->
<!--            <if test="null != sid and '' != sid">-->
<!--                and sid  = #{sid}-->
<!--            </if>-->
<!--            <if test="null != sname and '' != sname">-->
<!--                and sanme like '%${sname}%'-->
<!--            </if>-->
<!--            <if test="null == course">-->
<!--                and course = 'Java'-->
<!--            </if>-->
<!--        </where>-->
        <trim prefix="where" prefixOverrides="and">
            <if test="null != sid and '' != sid">
                and sid  = #{sid}
            </if>
            <if test="null != sname and '' != sname">
                and sanme like '%${sname}%'
            </if>
            <if test="null == course">
                and course = 'Java'
            </if>
        </trim>
    </select>
    <update id="updateStudent" parameterType="student">
        update student
<!--        <set>-->
<!--            <if test="null != sname and '' != sname">-->
<!--                 sname = #{sname},-->
<!--            </if>-->
<!--            <if test="null != age and '' != age">-->
<!--                 age = #{age},-->
<!--            </if>-->
<!--        </set>-->
<!--        where sid = #{sid}-->
        <trim prefix="set" suffixOverrides=",">
            <if test="null != sname and '' != sname">
                sname = #{sname},
            </if>
            <if test="null != age and '' != age">
                age = #{age},
            </if>
        </trim>
        where sid = #{sid}
    </update>

    <select id="findStudentByForeach" resultType="student">
        select * from student where sid in
        <foreach collection="list" item="sid" index="index" open="(" separator="," close=")">
            #{sid}
        </foreach>
    </select>
    <select id="findStudentByBind" resultType="student" parameterType="student">
        select * from student where
        <bind name="sname_pattern" value="'%'+sname+'%'"></bind>
        <if test="null != sname_pattern and '' != sname_pattern ">
            sname like #{sname_pattern}
        </if>
    </select>
</mapper>

在这里插入图片描述

(3)< where >元素

下面展示一些 代码片

<?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="student">
    <select id="findStudentBySnameAndCourse" parameterType="student" resultType="student">
            select * from student where sname = #{sname}
           <!--根据条件动态拼装SQL语句-->
           <if test="null!=course and ''!=course"> and course = #{course}</if>
    </select>
    <select id="findStudentByChoose" parameterType="student" resultType="student">
<!--        select * from student where 1 = 1-->
<!--        <choose>-->
<!--            &lt;!&ndash;如果sid不为null或者空字符串&ndash;&gt;-->
<!--            <when test="null != sid and '' != sid">-->
<!--                and sid = #{sid}-->
<!--            </when>-->
<!--             &lt;!&ndash;如果sname不为null或者空字符串&ndash;&gt;-->
<!--            <when test="null != sname and '' != sname">-->
<!--                and sname like '%${sname}%'-->
<!--            </when>-->
<!--            &lt;!&ndash;如果上面不满足,则执行下面的&ndash;&gt;-->
<!--            <otherwise>-->
<!--                and course = 'Java'-->
<!--            </otherwise>-->
<!--        </choose>-->
        select * from student
<!--        <where>-->
<!--            <if test="null != sid and '' != sid">-->
<!--                and sid  = #{sid}-->
<!--            </if>-->
<!--            <if test="null != sname and '' != sname">-->
<!--                and sanme like '%${sname}%'-->
<!--            </if>-->
<!--            <if test="null == course">-->
<!--                and course = 'Java'-->
<!--            </if>-->
<!--        </where>-->
        <trim prefix="where" prefixOverrides="and">
            <if test="null != sid and '' != sid">
                and sid  = #{sid}
            </if>
            <if test="null != sname and '' != sname">
                and sanme like '%${sname}%'
            </if>
            <if test="null == course">
                and course = 'Java'
            </if>
        </trim>
    </select>
    <update id="updateStudent" parameterType="student">
        update student
<!--        <set>-->
<!--            <if test="null != sname and '' != sname">-->
<!--                 sname = #{sname},-->
<!--            </if>-->
<!--            <if test="null != age and '' != age">-->
<!--                 age = #{age},-->
<!--            </if>-->
<!--        </set>-->
<!--        where sid = #{sid}-->
        <trim prefix="set" suffixOverrides=",">
            <if test="null != sname and '' != sname">
                sname = #{sname},
            </if>
            <if test="null != age and '' != age">
                age = #{age},
            </if>
        </trim>
        where sid = #{sid}
    </update>

    <select id="findStudentByForeach" resultType="student">
        select * from student where sid in
        <foreach collection="list" item="sid" index="index" open="(" separator="," close=")">
            #{sid}
        </foreach>
    </select>
    <select id="findStudentByBind" resultType="student" parameterType="student">
        select * from student where
        <bind name="sname_pattern" value="'%'+sname+'%'"></bind>
        <if test="null != sname_pattern and '' != sname_pattern ">
            sname like #{sname_pattern}
        </if>
    </select>
</mapper>

在这里插入图片描述

(4)< set >元素

下面展示一些 代码片

<?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="student">
    <select id="findStudentBySnameAndCourse" parameterType="student" resultType="student">
            select * from student where sname = #{sname}
           <!--根据条件动态拼装SQL语句-->
           <if test="null!=course and ''!=course"> and course = #{course}</if>
    </select>
    <select id="findStudentByChoose" parameterType="student" resultType="student">
<!--        select * from student where 1 = 1-->
<!--        <choose>-->
<!--            &lt;!&ndash;如果sid不为null或者空字符串&ndash;&gt;-->
<!--            <when test="null != sid and '' != sid">-->
<!--                and sid = #{sid}-->
<!--            </when>-->
<!--             &lt;!&ndash;如果sname不为null或者空字符串&ndash;&gt;-->
<!--            <when test="null != sname and '' != sname">-->
<!--                and sname like '%${sname}%'-->
<!--            </when>-->
<!--            &lt;!&ndash;如果上面不满足,则执行下面的&ndash;&gt;-->
<!--            <otherwise>-->
<!--                and course = 'Java'-->
<!--            </otherwise>-->
<!--        </choose>-->
        select * from student
<!--        <where>-->
<!--            <if test="null != sid and '' != sid">-->
<!--                and sid  = #{sid}-->
<!--            </if>-->
<!--            <if test="null != sname and '' != sname">-->
<!--                and sanme like '%${sname}%'-->
<!--            </if>-->
<!--            <if test="null == course">-->
<!--                and course = 'Java'-->
<!--            </if>-->
<!--        </where>-->
        <trim prefix="where" prefixOverrides="and">
            <if test="null != sid and '' != sid">
                and sid  = #{sid}
            </if>
            <if test="null != sname and '' != sname">
                and sanme like '%${sname}%'
            </if>
            <if test="null == course">
                and course = 'Java'
            </if>
        </trim>
    </select>
    <update id="updateStudent" parameterType="student">
        update student
<!--        <set>-->
<!--            <if test="null != sname and '' != sname">-->
<!--                 sname = #{sname},-->
<!--            </if>-->
<!--            <if test="null != age and '' != age">-->
<!--                 age = #{age},-->
<!--            </if>-->
<!--        </set>-->
<!--        where sid = #{sid}-->
        <trim prefix="set" suffixOverrides=",">
            <if test="null != sname and '' != sname">
                sname = #{sname},
            </if>
            <if test="null != age and '' != age">
                age = #{age},
            </if>
        </trim>
        where sid = #{sid}
    </update>

    <select id="findStudentByForeach" resultType="student">
        select * from student where sid in
        <foreach collection="list" item="sid" index="index" open="(" separator="," close=")">
            #{sid}
        </foreach>
    </select>
    <select id="findStudentByBind" resultType="student" parameterType="student">
        select * from student where
        <bind name="sname_pattern" value="'%'+sname+'%'"></bind>
        <if test="null != sname_pattern and '' != sname_pattern ">
            sname like #{sname_pattern}
        </if>
    </select>
</mapper>

在这里插入图片描述

(5)< trim >元素

下面展示一些 代码片

<?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="student">
    <select id="findStudentBySnameAndCourse" parameterType="student" resultType="student">
            select * from student where sname = #{sname}
           <!--根据条件动态拼装SQL语句-->
           <if test="null!=course and ''!=course"> and course = #{course}</if>
    </select>
    <select id="findStudentByChoose" parameterType="student" resultType="student">
<!--        select * from student where 1 = 1-->
<!--        <choose>-->
<!--            &lt;!&ndash;如果sid不为null或者空字符串&ndash;&gt;-->
<!--            <when test="null != sid and '' != sid">-->
<!--                and sid = #{sid}-->
<!--            </when>-->
<!--             &lt;!&ndash;如果sname不为null或者空字符串&ndash;&gt;-->
<!--            <when test="null != sname and '' != sname">-->
<!--                and sname like '%${sname}%'-->
<!--            </when>-->
<!--            &lt;!&ndash;如果上面不满足,则执行下面的&ndash;&gt;-->
<!--            <otherwise>-->
<!--                and course = 'Java'-->
<!--            </otherwise>-->
<!--        </choose>-->
        select * from student
<!--        <where>-->
<!--            <if test="null != sid and '' != sid">-->
<!--                and sid  = #{sid}-->
<!--            </if>-->
<!--            <if test="null != sname and '' != sname">-->
<!--                and sanme like '%${sname}%'-->
<!--            </if>-->
<!--            <if test="null == course">-->
<!--                and course = 'Java'-->
<!--            </if>-->
<!--        </where>-->
        <trim prefix="where" prefixOverrides="and">
            <if test="null != sid and '' != sid">
                and sid  = #{sid}
            </if>
            <if test="null != sname and '' != sname">
                and sanme like '%${sname}%'
            </if>
            <if test="null == course">
                and course = 'Java'
            </if>
        </trim>
    </select>
    <update id="updateStudent" parameterType="student">
        update student
<!--        <set>-->
<!--            <if test="null != sname and '' != sname">-->
<!--                 sname = #{sname},-->
<!--            </if>-->
<!--            <if test="null != age and '' != age">-->
<!--                 age = #{age},-->
<!--            </if>-->
<!--        </set>-->
<!--        where sid = #{sid}-->
        <trim prefix="set" suffixOverrides=",">
            <if test="null != sname and '' != sname">
                sname = #{sname},
            </if>
            <if test="null != age and '' != age">
                age = #{age},
            </if>
        </trim>
        where sid = #{sid}
    </update>

    <select id="findStudentByForeach" resultType="student">
        select * from student where sid in
        <foreach collection="list" item="sid" index="index" open="(" separator="," close=")">
            #{sid}
        </foreach>
    </select>
    <select id="findStudentByBind" resultType="student" parameterType="student">
        select * from student where
        <bind name="sname_pattern" value="'%'+sname+'%'"></bind>
        <if test="null != sname_pattern and '' != sname_pattern ">
            sname like #{sname_pattern}
        </if>
    </select>
</mapper>

在这里插入图片描述

在这里插入图片描述

(6)< foreach >元素

下面展示一些 代码片

<?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="student">
    <select id="findStudentBySnameAndCourse" parameterType="student" resultType="student">
            select * from student where sname = #{sname}
           <!--根据条件动态拼装SQL语句-->
           <if test="null!=course and ''!=course"> and course = #{course}</if>
    </select>
    <select id="findStudentByChoose" parameterType="student" resultType="student">
<!--        select * from student where 1 = 1-->
<!--        <choose>-->
<!--            &lt;!&ndash;如果sid不为null或者空字符串&ndash;&gt;-->
<!--            <when test="null != sid and '' != sid">-->
<!--                and sid = #{sid}-->
<!--            </when>-->
<!--             &lt;!&ndash;如果sname不为null或者空字符串&ndash;&gt;-->
<!--            <when test="null != sname and '' != sname">-->
<!--                and sname like '%${sname}%'-->
<!--            </when>-->
<!--            &lt;!&ndash;如果上面不满足,则执行下面的&ndash;&gt;-->
<!--            <otherwise>-->
<!--                and course = 'Java'-->
<!--            </otherwise>-->
<!--        </choose>-->
        select * from student
<!--        <where>-->
<!--            <if test="null != sid and '' != sid">-->
<!--                and sid  = #{sid}-->
<!--            </if>-->
<!--            <if test="null != sname and '' != sname">-->
<!--                and sanme like '%${sname}%'-->
<!--            </if>-->
<!--            <if test="null == course">-->
<!--                and course = 'Java'-->
<!--            </if>-->
<!--        </where>-->
        <trim prefix="where" prefixOverrides="and">
            <if test="null != sid and '' != sid">
                and sid  = #{sid}
            </if>
            <if test="null != sname and '' != sname">
                and sanme like '%${sname}%'
            </if>
            <if test="null == course">
                and course = 'Java'
            </if>
        </trim>
    </select>
    <update id="updateStudent" parameterType="student">
        update student
<!--        <set>-->
<!--            <if test="null != sname and '' != sname">-->
<!--                 sname = #{sname},-->
<!--            </if>-->
<!--            <if test="null != age and '' != age">-->
<!--                 age = #{age},-->
<!--            </if>-->
<!--        </set>-->
<!--        where sid = #{sid}-->
        <trim prefix="set" suffixOverrides=",">
            <if test="null != sname and '' != sname">
                sname = #{sname},
            </if>
            <if test="null != age and '' != age">
                age = #{age},
            </if>
        </trim>
        where sid = #{sid}
    </update>

    <select id="findStudentByForeach" resultType="student">
        select * from student where sid in
        <foreach collection="list" item="sid" index="index" open="(" separator="," close=")">
            #{sid}
        </foreach>
    </select>
    <select id="findStudentByBind" resultType="student" parameterType="student">
        select * from student where
        <bind name="sname_pattern" value="'%'+sname+'%'"></bind>
        <if test="null != sname_pattern and '' != sname_pattern ">
            sname like #{sname_pattern}
        </if>
    </select>
</mapper>

在这里插入图片描述

(7)< bind >元素

下面展示一些 代码片

<?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="student">
    <select id="findStudentBySnameAndCourse" parameterType="student" resultType="student">
            select * from student where sname = #{sname}
           <!--根据条件动态拼装SQL语句-->
           <if test="null!=course and ''!=course"> and course = #{course}</if>
    </select>
    <select id="findStudentByChoose" parameterType="student" resultType="student">
<!--        select * from student where 1 = 1-->
<!--        <choose>-->
<!--            &lt;!&ndash;如果sid不为null或者空字符串&ndash;&gt;-->
<!--            <when test="null != sid and '' != sid">-->
<!--                and sid = #{sid}-->
<!--            </when>-->
<!--             &lt;!&ndash;如果sname不为null或者空字符串&ndash;&gt;-->
<!--            <when test="null != sname and '' != sname">-->
<!--                and sname like '%${sname}%'-->
<!--            </when>-->
<!--            &lt;!&ndash;如果上面不满足,则执行下面的&ndash;&gt;-->
<!--            <otherwise>-->
<!--                and course = 'Java'-->
<!--            </otherwise>-->
<!--        </choose>-->
        select * from student
<!--        <where>-->
<!--            <if test="null != sid and '' != sid">-->
<!--                and sid  = #{sid}-->
<!--            </if>-->
<!--            <if test="null != sname and '' != sname">-->
<!--                and sanme like '%${sname}%'-->
<!--            </if>-->
<!--            <if test="null == course">-->
<!--                and course = 'Java'-->
<!--            </if>-->
<!--        </where>-->
        <trim prefix="where" prefixOverrides="and">
            <if test="null != sid and '' != sid">
                and sid  = #{sid}
            </if>
            <if test="null != sname and '' != sname">
                and sanme like '%${sname}%'
            </if>
            <if test="null == course">
                and course = 'Java'
            </if>
        </trim>
    </select>
    <update id="updateStudent" parameterType="student">
        update student
<!--        <set>-->
<!--            <if test="null != sname and '' != sname">-->
<!--                 sname = #{sname},-->
<!--            </if>-->
<!--            <if test="null != age and '' != age">-->
<!--                 age = #{age},-->
<!--            </if>-->
<!--        </set>-->
<!--        where sid = #{sid}-->
        <trim prefix="set" suffixOverrides=",">
            <if test="null != sname and '' != sname">
                sname = #{sname},
            </if>
            <if test="null != age and '' != age">
                age = #{age},
            </if>
        </trim>
        where sid = #{sid}
    </update>

    <select id="findStudentByForeach" resultType="student">
        select * from student where sid in
        <foreach collection="list" item="sid" index="index" open="(" separator="," close=")">
            #{sid}
        </foreach>
    </select>
    <select id="findStudentByBind" resultType="student" parameterType="student">
        select * from student where
        <bind name="sname_pattern" value="'%'+sname+'%'"></bind>
        <if test="null != sname_pattern and '' != sname_pattern ">
            sname like #{sname_pattern}
        </if>
    </select>
</mapper>

在这里插入图片描述

7.编写测试类

(1)TestIf类

下面展示一些 代码片

package com.qfedu.test;

import com.qfedu.pojo.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.*;

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

public class TestIf {
    public static void main(String[] args) {
        String resource = "mybatis-config.xml";
        try {
            InputStream in = Resources.getResourceAsStream(resource);
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
            SqlSession sqlSession = factory.openSession();
            Student student = new Student();
            student.setSname("LiSi");
            student.setCourse("Java");
            List<Student> selectList = sqlSession.selectList("student.findStudentBySnameAndCourse", student);
            for (Student stu : selectList){
                System.out.println(stu.toString());
            }
            sqlSession.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

(2)TestChoose类

下面展示一些 代码片

package com.qfedu.test;

import com.qfedu.pojo.Student;
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;
import java.util.List;

public class TestChoose {
    public static void main(String[] args) {
        String resource = "mybatis-config.xml";
        try {
            InputStream in = Resources.getResourceAsStream(resource);
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
            SqlSession sqlSession = factory.openSession();
            Student student = new Student();
//            student.setSid(2);
//            student.setSname("LiSi");
            List<Student> selectList = sqlSession.selectList("student.findStudentByChoose", student);
            for (Student stu : selectList){
                System.out.println(stu.toString());
            }
            sqlSession.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

(3)TestSet类

下面展示一些 代码片

package com.qfedu.test;

import com.qfedu.pojo.Student;
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 TestSet {
    public static void main(String[] args) {
        String resource = "mybatis-config.xml";
        try {
            InputStream in = Resources.getResourceAsStream(resource);
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
            SqlSession sqlSession = factory.openSession();
            Student student = new Student();
//            为student成员属性赋值
            student.setSid(4);
//            student.setSname("ZhaoLiu");
//            student.setAge("20");
            student.setSname("SunQi");
            student.setAge("21");
//            调用sqlSession的update()方法
            int result = sqlSession.update("student.updateStudent",student);
            if(result>0){
                System.out.println("成功更新"+result+"条数据");
            }else{
                System.out.println("更新操作失败");
            }
            sqlSession.commit();
            sqlSession.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

在这里插入图片描述
在这里插入图片描述

(4)TestForeach类

下面展示一些 代码片

package com.qfedu.test;

import com.qfedu.pojo.Student;
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;
import java.util.ArrayList;
import java.util.List;

public class TestForeach {
    public static void main(String[] args) {
//        读取配置文件
        String resource = "mybatis-config.xml";
//        输入流
        try {
            InputStream in = Resources.getResourceAsStream(resource);
//            创建SQLSessionFactory对象
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//            创建sqlSession对象
            SqlSession sqlSession = factory.openSession();
            ArrayList<Integer> list = new ArrayList<Integer>();
            list.add(1);
            list.add(2);
            list.add(3);
//            调用SqlSession的selectList()方法查询
            List<Student> stuList = sqlSession.selectList("student.findStudentByForeach", list);
            for (Student stu:stuList){
                System.out.println(stu.toString());
            }
//            关闭SqlSession
            sqlSession.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

在这里插入图片描述

(5)TestBind类

下面展示一些 代码片

package com.qfedu.test;

import com.qfedu.pojo.Student;
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;
import java.util.List;

public class TestBind {
    public static void main(String[] args) {
//        加载配置文件
        String resource = "mybatis-config.xml";
        try {
            InputStream in = Resources.getResourceAsStream(resource);
//            创建SQLSessionFactory对象
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//            创建SqlSession对象
            SqlSession sqlSession = factory.openSession();
            Student student = new Student();
            student.setSname("LiSi");
            List<Student> selectList = sqlSession.selectList("student.findStudentByBind",student);
            for (Student stu:selectList){
                System.out.println(stu.toString());
            }
            sqlSession.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值