User user = new User(); user.setUserName("AAA"); user.setUserAddress("北京市顺义区"); user.setUserSex("男"); user.setUserBirthday(new Date()); userDao.saveUser(user);
可以在新增用户后获取id
在saveUser前后打印user,可以发现id从null变为值
删
1 2 3 4
<!-- 删除用户--> <deleteid="deleteUser"parameterType="java.lang.Integer"> delete from user where id = #{uid} </delete>
这里uid,传基本类型或基本类型包装类时,占位符可以随便写
1
userDao.deleteUser(48);
改
1 2 3 4
<!-- 更新用户 --> <updateid="updateUser"parameterType="com.itheima.domain.User"> update user set username=#{userName},address=#{userAddress},sex=#{userAex},birthday=#{userBirthday} where id=#{userId} </update>
1 2 3 4 5 6 7
User user = new User(); user.setUserId(50); user.setUserName("BBB"); user.setUserAddress("北京市顺义区"); user.setUserSex("女"); user.setUserBirthday(new Date()); userDao.updateUser(user);
查
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<!-- 根据id查询用户 --> <selectid="findById"parameterType="int"resultType="com.itheima.domain.User"> select * from user where id = #{uid} </select>
<!-- 根据名称模糊查询 --> <selectid="findByName"parameterType="string"resultType="com.itheima.domain.User"> select * from user where username like #{name} </select>
<!-- 获取用户的总记录条数 --> <selectid="findTotal"resultType="int"> select count(id) from user; </select>
1 2 3 4 5 6 7 8 9 10 11 12 13 14
@Test publicvoidtestFindOne(){ User user = userDao.findById(50); }