Mybatis

目录

一、配置依赖文件

二、配置log4j.properties文件

三、配置SqlMapConfig.xml文件

四、配置Mapper.xml文件

五、增删改查

 六、动态代理

七、输入映射

八、输出映射

九、动态SQL

十、多对一

十一、一对多

十二、多对多

十三、自查讯


一、配置依赖文件

<?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>com.iotek</groupId>
    <artifactId>fristMybatis</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <dependencies>

        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <!-- mybatis核心配置文件-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.7</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/mysql/mysql-connector-java -->
        <!-- mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.30</version>
        </dependency>

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

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

二、配置log4j.properties文件

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

三、配置SqlMapConfig.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>
    <!-- 和spring整合后 environments配置将废除-->
    <environments default="development">
        <environment id="development">
            <!-- 使用jdbc事务管理-->
            <transactionManager type="JDBC"/>
            <!-- 数据库连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="GoodsMapper.xml"/>
    </mappers>
</configuration>

四、配置Mapper.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.iotek">
    <!-- ID不可重复-->
    <select id="selectGoodsByGoodsID" parameterType="int" resultType="com.iotek.pojo.Goods">
        SELECT * FROM t_goods WHERE goods_id = 20
    </select>
</mapper>
package com.iotek.pojo;

/**
 * @Author:
 * @CreateTime: 2023-02-05  13:25
 */
public class Goods {
    private int goods_id;
    private String goods_name;
    private double goods_price;

    public Goods() {
        super();
    }

    public Goods(int goods_id, String goods_name, double goods_price) {
        this.goods_id = goods_id;
        this.goods_name = goods_name;
        this.goods_price = goods_price;
    }

    public int getGoods_id() {
        return goods_id;
    }

    public void setGoods_id(int goods_id) {
        this.goods_id = goods_id;
    }

    public String getGoods_name() {
        return goods_name;
    }

    public void setGoods_name(String goods_name) {
        this.goods_name = goods_name;
    }

    public double getGoods_price() {
        return goods_price;
    }

    public void setGoods_price(double goods_price) {
        this.goods_price = goods_price;
    }

    @Override
    public String toString() {
        return "Goods{" +
                "goods_id=" + goods_id +
                ", goods_name='" + goods_name + '\'' +
                ", goods_price=" + goods_price +
                '}';
    }
}
package com.iotek.mybatis.test;

import com.iotek.pojo.Goods;
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;

/**
 * @Author:
 * @CreateTime: 2023-02-05  13:38
 */
public class MybatisTest {

    /**
     * @description:查询一条数据
     * @param
     * @return void
     * @time: 2023/2/5 13:53
     **/
    @Test
    public void test1() throws IOException {
        //加载核心配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        //所有的增删改查都在session中
        SqlSession session = sqlSessionFactory.openSession();
        //查询一条数据 com.iotek.selectGoodsByGoodsID命名空间
        Goods goods= session.selectOne("com.iotek.selectGoodsByGoodsID");
        System.out.println(goods);
    }
}

五、增删改查

<?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">
<!-- sql语句的映射-->
<!-- namespace命名空间-->
<mapper namespace="com.iotek">
    <!--statment78 ID不可重复  parameterType输入 resultType输出-->
    <!-- 查询一条数据-->
    <select id="selectGoodsByGoodsID" parameterType="int" resultType="com.iotek.pojo.Goods">
        SELECT * FROM t_goods WHERE goods_id = #{id}
    </select>
    <!-- 添加一条数据-->
    <!--
        #{}表示占位符 可以防止sql注入
        t_goods(goods_name,goods_price)中是数数据库字段名
        VALUES (#{goods_name},#{goods_price})中是对象的属性名
    -->
    <insert id="insertOneGoods" parameterType="com.iotek.pojo.Goods">
        INSERT INTO t_goods(goods_name,goods_price) VALUES(#{goods_name},#{goods_price})
    </insert>

    <!-- 删除一条数据-->
    <delete id="deleteOneGoods" parameterType="int">
        DELETE FROM t_goods WHERE goods_id=#{id}
    </delete>
    <!-- 修改一条数据-->
    <update id="updateOneGoods" parameterType="com.iotek.pojo.Goods">
        UPDATE t_goods SET goods_name=#{goods_name},goods_price=#{goods_price} WHERE goods_id=#{goods_id}
    </update>

    <!-- 模糊查询-->
    <!-- #{}占位符 ${}拼串-->
    <select id="selectGoodsLikeGoodsName" parameterType="String" resultType="com.iotek.pojo.Goods">
        SELECT * FROM t_goods WHERE goods_name LIKE '%${value}%'
    </select>
</mapper>
package com.iotek.mybatis.test;

import com.iotek.pojo.Goods;
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.List;

/**
 * @Author:
 * @CreateTime: 2023-02-05  13:38
 */
public class MybatisTest {
    /**
     * @description:查询一条数据
     * @param
     * @return void
     * @time: 2023/2/5 13:53
     **/
    @Test
    public void test1() throws IOException {
        //加载核心配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        //所有的增删改查都在session中
        SqlSession session = sqlSessionFactory.openSession();
        //查询一条数据 com.iotek.selectGoodsByGoodsID命名空间
        Goods goods= session.selectOne("com.iotek.selectGoodsByGoodsID",10);
        System.out.println(goods);
    }
    /**
     * @description:插入一条数据
     * @param
     * @return void
     * @time: 2023/2/5 15:09
     **/
    @Test
    public void test2() throws IOException {
        //加载核心配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        //所有的增删改查都在session中
        SqlSession session = sqlSessionFactory.openSession();
        //查询一条数据 com.iotek.selectGoodsByGoodsID命名空间
        Goods goods = new Goods(0,"宏碁/ACER",29577.28);
        //返回值影响行数
        int insert = session.insert("com.iotek.insertOneGoods", goods);
        //事务提交 内从中提交到磁盘
        session.commit();
        System.out.println("插入了"+insert+"条数据");
    }
    /**
     * @description:删除了一条数据
     * @param
     * @return void
     * @time: 2023/2/5 20:16
     **/
    @Test
    public void test3() throws IOException {
        //加载核心配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        //所有的增删改查都在session中
        SqlSession session = sqlSessionFactory.openSession();
        //返回值影响行数
        int delete = session.delete("com.iotek.deleteOneGoods", 21);
        //事务提交 内从中提交到磁盘
        session.commit();
        System.out.println("删除了"+delete+"条数据");
    }
    /**
     * @description:修改了一条数据
     * @param
     * @return void
     * @time: 2023/2/5 20:32
     **/
    @Test
    public void test4() throws IOException {
        //加载核心配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        //所有的增删改查都在session中
        SqlSession session = sqlSessionFactory.openSession();
        //返回值影响行数
        Goods goods = new Goods(22,"微星/MSI",39577.28);
        int delete = session.update("com.iotek.updateOneGoods", goods);
        //事务提交 内从中提交到磁盘
        session.commit();
        System.out.println("修改了"+delete+"条数据");
    }

    /**
     * @description:查询集合
     * @param
     * @return void
     * @time: 2023/2/5 20:44
     **/
    @Test
    public void test5() throws IOException {
        //加载核心配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        //所有的增删改查都在session中
        SqlSession session = sqlSessionFactory.openSession();
        //返回集合
        List<Goods> goodsLis = session.selectList("com.iotek.selectGoodsLikeGoodsName","华");
        //事务提交 内从中提交到磁盘
        System.out.println(goodsLis);
    }
}

 六、动态代理

<?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">
<!-- mybatis动态代理模式 相当于Dao接口-->
<!--
    1、namespace名字与类路径相同
    2、statement的ID与接口方法名一致
    3、输入参数类型与parameterType类型一致
    4、输入类型与resultType类型一致
-->
<mapper namespace="com.iotek.Dao.UserMapper">
    <!-- id与接口方法名相同-->
    <insert id="addUser" parameterType="com.iotek.pojo.User">
        INSERT INTO t_user(userName,userAge) VALUES(#{userName},#{userAge})
    </insert>

    <select id="queryUserByUserName" parameterType="java.lang.String" resultType="User">
        SELECT * FROM t_user WHERE userName LIKE '%${userName}%'
    </select>

    <!-- 传入的是对象 User与UserAgeCondition有组合关系 userAgeCondition.minAge对象的属性-->
    <select id="queryUserByUserAgeCondition" parameterType="User" resultType="User">
        SELECT * FROM t_user WHERE userAge BETWEEN #{userAgeCondition.minAge} AND #{userAgeCondition.maxAge}
    </select>
</mapper>
package com.iotek.mybatis.DaoTest;

import com.iotek.Dao.UserMapper;
import com.iotek.pojo.User;
import com.iotek.pojo.UserAgeCondition;
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.Before;
import org.junit.Test;

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

/**
 * @Author:
 * @CreateTime: 2023-02-05  23:06
 */
public class UserDaoTest {
    private SqlSessionFactory sqlSessionFactory;
    //Before在运行Test之前执行
    @Before
    public void getSessionFactory() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
    }
    /**
     * @description:添加一条数据
     * @param
     * @return void
     * @time: 2023/2/6 12:00
     **/
    @Test
    public void addUser(){
        SqlSession session = sqlSessionFactory.openSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);//代理对象
        User user = new User(0,"lucy",18);
        userMapper.addUser(user);
        session.commit();
    }
    /**
     * @description:姓名模糊查询
     * @param
     * @return void
     * @time: 2023/2/6 14:38
     **/
    @Test
    public void queryUserByUserName(){
        SqlSession session = sqlSessionFactory.openSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);//代理对象
        List<User> user = userMapper.queryUserByUserName("思");
        System.out.println(user);
    }
    /**
     * @description:查询年龄20到50岁之间的
     * @param
     * @return void
     * @time: 2023/2/6 14:15
     **/
    @Test
    public void queryUserByUserAgeCondition(){
        SqlSession session = sqlSessionFactory.openSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);//代理对象
        UserAgeCondition uc = new UserAgeCondition();
        uc.setMaxAge(50);
        uc.setMinAge(20);
        User user = new User();
        user.setUserAgeCondition(uc);
        List<User> users = userMapper.queryUserByUserAgeCondition(user);
        System.out.println(users);
    }
}

七、输入映射

package com.iotek.Dao;

import com.iotek.pojo.Goods;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

/**
 * @Author:
 * @CreateTime: 2023-02-06  12:41
 */
public interface GoodsMapper {
    //添加一条数据
    public void addGoods(Goods goods);
    //删除一条数据
    public void deleteGoodsByGoodsId(int goodsID);
    //修改一条数据
    public void updateGoodsByGoodsId(Goods goods);
    //根据ID查询
    public Goods queryGoodsByGoodsID(int goodsId);
    //根据名称查询
    public List<Goods> queryGoodsByGoodsName(String goodsName);
    //根据ID和名称查询  传入多个参数默认放入Map键值对中 map key value --> key=param1 value=goodsID;key=param2 value=goodsName 参数按顺序
    public Goods queryGoodsByGoodsIDAndGoodsName(int goodsID,String goodsName);
    //根据ID和名称查询 @Param注解方式
    public Goods queryGoodsByGoodsIDAndGoodsName2(@Param("goodsID") int goodsID, @Param("goodsName") String goodsName);
    //根据ID和名称查询 一个用param一个用@Param注解方式
    public Goods queryGoodsByGoodsIDAndGoodsName3(@Param("goodsID") int goodsID, String goodsName);
    //根据ID和名称查询 传入Map
    public Goods queryGoodsByMap(Map<String,Object> map);
    //根据ID和名称查询 传入List集合
    public Goods queryGoodsByIDAndList(int goodsID,List<Goods> goods);
}
<?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.iotek.Dao.GoodsMapper">

    <insert id="addGoods" parameterType="Goods">
        INSERT INTO t_goods(goods_name,goods_price) VALUES (#{goods_name},#{goods_price});
    </insert>

    <delete id="deleteGoodsByGoodsId" parameterType="int">
        DELETE FROM t_goods WHERE goods_id = #{goods_id}
    </delete>

    <update id="updateGoodsByGoodsId" parameterType="Goods">
        UPDATE t_goods SET goods_name=#{goods_name},goods_price=#{goods_price} WHERE goods_id = #{goods_id}
    </update>

    <select id="queryGoodsByGoodsID" parameterType="int" resultType="Goods">
        SELECT * FROM t_goods WHERE goods_id=#{goods_id}
    </select>

    <select id="queryGoodsByGoodsName" parameterType="java.lang.String" resultType="Goods">
        SELECT * FROM t_goods WHERE goods_name LIKE '%${goods_name}%'
    </select>

    <!-- 多个参数parameterType可以不写-->
    <select id="queryGoodsByGoodsIDAndGoodsName" resultType="Goods">
        SELECT * FROM t_goods WHERE goods_id=#{param1} AND goods_name=#{param2}
    </select>

    <select id="queryGoodsByGoodsIDAndGoodsName2" resultType="Goods">
        SELECT * FROM t_goods WHERE goods_id=#{goodsID} AND goods_name=#{goodsName}
    </select>

    <select id="queryGoodsByGoodsIDAndGoodsName3" resultType="Goods">
        SELECT * FROM t_goods WHERE goods_id=#{goodsID} AND goods_name=#{param2}
    </select>

    <!-- goods_id=#{goodsID} AND goods_name=#{goodsName}参数为map的Test中Map的key-->
    <select id="queryGoodsByMap" parameterType="hashMap" resultType="Goods">
        SELECT * FROM t_goods WHERE goods_id=#{goodsID} AND goods_name=#{goodsName}
    </select>

    <!-- 多值 动态sql-->
    <select id="queryGoodsByIDAndList" resultType="Goods">
        SELECT * FROM t_goods WHERE goods_id=#{param1} AND goods_name=#{param2[0].goods_name}
    </select>
</mapper>
package com.iotek.mybatis.DaoTest;

import com.iotek.Dao.GoodsMapper;
import com.iotek.pojo.Goods;
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.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Author:
 * @CreateTime: 2023-02-06  12:49
 */
public class GoodsDaoTest {
    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void getSqlSessionFactory() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
    }
    /**
     * @description:添加一条记录
     * @param
     * @return void
     * @time: 2023/2/6 16:06
     **/
    @Test
    public void addGoods(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = session.getMapper(GoodsMapper.class);
        Goods goods = new Goods(0,"戴尔/DELL",45633.2);
        goodsMapper.addGoods(goods);
        session.commit();
    }
    /**
     * @description:根据ID删除
     * @param
     * @return void
     * @time: 2023/2/6 16:06
     **/
    @Test
    public void deleteGoodsByGoodsId(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = session.getMapper(GoodsMapper.class);
        goodsMapper.deleteGoodsByGoodsId(22);
        session.commit();
    }
    /**
     * @description:根据ID修改
     * @param
     * @return void
     * @time: 2023/2/6 16:07
     **/
    @Test
    public void updateGoodsByGoodsId(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = session.getMapper(GoodsMapper.class);
        Goods goods = new Goods(24,"华为/HUAWEI",85416.37);
        goodsMapper.updateGoodsByGoodsId(goods);
        session.commit();
    }
    /**
     * @description:根据ID查询
     * @param
     * @return void
     * @time: 2023/2/6 16:07
     **/
    @Test
    public void queryGoodsByGoodsID(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = session.getMapper(GoodsMapper.class);
        Goods goods = goodsMapper.queryGoodsByGoodsID(10);
        System.out.println(goods);
    }
    /**
     * @description:根据名称查询
     * @param
     * @return void
     * @time: 2023/2/6 16:07
     **/
    @Test
    public void queryGoodsByGoodsName(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = session.getMapper(GoodsMapper.class);
        List<Goods> goodsList = goodsMapper.queryGoodsByGoodsName("华");
        System.out.println(goodsList);
    }
    /**
     * @description:根据ID和名称查询
     * @param
     * @return void
     * @time: 2023/2/6 16:08
     **/
    @Test
    public void queryGoodsByGoodsIDAndGoodsName(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = session.getMapper(GoodsMapper.class);
        Goods goods = goodsMapper.queryGoodsByGoodsIDAndGoodsName(10,"微星/MSI");
        System.out.println(goods);
    }
    @Test
    public void queryGoodsByGoodsIDAndGoodsName2(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = session.getMapper(GoodsMapper.class);
        Goods goods = goodsMapper.queryGoodsByGoodsIDAndGoodsName2(10,"微星/MSI");
        System.out.println(goods);
    }
    @Test
    public void queryGoodsByGoodsIDAndGoodsName3(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = session.getMapper(GoodsMapper.class);
        Goods goods = goodsMapper.queryGoodsByGoodsIDAndGoodsName3(10,"微星/MSI");
        System.out.println(goods);
    }
    @Test
    public void queryGoodsByMap(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = session.getMapper(GoodsMapper.class);
        Map<String,Object> map = new HashMap<>();
        map.put("goodsID",10);
        map.put("goodsName","微星/MSI");
        Goods goods = goodsMapper.queryGoodsByMap(map);
        System.out.println(goods);
    }
    @Test
    public void queryGoodsByIDAndList(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsMapper goodsMapper = session.getMapper(GoodsMapper.class);
        Goods goods = new Goods();
        goods.setGoods_name("微星/MSI");
        List<Goods> goodsList = new ArrayList<>();
        goodsList.add(goods);
        Goods gd = goodsMapper.queryGoodsByIDAndList(10,goodsList);
        System.out.println(gd);
    }
}

八、输出映射

<?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.iotek.Dao.AnimalDao">

    <!--<select id="selectAnimalByAniamlID" parameterType="int" resultType="Animal">
        SELECT * FROM t_animal WHERE t_id = #{t_id}
    </select>-->

    <!-- 当属性与字段名不一致时解决方案-->

    <!-- 1、别名-->
    <!--<select id="selectAnimalByAniamlID" parameterType="int" resultType="Animal">
        SELECT t_id, t_animalName AS animalName,t_animalAge AS animalAge FROM t_animal WHERE t_id = #{t_id}
    </select>-->

    <!--2、resultMap映射 结果集的映射-->
    <!-- type 类型是返回类型 column数据库字段名 property对象的属性-->
    <resultMap id="animalMapper" type="Animal">
        <result column="t_animalName" property="animalName"></result>
        <result column="t_animalAge" property="animalAge"></result>

    </resultMap>
    <!-- resultMap引用上面ID-->
    <select id="selectAnimalByAniamlID" parameterType="int" resultMap="animalMapper">
        SELECT * FROM t_animal WHERE t_id = #{t_id}
    </select>
</mapper>

九、动态SQL

<?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.iotek.Dao.GoodsDao">
    <!-- 动态Sql-->
    <!-- if test必须有 WHERE 1=1永真条件-->
    <select id="queryGoodsByIf" parameterType="Goods" resultType="Goods">
        SELECT * FROM t_goods WHERE 1=1
        <if test="goods_name!= null and goods_name!=''">
            AND goods_name LIKE "%${goods_name}%"
        </if>
        <if test="goods_price>8000">
            AND goods_price>#{goods_price}
        </if>
    </select>

    <!--where -->
    <select id="queryGoodsByWhere" parameterType="Goods" resultType="Goods">
        SELECT * FROM t_goods
        <where>
            <if test="goods_name!= null and goods_name!=''">
                AND goods_name LIKE "%${goods_name}%"
            </if>
            <if test="goods_price>8000">
                AND goods_price>#{goods_price}
            </if>
        </where>
    </select>

    <!--choose otherwise都不满足执行 -->
    <select id="queryGoodsByChoose" parameterType="Goods" resultType="Goods">
        SELECT * FROM t_goods
        <where>
            <choose>
                <when test="goods_name!= null and goods_name!=''">
                    AND goods_name LIKE "%${goods_name}%"
                </when>
                <when test="goods_price>8000">
                    AND goods_price>#{goods_price}
                </when>
                <otherwise>
                    1=1
                </otherwise>
            </choose>
        </where>
    </select>

    <!-- 数组 array collection遍历数组的内容 item每一项赋给变量里 open开始 close结束 separator分隔符-->
    <select id="queryGoodsByForeach1" parameterType="Object[]" resultType="Goods">
        SELECT * FROM t_goods
        <if test="array.length>0">
            WHERE goods_id IN
            <foreach collection="array" item="myid" open="(" close=")" separator=",">
                #{myid}
            </foreach>
        </if>
    </select>

    <!-- 集合 collection遍历的内容 parameterType参数可以不写-->
    <select id="queryGoodsByForeach2" resultType="Goods">
        SELECT * FROM t_goods
        <if test="list.size>0">
            WHERE goods_id IN
            <foreach collection="list" item="myid" open="(" close=")" separator=",">
                #{myid}
            </foreach>
        </if>
    </select>

    <select id="queryGoodsByForeach3" resultType="Goods">
        <include refid="mySql"></include>
        <if test="list.size>0">
            WHERE goods_id IN
            <foreach collection="list" item="goods" open="(" close=")" separator=",">
                #{goods.goods_id}
            </foreach>
        </if>
    </select>

    <!-- sql片段 解决sql重复问题 -->
    <sql id="mySql">
        SELECT * FROM t_goods
    </sql>
</mapper>
package com.iotek.DaoTest;

import com.iotek.Dao.AnimalDao;
import com.iotek.Dao.GoodsDao;
import com.iotek.pojo.Animal;
import com.iotek.pojo.Goods;
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.Before;
import org.junit.Test;

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

/**
 * @Author:
 * @CreateTime: 2023-02-13  14:01
 */
public class DynSqlTest {
    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void getsessionFactory() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
    }

    @Test
    public void queryGoodsByIf(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsDao goodsDao = session.getMapper(GoodsDao.class);
        Goods goods = new Goods();
        goods.setGoods_name("华为");//SELECT * FROM t_goods WHERE goods_name LIKE "%华为%"
        //goods.setGoods_name("");//SELECT * FROM t_goods
        goods.setGoods_price(80000);//SELECT * FROM t_goods WHERE goods_name LIKE "%华为%" AND goods_price>?
        List<Goods> list = goodsDao.queryGoodsByIf(goods);
        System.out.println(list);
    }
    @Test
    public void queryGoodsByWhere(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsDao goodsDao = session.getMapper(GoodsDao.class);
        Goods goods = new Goods();
        goods.setGoods_name("华为");//SELECT * FROM t_goods WHERE goods_name LIKE "%华为%"
        //goods.setGoods_name("");//SELECT * FROM t_goods
        goods.setGoods_price(80000);//SELECT * FROM t_goods WHERE goods_name LIKE "%华为%" AND goods_price>?
        List<Goods> list = goodsDao.queryGoodsByWhere(goods);
        System.out.println(list);
    }
    @Test
    public void queryGoodsByChoose(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsDao goodsDao = session.getMapper(GoodsDao.class);
        Goods goods = new Goods();
        goods.setGoods_name("华为");//SELECT * FROM t_goods WHERE goods_name LIKE "%华为%"
        //goods.setGoods_name("");//SELECT * FROM t_goods
        goods.setGoods_price(80000);//SELECT * FROM t_goods WHERE goods_name LIKE "%华为%" AND goods_price>?
        List<Goods> list = goodsDao.queryGoodsByChoose(goods);
        System.out.println(list);
    }
    @Test
    public void queryGoodsByForeach1(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsDao goodsDao = session.getMapper(GoodsDao.class);
        int ids[] = {1,20,4};
        List<Goods> list = goodsDao.queryGoodsByForeach1(ids);
        System.out.println(list);
    }
    @Test
    public void queryGoodsByForeach2(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsDao goodsDao = session.getMapper(GoodsDao.class);
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(20);
        list.add(14);
        List<Goods> goodsList = goodsDao.queryGoodsByForeach2(list);
        System.out.println(goodsList);
    }
    @Test
    public void queryGoodsByForeach3(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsDao goodsDao = session.getMapper(GoodsDao.class);
        Goods g1= new Goods();
        g1.setGoods_id(1);
        Goods g2= new Goods();
        g2.setGoods_id(20);
        Goods g3= new Goods();
        g3.setGoods_id(14);
        List<Goods> goods = new ArrayList<>();
        goods.add(g1);
        goods.add(g2);
        goods.add(g3);
        List<Goods> goodsList = goodsDao.queryGoodsByForeach3(goods);
        System.out.println(goodsList);
    }
}

十、多对一

package com.iotek.pojo;

/**
 * @Author:
 * @CreateTime: 2023-02-13  15:49
 */
public class Order {
    private Integer id;
    private double orderNumer;
    private double totalamount;
    private String orderTime;
    private GoodsTwo goodsTwo;

    public Order() {
        super();
    }

    public Order(Integer id, double orderNumer, double totalamount, String orderTime, GoodsTwo goodsTwo) {
        this.id = id;
        this.orderNumer = orderNumer;
        this.totalamount = totalamount;
        this.orderTime = orderTime;
        this.goodsTwo = goodsTwo;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public double getOrderNumer() {
        return orderNumer;
    }

    public void setOrderNumer(double orderNumer) {
        this.orderNumer = orderNumer;
    }

    public double getTotalamount() {
        return totalamount;
    }

    public void setTotalamount(double totalamount) {
        this.totalamount = totalamount;
    }

    public String getOrderTime() {
        return orderTime;
    }

    public void setOrderTime(String orderTime) {
        this.orderTime = orderTime;
    }

    public GoodsTwo getGoodsTwo() {
        return goodsTwo;
    }

    public void setGoodsTwo(GoodsTwo goodsTwo) {
        this.goodsTwo = goodsTwo;
    }

    @Override
    public String toString() {
        return "order{" +
                "id=" + id +
                ", orderNumer=" + orderNumer +
                ", totalamount=" + totalamount +
                ", orderTime='" + orderTime + '\'' +
                ", goodsTwo=" + goodsTwo +
                '}';
    }
}
package com.iotek.pojo;

import java.util.Set;

/**
 * @Author:一的一段维护端
 * @CreateTime: 2023-02-13  15:47
 */
public class GoodsTwo {
    private Integer gid;
    private String goodsname;
    private double goodsprice;
    private Set<Order> orders;//关联关系

    public GoodsTwo() {
        super();
    }

    public GoodsTwo(Integer gid, String goodsname, double goodsprice, Set<Order> orders) {
        this.gid = gid;
        this.goodsname = goodsname;
        this.goodsprice = goodsprice;
        this.orders = orders;
    }

    public Integer getGid() {
        return gid;
    }

    public void setGid(Integer gid) {
        this.gid = gid;
    }

    public String getGoodsname() {
        return goodsname;
    }

    public void setGoodsname(String goodsname) {
        this.goodsname = goodsname;
    }

    public double getGoodsprice() {
        return goodsprice;
    }

    public void setGoodsprice(double goodsprice) {
        this.goodsprice = goodsprice;
    }

    public Set<Order> getOrders() {
        return orders;
    }

    public void setOrders(Set<Order> orders) {
        this.orders = orders;
    }

    @Override
    public String toString() {
        return "GoodsTwo{" +
                "gid=" + gid +
                ", goodsname='" + goodsname + '\'' +
                ", goodsprice=" + goodsprice +
                ", orders=" + orders +
                '}';
    }
}
<?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.iotek.Dao.OrderDao">
    <!-- 多对一 -->
    <!--多对1 多表查询-->
    <resultMap id="orderMapper" type="Order">
        <id column="id" property="id"/>
        <result column="orderNumer" property="orderNumer"/>
        <result column="totalamount" property="totalamount"/>
        <result column="orderTime" property="orderTime"/>
        <association property="goodsTwo" javaType="GoodsTwo">
            <id column="gid" property="gid"/>
            <result column="goodsname" property="goodsname" />
            <result column="goodsprice" property="goodsprice" />
        </association>
    </resultMap>
    <select id="getOrderByIDManyToOne1" resultMap="orderMapper">
        SELECT gid,goodsname,goodsprice,id,orderNumer,totalamount,orderTime FROM goods g, t_order o WHERE g.gid=o.goods_id AND o.id=#{id}
    </select>

    <!-- 分步查询 -->
    <select id="getGoodsTwoByOrderID" parameterType="int" resultType="GoodsTwo">
        SELECT gid,goodsname,goodsprice FROM goods WHERE gid=#{goods_id}
    </select>
    <resultMap id="orderMapper1" type="Order">
        <id column="id" property="id"/>
        <result column="orderNumer" property="orderNumer"/>
        <result column="totalamount" property="totalamount"/>
        <result column="orderTime" property="orderTime"/>
        <association property="goodsTwo" javaType="GoodsTwo" select="getGoodsTwoByOrderID" column="goods_id">

        </association>
    </resultMap>
    <select id="getOrderByIDManyToOne2" resultMap="orderMapper1">
        SELECT id,orderNumer,totalamount,orderTime,goods_id FROM t_order WHERE id=#{id}
    </select>
</mapper>
package com.iotek.DaoTest;

import com.iotek.Dao.GoodsTwoDao;
import com.iotek.Dao.OrderDao;
import com.iotek.pojo.Goods;
import com.iotek.pojo.GoodsTwo;
import com.iotek.pojo.Order;
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.Before;
import org.junit.Test;

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

/**
 * @Author:
 * @CreateTime: 2023-02-13  16:22
 */
public class OneToMany {
    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void getsessionFactory() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
    }

    @Test
    public void ManyToOne1(){
        SqlSession session = sqlSessionFactory.openSession();
        OrderDao orderDao = session.getMapper(OrderDao.class);
        Order order = orderDao.getOrderByIDManyToOne1(1);
        System.out.println(order);
    }
    @Test
    public void ManyToOne2(){
        SqlSession session = sqlSessionFactory.openSession();
        OrderDao orderDao = session.getMapper(OrderDao.class);
        Order order = orderDao.getOrderByIDManyToOne2(4);
        System.out.println(order);
    }
}

十一、一对多

<?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.iotek.Dao.GoodsTwoDao">
    <!-- 1对多 多表查询 -->
    <!-- 集合中 property对应集合中的属性名 ofType集合中的属性-->
    <resultMap id="goodsTwoMapper" type="GoodsTwo">
        <id column="gid" property="gid"/>
        <result column="goodsname" property="goodsname"/>
        <result column="goodsprice" property="goodsprice"/>
        <collection property="orders" ofType="Order">
            <id column="id" property="id"/>
            <result column="orderNumer" property="orderNumer" />
            <result column="totalamount" property="totalamount" />
            <result column="orderTime" property="orderTime" />
        </collection>
    </resultMap>
    <select id="getGoodsByIDOneToMany1" resultMap="goodsTwoMapper">
        SELECT gid,goodsname,goodsprice,id,orderNumer,totalamount,orderTime FROM goods g, t_order o WHERE g.gid=o.goods_id AND g.gid=#{gid}
    </select>

    <!-- 1对多 分步法 -->
    <select id="getOrderByGoodsID" parameterType="int" resultType="Order">
        SELECT id,orderNumer,totalamount,orderTime FROM t_order WHERE goods_id=#{gid}
    </select>
    <resultMap id="goodsTwoMapper2" type="GoodsTwo">
        <id column="gid" property="gid"/>
        <result column="goodsname" property="goodsname"/>
        <result column="goodsprice" property="goodsprice"/>
        <collection property="orders" ofType="Order" select="getOrderByGoodsID" column="gid">

        </collection>
    </resultMap>
    <select id="getGoodsByIDOneToMany2" resultMap="goodsTwoMapper2">
        SELECT gid,goodsname,goodsprice FROM goods WHERE gid=#{gid}
    </select>
</mapper>
package com.iotek.DaoTest;

import com.iotek.Dao.GoodsTwoDao;
import com.iotek.pojo.GoodsTwo;
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.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Author:
 * @CreateTime: 2023-02-13  16:22
 */
public class ManyToOne {
    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void getsessionFactory() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
    }

    @Test
    public void OneToMany1(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsTwoDao goodsTwoDao = session.getMapper(GoodsTwoDao.class);
        GoodsTwo goodsByID = goodsTwoDao.getGoodsByIDOneToMany1(1);
        System.out.println(goodsByID);
    }
    @Test
    public void OneToMany2(){
        SqlSession session = sqlSessionFactory.openSession();
        GoodsTwoDao goodsTwoDao = session.getMapper(GoodsTwoDao.class);
        GoodsTwo goodsByID = goodsTwoDao.getGoodsByIDOneToMany2(2);
        System.out.println(goodsByID);
    }
}

十二、多对多

package com.iotek.pojo;

import java.util.Set;

/**
 * @Author:
 * @CreateTime: 2023-02-13  23:32
 */
public class Student {
    private Integer sid;
    private String sname;
    private Set<Course> courses;

    public Student() {
        super();
    }

    public Student(Integer sid, String sname, Set<Course> courses) {
        this.sid = sid;
        this.sname = sname;
        this.courses = courses;
    }

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public Set<Course> getCourses() {
        return courses;
    }

    public void setCourses(Set<Course> courses) {
        this.courses = courses;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", sname='" + sname + '\'' +
                ", courses=" + courses +
                '}';
    }
}
package com.iotek.pojo;

/**
 * @Author:
 * @CreateTime: 2023-02-13  23:28
 */
public class Course {
    private Integer cid;
    private String cname;

    public Course() {
        super();
    }

    public Course(Integer cid, String cname) {
        this.cid = cid;
        this.cname = cname;
    }

    public Integer getCid() {
        return cid;
    }

    public void setCid(Integer cid) {
        this.cid = cid;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    @Override
    public String toString() {
        return "Course{" +
                "cid=" + cid +
                ", cname='" + cname + '\'' +
                '}';
    }
}
<?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.iotek.Dao.StudentDao">
    <!-- 多对多 -->
    <resultMap id="studentMapper" type="Student">
        <id column="sid" property="sid"/>
        <result column="sname" property="sname"/>
        <collection property="courses" ofType="Course">
            <id column="cid" property="cid"/>
            <result column="cname" property="cname" />
        </collection>
    </resultMap>
    <select id="getStudentByIdManyToMany1" parameterType="int" resultMap="studentMapper">
        SELECT sid,sname,cid,cname FROM t_student s,t_course c,t_middle m
        WHERE s.sid = m.studentid AND c.cid = m.courseid AND s.sid = #{sid}
    </select>
</mapper>
package com.iotek.DaoTest;

import com.iotek.Dao.OrderDao;
import com.iotek.Dao.StudentDao;
import com.iotek.pojo.Order;
import com.iotek.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 org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Author:
 * @CreateTime: 2023-02-13  16:22
 */
public class MangToMany {
    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void getsessionFactory() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
    }

    @Test
    public void ManytoMany1(){
        SqlSession session = sqlSessionFactory.openSession();
        StudentDao studentDao = session.getMapper(StudentDao.class);
        Student student = studentDao.getStudentByIdManyToMany1(1);
        System.out.println(student);
    }
}

十三、自查讯

package com.iotek.pojo;

import java.util.Set;

/**
 * @Author:
 * @CreateTime: 2023-02-13  23:59
 */
public class Newlable {
    private int id;
    private String name;
    private Set<Newlable> newlables;//栏目下所有的子栏目

    public Newlable() {
        super();
    }

    public Newlable(int id, String name, Set<Newlable> newlables) {
        this.id = id;
        this.name = name;
        this.newlables = newlables;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set<Newlable> getNewlables() {
        return newlables;
    }

    public void setNewlables(Set<Newlable> newlables) {
        this.newlables = newlables;
    }

    @Override
    public String toString() {
        return "Newlable{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", newlables=" + newlables +
                '}';
    }
}
package com.iotek.Dao;

import com.iotek.pojo.Newlable;

import java.util.List;

/**
 * @Author:
 * @CreateTime: 2023-02-14  00:02
 */
public interface NewlableDao {
    public List<Newlable> getChildrenByPid1(int pid);
    public List<Newlable> getChildrenByPid2(int pid);
}
<?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.iotek.Dao.NewlableDao">
    <!-- 自查讯 -->
    <resultMap id="newlableMapper" type="Newlable">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <collection property="newlables" ofType="Newlable" select="getChildrenByPid1" column="id"></collection>
    </resultMap>
    <select id="getChildrenByPid1" parameterType="int" resultMap="newlableMapper">
        SELECT id,name FROM t_newlable WHERE pid=#{id}
    </select>


    <select id="getChildrenById" resultMap="newlableMapper1">
        SELECT id,name FROM t_newlable WHERE pid=#{id}
    </select>
    <resultMap id="newlableMapper1" type="Newlable">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <collection property="newlables" ofType="Newlable" select="getChildrenById" column="id"></collection>
    </resultMap>
    <select id="getChildrenByPid2" parameterType="int" resultMap="newlableMapper1">
        SELECT id,name FROM t_newlable WHERE pid=#{pid}
    </select>

</mapper>
package com.iotek.DaoTest;

import com.iotek.Dao.NewlableDao;
import com.iotek.Dao.OrderDao;
import com.iotek.pojo.Newlable;
import com.iotek.pojo.Order;
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.Before;
import org.junit.Test;

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

/**
 * @Author:
 * @CreateTime: 2023-02-14  00:17
 */
public class SelectOwn {
    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void getsessionFactory() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
    }

    @Test
    public void ManyToOne1(){
        SqlSession session = sqlSessionFactory.openSession();
        NewlableDao newlableDao = session.getMapper(NewlableDao.class);
        List<Newlable> newlableList = newlableDao.getChildrenByPid1(2);
        System.out.println(newlableList);
    }
    @Test
    public void ManyToOne2(){
        SqlSession session = sqlSessionFactory.openSession();
        NewlableDao newlableDao = session.getMapper(NewlableDao.class);
        List<Newlable> newlableList = newlableDao.getChildrenByPid2(2);
        System.out.println(newlableList);
    }
}

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杪&秋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值