@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); }
DELETE
继续以 tb_user 表为例,修改映射文件,增加如下配置:
1 2 3
<delete id="delete"> DELETE FROM tb_user WHERE id = #{id} </delete>
单元测试代码如下:
1 2 3 4 5 6 7
@Test public void testDelete() { TbUser tbUser = new TbUser(); tbUser.setId(37L);
tbUserDao.delete(tbUser); }
查询单个对象
继续以 tb_user 表为例,修改映射文件,增加如下配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<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>
<update id="update"> UPDATE tb_user SET username = #{username}, password = #{password}, phone = #{phone}, email = #{email}, created = #{created}, updated = #{update} WHERE id = #{id} </update>
单元测试代码如下:
1 2 3 4 5 6 7
@Test public void testUpdate() { TbUser tbUser = tbUserDao.getById(36L); tbUser.setUsername("Lusifer");
tbUserDao.update(tbUser); }
使用模糊查询
继续以tb_user 表为例,修改映射文件,增加如下配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<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>