加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2021-05-21 05:02:01 所属栏目:大数据 来源: https://www.jb51.cc
导读:? Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件。 ? 1、相关文件 关于Mybatis-Generator的下载可以到这个地址:https://github.com/mybati

?

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

?

1、相关文件

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

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

以下是相关文件截图:

?

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

generatorConfig.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE generatorConfiguration
 3   PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
 4   "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
 5 <generatorConfiguration 6     <!--数据库驱动-->
 7     classPathEntry    location="MysqL-connector-java-5.0.8-bin.jar"/>
 8     context id="DB2Tables"    targetRuntime="MyBatis3" 9         commentGenerator10             property name="suppressDate" value="true"11             ="suppressAllComments"12         </13         数据库链接地址账号密码14         jdbcConnection driverClass="com.MysqL.jdbc.Driver" connectionURL="jdbc:MysqL://localhost/mymessages" userId="root" password="root"15         jdbcConnection16         javaTypeResolver17             ="forceBigDecimals"="false"18         19         生成Model类存放位置20         javaModelGenerator targetPackage="lcw.model" targetProject="src"21             ="enableSubPackages"22             ="trimStrings"23         javaModelGenerator24         生成映射文件存放位置25         sqlMapGenerator ="lcw.mapping"26             27         sqlMapGenerator28         生成Dao类存放位置29         javaClientGenerator type="XMLMAPPER" targetPackage="lcw.dao"30             31         javaClientGenerator32         生成对应表及类名33         table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample enableDeleteByExample enableSelectByExample selectByExampleQueryId></table34     context35 >

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

上面配置文件中的:

>

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

?

生成语句文件:

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

?

?

2、使用方法

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

?

看下效果图:

首先这个是我的数据库表

?

?

生成相关代码:

Message.java

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

MessgaeMapper.xml

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

MessgaeMapper.java

 lcw.dao;
import lcw.model.Messgae;
 4 
interface MessgaeMapper {
int deleteByPrimaryKey(Integer id);
 insert(Messgae record);
 insertSelective(Messgae record);
12     Messgae selectByPrimaryKey(Integer id);
13 
14      updateByPrimaryKeySelective(Messgae record);
 updateByPrimaryKey(Messgae record);
17 }

?

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读