继续以 tb_user
表为例,修改映射文件,增加如下配置:
<insert id="insert">
INSERT INTO tb_user (
id,
username,
password,
phone,
email,
created,
updated
)
VALUES
(
#{id},
#{username},
#{password},
#{phone},
#{email},
#{created},
#{update}
)
</insert>
单元测试代码如下:
@Test
public void testInsert() {
TbUser tbUser = new TbUser();
tbUser.setEmail("admin@admin.com");
tbUser.setPassword("admin");
tbUser.setPhone("15888888888");
tbUser.setUsername("Lusifer");
tbUser.setCreated(new Date());
tbUser.setUpdate(new Date());
tbUserDao.insert(tbUser);
}
继续以 tb_user
表为例,修改映射文件,增加如下配置:
<delete id="delete">
DELETE FROM tb_user WHERE id = #{id}
</delete>
单元测试代码如下:
@Test
public void testDelete() {
TbUser tbUser = new TbUser();
tbUser.setId(37L);
tbUserDao.delete(tbUser);
}
继续以 tb_user
表为例,修改映射文件,增加如下配置:
<select id="getById" resultType="TbUser">
SELECT
a.id,
a.username,
a.password,
a.phone,
a.email,
a.created,
a.updated AS "update"
FROM
tb_user AS a
WHERE
a.id = #{id}
</select>
单元测试代码如下:
@Test
public void testGetById() {
TbUser tbUser = tbUserDao.getById(36L);
System.out.println(tbUser.getUsername());
}
继续以 tb_user
表为例,修改映射文件,增加如下配置:
<update id="update">
UPDATE
tb_user
SET
username = #{username},
password = #{password},
phone = #{phone},
email = #{email},
created = #{created},
updated = #{update}
WHERE id = #{id}
</update>
单元测试代码如下:
@Test
public void testUpdate() {
TbUser tbUser = tbUserDao.getById(36L);
tbUser.setUsername("Lusifer");
tbUserDao.update(tbUser);
}
继续以tb_user
表为例,修改映射文件,增加如下配置:
<select id="selectByName" resultType="TbUser">
SELECT
a.id,
a.username,
a.password,
a.phone,
a.email,
a.created,
a.updated AS "update"
FROM
tb_user AS a
WHERE
a.username LIKE CONCAT ('%', #{username}, '%')
</select>
在进行模糊查询时,需要进行字符串的拼接。SQL 中的字符串的拼接使用的是函数concat(arg1, arg2, …)
。注意不能使用 Java 中的字符串连接符 +。
单元测试代码如下:
@Test
public void testSelectByName() {
List<TbUser> tbUsers = tbUserDao.selectByName("uni");
for (TbUser tbUser : tbUsers) {
System.out.println(tbUser.getUsername());
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/qq_45297578/article/details/121677480
内容来源于网络,如有侵权,请联系作者删除!