目錄
1. 配置打印 MyBatis 執(zhí)行的SQL
2. 查詢操作
2.1 通過(guò)用戶 ID 查詢用戶信息、查詢所有用戶信息
(1) Mapper 接口
(2)UserMapper.xml 查詢所有用戶的具體實(shí)現(xiàn) SQL
(3)進(jìn)行單元測(cè)試
3. 增加操作
3.1 在 mapper(interface)里面添加增加方法的聲明
3.2 在 XMl 中添加 標(biāo)簽和增加的 sql 代碼
3.3 生成測(cè)試類(lèi)
?4.修改操作
4.1 在 mapper(interface)里面添加修改方法的聲明
4.2 在 XMl 中添加 標(biāo)簽和修改的 sql 代碼
4.3 生成測(cè)試類(lèi)
?5. 刪除操作
5.1 在 mapper(interface)里面添加刪除方法的聲明
5.2 在 XMl 中添加 標(biāo)簽和刪除的 sql 代碼
5.3 生成測(cè)試類(lèi)
1. 配置打印 MyBatis 執(zhí)行的SQL
#mybatis 中 xml 保存路徑
mybatis:
mapper-locations:
- classpath:mybatis/**Mapper.xml
configuration: # 配置打印 MyBatis 執(zhí)行的 SQL
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 配置打印 MyBatis 執(zhí)行的 SQL
logging:
level:
com:
example:
demo: debug
可以看出MyBatis的底層就是JDBC,最終還是會(huì)生成JDBC的,只是MyBatis幫我們?nèi)?zhí)行了?
2. 查詢操作
2.1 通過(guò)用戶 ID 查詢用戶信息、查詢所有用戶信息
(1) Mapper 接口
@Mapper// 和五大類(lèi)注解是一樣的
public interface UserMapper {
/**
* 根據(jù)用戶 id 查詢用戶信息
* @param id
* @return
*/
Userinfo getUserById(@Param("id") Integer id);
/**
* 查詢?nèi)? * @return
*/
List<Userinfo> getAllUser();
}
(2)UserMapper.xml 查詢所有用戶的具體實(shí)現(xiàn) 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)進(jìn)行單元測(cè)試
?
單元測(cè)試知識(shí)點(diǎn)擊此處
添加單元測(cè)試業(yè)務(wù)邏輯?
@SpringBootTest// 1.表明當(dāng)前單元測(cè)試是運(yùn)行在Spring Boot環(huán)境中的
@Transactional // 開(kāi)啟一個(gè)事務(wù),執(zhí)行完恢復(fù)數(shù)據(jù)
class UserMapperTest {
@Autowired// 2.注入測(cè)試對(duì)象:屬性注入
private UserMapper userMapper;
@Test
// @Transactional
void getUserById() {
// 3.添加單元測(cè)試的業(yè)務(wù)代碼
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 中添加 標(biāo)簽和增加的 sql 代碼
在這里我們傳的是個(gè)對(duì)象,該怎么寫(xiě)呢?
是不是需要對(duì)象打點(diǎn)呢,不需要,直接去寫(xiě)對(duì)象的具體的屬性就行了
<!-- 只能得到受影響的行數(shù)-->
<insert id="add">
insert into userinfo (username,password,createtime,updatetime)
values(#{username},#{password},#{createtime},#{updatetime})
</insert>
注意:insert 操作只能得到受影響的行數(shù),所以不需要添加resultType
3.3 生成測(cè)試類(lèi)
@SpringBootTest// 1.表明當(dāng)前單元測(cè)試是運(yùn)行在Spring Boot環(huán)境中的
@Transactional // 開(kāi)啟一個(gè)事務(wù),執(zhí)行完恢復(fù)數(shù)據(jù)
class UserMapperTest {
@Autowired// 2.注入測(cè)試對(duì)象:屬性注入
private UserMapper userMapper;
@Test
void add() {
// 偽代碼,構(gòu)建對(duì)象并設(shè)置相應(yīng)的值
Userinfo userinfo = new Userinfo();
userinfo.setUsername("李四");
userinfo.setPassword("123456");
userinfo.setCreatetime(LocalDateTime.now());
userinfo.setUpdatetime(LocalDateTime.now());
// 調(diào)用MyBatis 添加方法執(zhí)行添加操作
int resule = userMapper.add(userinfo);
int uid = userinfo.getId();
System.out.println("user2的ID = " + uid);
Assertions.assertEquals(1,resule);
}
在這里我抱著試一試的想法去看能不能得到用戶的id
可以看見(jiàn)用這個(gè)方法是拿不到的
?4.修改操作
4.1 在 mapper(interface)里面添加修改方法的聲明
@Mapper
public interface UserMapper {
/**
* 修改用戶
*/
int updateUserName(Userinfo userinfo);
}
4.2 在 XMl 中添加 標(biāo)簽和修改的 sql 代碼
<!-- 默認(rèn)返回受影響的行數(shù)-->
<update id="updateUserName">
update userinfo set username=#{username} where id=#{id}
</update>
4.3 生成測(cè)試類(lèi)
@Test
void updateUserName() {
// 偽代碼,構(gòu)建測(cè)試數(shù)據(jù)
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// 和五大類(lèi)注解是一樣的
public interface UserMapper {
/**
* 刪除對(duì)象
*/
int delByName(@Param("id") Integer id);
}
5.2 在 XMl 中添加 標(biāo)簽和刪除的 sql 代碼
<!-- 默認(rèn)返回受影響的行數(shù)-->
<delete id="delByName">
delete from userinfo where id = ${id}
</delete>
5.3 生成測(cè)試類(lèi)
@Test
void delByName() {
Integer id = 5;
int resule = userMapper.delByName(id);
System.out.println("刪除:" + resule);
Assertions.assertEquals(1,resule);
}
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-627505.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-627505.html
到了這里,關(guān)于MyBatis 查詢數(shù)據(jù)庫(kù)之二(增、刪、改、查操作)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!