使用MyBatis框架实现对数据库增删改查

墨蓝 2022-04-17 02:55 338阅读 0赞

第一步:首先需要新建一个数据库,然后建一个表在表里面可以随便插入几条数据,不插入也可以,等下直接用程序插入,今天就用下面这个表举例
这里写图片描述

第二步:创建一个Java工程;
这里写图片描述

第三步:创建一个实体类User,将数据库中的字段名get、set。

  1. package com.zhiyuan.pojo;
  2. public class User {
  3. private int id;
  4. private String name;
  5. private String sex;
  6. public int getId() {
  7. return id;
  8. }
  9. public void setId(int id) {
  10. this.id = id;
  11. }
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. public String getSex() {
  19. return sex;
  20. }
  21. public void setSex(String sex) {
  22. this.sex = sex;
  23. }
  24. }
  25. 1
  26. 2
  27. 3
  28. 4
  29. 5
  30. 6
  31. 7
  32. 8
  33. 9
  34. 10
  35. 11
  36. 12
  37. 13
  38. 14
  39. 15
  40. 16
  41. 17
  42. 18
  43. 19
  44. 20
  45. 21
  46. 22
  47. 23
  48. 24
  49. 25
  50. 26

第四步:写连接数据库的config.xml配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "mybatis-3-config.dtd" >
  3. <configuration>
  4. <typeAliases>
  5. <typeAlias alias="User" type="com.zhiyuan.pojo.User"/>
  6. </typeAliases>
  7. <environments default="development">
  8. <environment id="development">
  9. <transactionManager type="JDBC" />
  10. <dataSource type="POOLED">
  11. <property name="driver" value="com.mysql.jdbc.Driver"/>
  12. <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis" />
  13. <property name="username" value="root" />
  14. <property name="password" value="liuxi" />
  15. </dataSource>
  16. </environment>
  17. </environments>
  18. <mappers>
  19. <mapper resource="com/zhiyuan/pojo/User.xml" />
  20. </mappers>
  21. </configuration>
  22. 1
  23. 2
  24. 3
  25. 4
  26. 5
  27. 6
  28. 7
  29. 8
  30. 9
  31. 10
  32. 11
  33. 12
  34. 13
  35. 14
  36. 15
  37. 16
  38. 17
  39. 18
  40. 19
  41. 20
  42. 21
  43. 22
  44. 23
  45. 24

第五步:配置对应实体类的user.XML文件,里面写MySQL增删改查语句:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
  3. <mapper namespace="com.zhiyuan.dao.Inter">
  4. <!-- 插入数据,这里ID是自动递增的,所有不需要插入 -->
  5. <insert id="getInsert" parameterType="User">
  6. insert into nb(name,sex) values(#{name},#{sex})
  7. </insert>
  8. <!-- 查询表中所有的数据 -->
  9. <select id="getUserList" resultType="com.zhiyuan.pojo.User">
  10. select * from nb
  11. </select>
  12. <!-- 根据ID查询表数据 -->
  13. <select id="getUser" parameterType="int" resultType="com.zhiyuan.pojo.User">
  14. select * from nb where id=#{id}
  15. </select>
  16. <!-- 根据ID更新表数据 -->
  17. <update id="getUpdate" parameterType="com.zhiyuan.pojo.User">
  18. update nb set name=#{name},sex=#{sex} where id=#{id}
  19. </update>
  20. <!-- 根据ID删除表数据 -->
  21. <delete id="getDelete" parameterType="int">
  22. delete from nb where id=#{id}
  23. </delete>
  24. </mapper>
  25. 1
  26. 2
  27. 3
  28. 4
  29. 5
  30. 6
  31. 7
  32. 8
  33. 9
  34. 10
  35. 11
  36. 12
  37. 13
  38. 14
  39. 15
  40. 16
  41. 17
  42. 18
  43. 19
  44. 20
  45. 21
  46. 22
  47. 23
  48. 24
  49. 25
  50. 26
  51. 27
  52. 28
  53. 29

第六步:写对应user.xml的接口类。

  1. package com.zhiyuan.dao;
  2. import java.util.List;
  3. import com.zhiyuan.pojo.User;
  4. public interface Inter {
  5. public List<User> getUserList();
  6. public User getUser(int id);
  7. public void getInsert(User user);
  8. public void getUpdate(User user);
  9. public void getDelete(int id);
  10. }
  11. 1
  12. 2
  13. 3
  14. 4
  15. 5
  16. 6
  17. 7
  18. 8
  19. 9
  20. 10
  21. 11
  22. 12
  23. 13

最后一步写测试类:

  1. package com.zhiyuan.main;
  2. import java.io.IOException;
  3. import java.io.Reader;
  4. import java.util.List;
  5. import org.apache.ibatis.io.Resources;
  6. import org.apache.ibatis.session.SqlSession;
  7. import org.apache.ibatis.session.SqlSessionFactory;
  8. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  9. import com.zhiyuan.dao.Inter;
  10. import com.zhiyuan.pojo.User;
  11. public class Main {
  12. static SqlSessionFactory ssf;
  13. static SqlSession ss;
  14. public static void main(String[] args) throws IOException {
  15. Reader re = Resources.getResourceAsReader("conf.xml");
  16. ssf = new SqlSessionFactoryBuilder().build(re);
  17. ss = ssf.openSession();
  18. insert();
  19. // userList();
  20. // user();
  21. // update();
  22. // delete();
  23. }
  24. //根据ID删除字段
  25. private static void delete() {
  26. ss = ssf.openSession();
  27. Inter ie = ss.getMapper(Inter.class);
  28. ie.getDelete(1);
  29. ss.commit();
  30. System.out.println("删除后:");
  31. userList();
  32. }
  33. //根据ID更新表数据
  34. private static void update() {
  35. ss = ssf.openSession();
  36. Inter ite = ss.getMapper(Inter.class);
  37. User us = ite.getUser(3);
  38. us.setName("徐深何");
  39. us.setSex("不详");
  40. ite.getUpdate(us);
  41. ss.commit();
  42. System.out.println("更新后:");
  43. userList();
  44. }
  45. //条件查询
  46. private static void user() {
  47. ss = ssf.openSession();
  48. Inter it = ss.getMapper(Inter.class);
  49. User user = it.getUser(3);
  50. if(user != null){
  51. System.out.println("姓名:" + user.getName());
  52. System.out.println( "性别:" + user.getSex());
  53. }
  54. }
  55. //查询全部
  56. private static void userList() {
  57. ss = ssf.openSession();
  58. Inter is = ss.getMapper(Inter.class);
  59. print(is.getUserList());
  60. }
  61. private static void print(List<User> userList) {
  62. for(User us : userList){
  63. System.out.println("编号:" + us.getId());
  64. System.out.println("姓名:" + us.getName());
  65. System.out.println("性别:" + us.getSex());
  66. }
  67. }
  68. //插入
  69. private static void insert() {
  70. ss = ssf.openSession();
  71. Inter in = ss.getMapper(Inter.class);
  72. User user = new User();
  73. user.setName("CSDN");
  74. user.setSex("????");
  75. in.getInsert(user);
  76. ss.commit();
  77. System.out.println("插入成功");
  78. userList();
  79. }
  80. }
  81. 1
  82. 2
  83. 3
  84. 4
  85. 5
  86. 6
  87. 7
  88. 8
  89. 9
  90. 10
  91. 11
  92. 12
  93. 13
  94. 14
  95. 15
  96. 16
  97. 17
  98. 18
  99. 19
  100. 20
  101. 21
  102. 22
  103. 23
  104. 24
  105. 25
  106. 26
  107. 27
  108. 28
  109. 29
  110. 30
  111. 31
  112. 32
  113. 33
  114. 34
  115. 35
  116. 36
  117. 37
  118. 38
  119. 39
  120. 40
  121. 41
  122. 42
  123. 43
  124. 44
  125. 45
  126. 46
  127. 47
  128. 48
  129. 49
  130. 50
  131. 51
  132. 52
  133. 53
  134. 54
  135. 55
  136. 56
  137. 57
  138. 58
  139. 59
  140. 60
  141. 61
  142. 62
  143. 63
  144. 64
  145. 65
  146. 66
  147. 67
  148. 68
  149. 69
  150. 70
  151. 71
  152. 72
  153. 73
  154. 74
  155. 75
  156. 76
  157. 77
  158. 78
  159. 79
  160. 80
  161. 81
  162. 82
  163. 83
  164. 84
  165. 85
  166. 86
  167. 87
  168. 88
  169. 89
  170. 90

最后输出结果为:
这里写图片描述
这里值得注意得到地方就是写配置文件时一定要细心,不然写到最后一运行一定会出现各种各样的错,找起来头痛。

发表评论

表情:
评论列表 (有 0 条评论,338人围观)

还没有评论,来说两句吧...

相关阅读