使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(转)

今天药忘吃喽~ 2022-04-02 07:50 240阅读 0赞

转载自https://www.cnblogs.com/smileberry/p/4145872.html

出处:http://www.cnblogs.com/lichenwei/p/4145696.html

Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件。

1、相关文件

关于Mybatis-Generator的下载可以到这个地址:https://github.com/mybatis/generator/releases

由于我使用的是Mysql数据库,这里需要在准备一个连接mysql数据库的驱动jar包

以下是相关文件截图:

20181225201250286

和Hibernate逆向生成一样,这里也需要一个配置文件:

generatorConfig.xml

按 Ctrl+C 复制代码

按 Ctrl+C 复制代码

需要修改文件配置的地方我都已经把注释标注出来了,这里的相关路径(如数据库驱动包,生成对应的相关文件位置可以自定义)不能带有中文。

上面配置文件中的:

  1. <table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>

tableName和domainObjectName为必选项,分别代表数据库表名和生成的实力类名,其余的可以自定义去选择(一般情况下均为false)。

生成语句文件:

  1. java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

2、使用方法

在该目录按住Shift键,右键鼠标选择”在此处打开命令窗口”,复制粘贴生成语句的文件代码即可。

看下效果图:

20181225201250322

20181225201250341

生成相关代码:

Message.java

复制代码

复制代码

  1. 1 package lcw.model;
  2. 2
  3. 3 public class Messgae {
  4. 4 private Integer id;
  5. 5
  6. 6 private String title;
  7. 7
  8. 8 private String describe;
  9. 9
  10. 10 private String content;
  11. 11
  12. 12 public Integer getId() {
  13. 13 return id;
  14. 14 }
  15. 15
  16. 16 public void setId(Integer id) {
  17. 17 this.id = id;
  18. 18 }
  19. 19
  20. 20 public String getTitle() {
  21. 21 return title;
  22. 22 }
  23. 23
  24. 24 public void setTitle(String title) {
  25. 25 this.title = title == null ? null : title.trim();
  26. 26 }
  27. 27
  28. 28 public String getDescribe() {
  29. 29 return describe;
  30. 30 }
  31. 31
  32. 32 public void setDescribe(String describe) {
  33. 33 this.describe = describe == null ? null : describe.trim();
  34. 34 }
  35. 35
  36. 36 public String getContent() {
  37. 37 return content;
  38. 38 }
  39. 39
  40. 40 public void setContent(String content) {
  41. 41 this.content = content == null ? null : content.trim();
  42. 42 }
  43. 43 }

复制代码

复制代码

MessgaeMapper.xml

复制代码

复制代码

  1. 1 <?xml version="1.0" encoding="UTF-8" ?>
  2. 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. 3 <mapper namespace="lcw.dao.MessgaeMapper" >
  4. 4 <resultMap id="BaseResultMap" type="lcw.model.Messgae" >
  5. 5 <id column="id" property="id" jdbcType="INTEGER" />
  6. 6 <result column="title" property="title" jdbcType="VARCHAR" />
  7. 7 <result column="describe" property="describe" jdbcType="VARCHAR" />
  8. 8 <result column="content" property="content" jdbcType="VARCHAR" />
  9. 9 </resultMap>
  10. 10 <sql id="Base_Column_List" >
  11. 11 id, title, describe, content
  12. 12 </sql>
  13. 13 <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
  14. 14 select
  15. 15 <include refid="Base_Column_List" />
  16. 16 from message
  17. 17 where id = #{id,jdbcType=INTEGER}
  18. 18 </select>
  19. 19 <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
  20. 20 delete from message
  21. 21 where id = #{id,jdbcType=INTEGER}
  22. 22 </delete>
  23. 23 <insert id="insert" parameterType="lcw.model.Messgae" >
  24. 24 insert into message (id, title, describe,
  25. 25 content)
  26. 26 values (#{id,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, #{describe,jdbcType=VARCHAR},
  27. 27 #{content,jdbcType=VARCHAR})
  28. 28 </insert>
  29. 29 <insert id="insertSelective" parameterType="lcw.model.Messgae" >
  30. 30 insert into message
  31. 31 <trim prefix="(" suffix=")" suffixOverrides="," >
  32. 32 <if test="id != null" >
  33. 33 id,
  34. 34 </if>
  35. 35 <if test="title != null" >
  36. 36 title,
  37. 37 </if>
  38. 38 <if test="describe != null" >
  39. 39 describe,
  40. 40 </if>
  41. 41 <if test="content != null" >
  42. 42 content,
  43. 43 </if>
  44. 44 </trim>
  45. 45 <trim prefix="values (" suffix=")" suffixOverrides="," >
  46. 46 <if test="id != null" >
  47. 47 #{id,jdbcType=INTEGER},
  48. 48 </if>
  49. 49 <if test="title != null" >
  50. 50 #{title,jdbcType=VARCHAR},
  51. 51 </if>
  52. 52 <if test="describe != null" >
  53. 53 #{describe,jdbcType=VARCHAR},
  54. 54 </if>
  55. 55 <if test="content != null" >
  56. 56 #{content,jdbcType=VARCHAR},
  57. 57 </if>
  58. 58 </trim>
  59. 59 </insert>
  60. 60 <update id="updateByPrimaryKeySelective" parameterType="lcw.model.Messgae" >
  61. 61 update message
  62. 62 <set >
  63. 63 <if test="title != null" >
  64. 64 title = #{title,jdbcType=VARCHAR},
  65. 65 </if>
  66. 66 <if test="describe != null" >
  67. 67 describe = #{describe,jdbcType=VARCHAR},
  68. 68 </if>
  69. 69 <if test="content != null" >
  70. 70 content = #{content,jdbcType=VARCHAR},
  71. 71 </if>
  72. 72 </set>
  73. 73 where id = #{id,jdbcType=INTEGER}
  74. 74 </update>
  75. 75 <update id="updateByPrimaryKey" parameterType="lcw.model.Messgae" >
  76. 76 update message
  77. 77 set title = #{title,jdbcType=VARCHAR},
  78. 78 describe = #{describe,jdbcType=VARCHAR},
  79. 79 content = #{content,jdbcType=VARCHAR}
  80. 80 where id = #{id,jdbcType=INTEGER}
  81. 81 </update>
  82. 82 </mapper>

复制代码

复制代码

MessgaeMapper.java

复制代码

复制代码

  1. 1 package lcw.dao;
  2. 2
  3. 3 import lcw.model.Messgae;
  4. 4
  5. 5 public interface MessgaeMapper {
  6. 6 int deleteByPrimaryKey(Integer id);
  7. 7
  8. 8 int insert(Messgae record);
  9. 9
  10. 10 int insertSelective(Messgae record);
  11. 11
  12. 12 Messgae selectByPrimaryKey(Integer id);
  13. 13
  14. 14 int updateByPrimaryKeySelective(Messgae record);
  15. 15
  16. 16 int updateByPrimaryKey(Messgae record);
  17. 17 }

复制代码

复制代码

发表评论

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

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

相关阅读