如何通过MyBatis实现增删改

UserMapper.xml

[html] view plain copy

  1. <!--增加 -->
  2. <insert id="add" parameterType="User">
  3. insert into smbms_user(userCode,userName,userPassword,gender,birthday,phone,address,userRole,createdBy,creationDate)
  4. values(#{userCode},#{userName},#{userPassword},#{gender},#{birthday},#{phone},#{address},#{userRole},#{createdBy},#{creationDate})
  5. </insert>
  6. <!-- 修改(4个起参数用对象)-->
  7. <update id="updatee" parameterType="User">
  8. update smbms_user set userCode=#{userCode},userName=#{userName},userPassword=#{userPassword},
  9. gender=#{gender},birthday=#{birthday},phone=#{phone},address=#{address},userRole=#{userRole},modifyBy=#{modifyBy},modifyDate=#{modifyDate}
  10. where id=#{id}
  11. </update>
  12. <!-- 修改密码(1-3参数使用@param) -->
  13. <update id="updatePwd">
  14. update smbms_user set userPassword=#{userPassword} where id=#{id}
  15. </update>
  16. <!-- 删除 -->
  17. <delete id="deletee" parameterType="int">
  18. delete from smbms_user where id=#{id}
  19. </delete>

UserMapper.java

[html] view plain copy

  1. //增加
  2. public int add(User user);
  3. //修改
  4. public int updatee(User user);
  5. //修改密码
  6. public int updatePwd(@Param("id")Integer id,@Param("userPassword")String pwd);
  7. //删除
  8. public int deletee(Integer id);

MapperTest.java

[html] view plain copy

  1. //添加数据条
  2. @Test
  3. public void test9() {
  4. SqlSession sqlSession=null;
  5. int count=0;
  6. try {
  7. sqlSession=MyBatisUtil.createSqlSession();
  8. User user=new User();
  9. user.setUserCode("003");
  10. user.setUserName("用户0011");
  11. user.setUserPassword("1234561");
  12. //如何添加Date类型的生日日期
  13. Date birthday=new SimpleDateFormat("yyyy-MM-dd").parse("2000-11-11");
  14. user.setBirthday(birthday);
  15. //如何添加 创建日期
  16. user.setCreationDate(new Date());
  17. user.setAddress("南京1");
  18. user.setGender(1);
  19. user.setPhone("13367891214");
  20. user.setUserRole(1);
  21. user.setCreatedBy(1);
  22. count=sqlSession.getMapper(UserMapper.class).add(user);
  23. } catch (Exception e) {
  24. // TODO Auto-generated catch block
  25. e.printStackTrace();
  26. }finally{
  27. MyBatisUtil.closeSqlSession(sqlSession);
  28. }
  29. System.out.println(count);
  30. }
  31. //修改某数据条中的3个以上字段对应的数据内容
  32. @Test
  33. public void test10() {
  34. SqlSession sqlSession=null;
  35. int count=0;
  36. try {
  37. sqlSession=MyBatisUtil.createSqlSession();
  38. User user=new User();
  39. user.setId(18);
  40. user.setUserCode("17");
  41. user.setUserName("用户00");
  42. user.setUserPassword("1234");
  43. Date birthday=new SimpleDateFormat("yyyy-MM-dd").parse("2000-11-11");
  44. user.setBirthday(birthday);
  45. user.setCreationDate(new Date());
  46. user.setAddress("南京12");
  47. user.setGender(1);
  48. user.setPhone("133678912344");
  49. user.setUserRole(2);
  50. //user.setCreatedBy(1);
  51. user.setModifyBy(1);
  52. user.setModifyDate(new Date());
  53. count=sqlSession.getMapper(UserMapper.class).updatee(user);
  54. } catch (Exception e) {
  55. // TODO Auto-generated catch block
  56. e.printStackTrace();
  57. }finally{
  58. MyBatisUtil.closeSqlSession(sqlSession);
  59. }
  60. System.out.println(count);
  61. }
  62. //修改某数据条的3个之内字段对应的数据内容
  63. @Test
  64. public void test11() {
  65. SqlSession sqlSession=null;
  66. int count=0;
  67. //密码和 id两个字段
  68. String pwd="666888";
  69. Integer id=1;
  70. try {
  71. sqlSession=MyBatisUtil.createSqlSession();
  72. count=sqlSession.getMapper(UserMapper.class).updatePwd(id, pwd);
  73. } catch (Exception e) {
  74. // TODO Auto-generated catch block
  75. e.printStackTrace();
  76. }finally{
  77. MyBatisUtil.closeSqlSession(sqlSession);
  78. }
  79. System.out.println(count);
  80. }
  81. //删除数据条
  82. @Test
  83. public void test12() {
  84. SqlSession sqlSession=null;
  85. int count=0;
  86. Integer id=19;
  87. try {
  88. sqlSession=MyBatisUtil.createSqlSession();
  89. count=sqlSession.getMapper(UserMapper.class).deletee(id);
  90. } catch (Exception e) {
  91. // TODO Auto-generated catch block
  92. e.printStackTrace();
  93. }finally{
  94. MyBatisUtil.closeSqlSession(sqlSession);
  95. }
  96. System.out.println(count);
  97. }

如何通过MyBatis实现增删改

相关推荐