jck28 - 小柒 - 后端接口开发 - MyBatis 增删改查

一,添加配置文件TestUserMapping.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">

<!--  namespace:命名空间,与sql标签的id属性共同构成唯一标识-->
<mapper namespace="findUser">
    <!--  resultType:返回结果类型,完成自动的映射封装,value为对应封装的实体类全路径(包名+类名)-->
    <select id ="findAll" resultType="com/ceshiren/entity/TestUser">  <!--这里建立映射关系-->
        SELECT * FROM test_user
    </select>

    <select id ="findById" parameterType="int">
        SELECT * FROM test_user where id=#{id}
    </select>

<!--新增   #{}是占位符,引用实体类中的属性的值 -->
<!--    parameterType :指定要插入的数据类型,即实体类-->
    <insert id ="insertUser" parameterType="com/ceshiren/entity/TestUser">  <!--这里建立映射关系-->
        INSERT * INTO test_user(user_name,password,email,auto_create_case_job_name,start_test_job_name,create_time,update_time)
        values (#{username},#{password},#{email},#{autoCreateCaseJobName},#{startTestJobName},#{createTime},#{updateTime})
    </insert>


<!--    修改数据-->
    <update id ="updatetUser" parameterType="com/ceshiren/entity/TestUser">  <!--这里建立映射关系-->
        UPDATE test_user SET user_name=#{username}, password=#{password}, email=#{email} where id=395
    </update>

    <!--删除-->
<!--    parameterType 为参数类型-->
    <delete id="deleteUser" parameterType="java.lang.Integer">
        delete from test_user where id = #{id}
    </delete>


</mapper>

二,编写对应的测试代码

package com.ceshiren;

import com.ceshiren.entity.TestUser;
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.jupiter.api.Test;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;


public class MybatisTest {

    @Test
    void mybatisTest(){
        try {
//          (1) 加载核心配置文件
            InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
//          (2) 获取sqlSessionFactory工厂对象
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
//          (3) 获取sqlSession会话对象
            SqlSession sqlSession = build.openSession();
//          (4) 执行sql(获取TestUserMapping文件配置的数据sql,s为 namespace + id
            List<Object> selectList = sqlSession.selectList("findUser.findAll", TestUser.class);
//          (5) 打印结果
            selectList.forEach(System.out::println);
//          (6) 释放资源
            sqlSession.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    void insertUser() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = build.openSession();
        TestUser testUser = new TestUser();
        testUser.setUserName("demi");
        testUser.setPassword("123456");
        testUser.setEmail("1265967088@qq.com");
        testUser.setCreateTime(LocalDate.now());
        int insert = sqlSession.insert("user.insertUser", testUser);
        System.out.println(insert);
        sqlSession.commit();   //必须要加上事务提交,不然数据插入不会成功
        sqlSession.close();

    }

    @Test
    void updateUser() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = build.openSession();

        TestUser testUser = new TestUser();
        testUser.setId(395);
        testUser.setUserName("hogwarts");
        testUser.setPassword("semi123");
        testUser.setEmail("hogwarts@qq.com");
        int update = sqlSession.update("user.updateUser", testUser);
        System.out.println(update);
        sqlSession.commit();
        sqlSession.close();
    }


    @Test
    void findById() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = build.openSession();

        TestUser selectOne = sqlSession.selectOne("user.findById", 395);   //395为查询的id
        System.out.println(selectOne);
        sqlSession.close();
    }

    @Test
    void deleteOne() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = build.openSession();
        int delete = sqlSession.delete("user.deleteUser", 395);
        System.out.println(delete);
        sqlSession.commit();
        sqlSession.close();
    }
}