MyBatis 查询数据库之二(增、删、改、查操作)

本文详细介绍了如何在MyBatis中配置打印SQL、执行查询、增加、修改和删除操作,以及如何编写单元测试。内容包括Mapper接口的定义、XML映射文件中的SQL编写和SpringBootTest下的测试用例。
摘要由CSDN通过智能技术生成

目录

1. 配置打印 MyBatis 执行的SQL

2. 查询操作

2.1 通过用户 ID 查询用户信息、查询所有用户信息

(1) Mapper 接口

(2)UserMapper.xml 查询所有用户的具体实现 SQL

(3)进行单元测试

3. 增加操作

3.1 在 mapper(interface)里面添加增加方法的声明

3.2 在 XMl 中添加 标签和增加的 sql 代码

3.3 生成测试类

 4.修改操作

4.1 在 mapper(interface)里面添加修改方法的声明

4.2 在 XMl 中添加 标签和修改的 sql 代码

4.3 生成测试类

 5. 删除操作

5.1 在 mapper(interface)里面添加删除方法的声明

5.2 在 XMl 中添加 标签和删除的 sql 代码

5.3 生成测试类


1. 配置打印 MyBatis 执行的SQL

#mybatis 中 xml 保存路径
mybatis:
  mapper-locations:
    - classpath:mybatis/**Mapper.xml
  configuration: # 配置打印 MyBatis 执行的 SQL
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

# 配置打印 MyBatis 执行的 SQL
logging:
  level:
    com:
      example:
        demo: debug

可以看出MyBatis的底层就是JDBC,最终还是会生成JDBC的,只是MyBatis帮我们去执行了 

2. 查询操作

2.1 通过用户 ID 查询用户信息、查询所有用户信息

(1) Mapper 接口

@Mapper// 和五大类注解是一样的
public interface UserMapper {
    /**
     * 根据用户 id 查询用户信息
     * @param id
     * @return
     */
    Userinfo getUserById(@Param("id") Integer id);

    /**
     * 查询全部
     * @return
     */
    List<Userinfo> getAllUser();
}

(2)UserMapper.xml 查询所有用户的具体实现 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.example.ssmdemo1.mapper.UserMapper">
    <select id="getUserById" resultType="com.example.ssmdemo1.entity.Userinfo">
        select * from userinfo where id=#{id}
    </select>
    <select id="getAll" resultType="com.example.ssmdemo1.entity.Userinfo">
        select * from userinfo
    </select>
</mapper>

(3)进行单元测试

 

单元测试知识点击此处

添加单元测试业务逻辑 

@SpringBootTest// 1.表明当前单元测试是运行在Spring Boot环境中的
@Transactional // 开启一个事务,执行完恢复数据
class UserMapperTest {

    @Autowired// 2.注入测试对象:属性注入
    private UserMapper userMapper;

    @Test
//    @Transactional
    void getUserById() {
        // 3.添加单元测试的业务代码
        Userinfo userinfo = userMapper.getUserById(1);
        System.out.println(userinfo);
        Assertions.assertEquals("admin",userinfo.getUsername());
    }



    @Test
    void getAllUser() {
        List<Userinfo> list = userMapper.getAllUser();
        Assertions.assertEquals(1,list.size());
    }
}

3. 增加操作

3.1 在 mapper(interface)里面添加增加方法的声明

/**
     * 添加操作
     * @return
     */
    int add(Userinfo userinfo);

3.2 在 XMl 中添加 标签和增加的 sql 代码

在这里我们传的是个对象,该怎么写呢?

是不是需要对象打点呢,不需要,直接去写对象的具体的属性就行了

<!--    只能得到受影响的行数-->
    <insert id="add">
        insert into userinfo (username,password,createtime,updatetime)
        values(#{username},#{password},#{createtime},#{updatetime})
    </insert>

注意:insert 操作只能得到受影响的行数,所以不需要添加resultType

3.3 生成测试类

@SpringBootTest// 1.表明当前单元测试是运行在Spring Boot环境中的
@Transactional // 开启一个事务,执行完恢复数据
class UserMapperTest {

    @Autowired// 2.注入测试对象:属性注入
    private UserMapper userMapper;

    @Test
    void add() {
        // 伪代码,构建对象并设置相应的值
        Userinfo userinfo = new Userinfo();
        userinfo.setUsername("李四");
        userinfo.setPassword("123456");
        userinfo.setCreatetime(LocalDateTime.now());
        userinfo.setUpdatetime(LocalDateTime.now());
        // 调用MyBatis 添加方法执行添加操作
        int resule = userMapper.add(userinfo);

        int uid = userinfo.getId();
        System.out.println("user2的ID = " + uid);

        Assertions.assertEquals(1,resule);
    }

在这里我抱着试一试的想法去看能不能得到用户的id

可以看见用这个方法是拿不到的

 4.修改操作

4.1 在 mapper(interface)里面添加修改方法的声明

@Mapper
public interface UserMapper {

   /**
     * 修改用户
     */
    int updateUserName(Userinfo userinfo);
}

4.2 在 XMl 中添加 标签和修改的 sql 代码

<!--    默认返回受影响的行数-->
    <update id="updateUserName">
        update userinfo set username=#{username} where id=#{id}
    </update>

4.3 生成测试类

 @Test
    void updateUserName() {
        // 伪代码,构建测试数据
        Userinfo userinfo = new Userinfo();
        userinfo.setId(5);// 修改id为5的用户
        userinfo.setUsername("老五");
        int result = userMapper.updateUserName(userinfo);

        System.out.println("修改:" + result);
        Assertions.assertEquals(1,result);
    }

 5. 删除操作

5.1 在 mapper(interface)里面添加删除方法的声明

@Mapper// 和五大类注解是一样的
public interface UserMapper {

    /**
     * 删除对象
     */
    int delByName(@Param("id") Integer id);
}

5.2 在 XMl 中添加 标签和删除的 sql 代码

<!--    默认返回受影响的行数-->
    <delete id="delByName">
        delete from userinfo where id = ${id}
    </delete>

5.3 生成测试类

 @Test
    void delByName() {
        Integer id = 5;
        int resule = userMapper.delByName(id);
        System.out.println("删除:" + resule);
        Assertions.assertEquals(1,resule);
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

豆腐乾净找方规

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

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

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

打赏作者

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

抵扣说明:

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

余额充值