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

摘要:
--数据库链接地址账号密码--˃141516171819202122232425262728˂!

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

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

1、相关文件

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

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

我自己实际用到的是第一个和第三个文件,

可以自己新建一个文件夹叫做mybatis-generator,解压源码后,将解压的文件放置新建的src文件内部,解压第一个文件中,将其中lib下的jar提取出来放置与src同级别的目录下,将下方介绍的config.xml文件也放置一起,最终形成的文件夹目录如下,这里用的是sqlserver:

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

下面这个是用mysql,

如果是Mysql数据库,这里需要在准备一个连接mysql数据库的驱动mysql-connector-java jar包

如果是使用sqlserver,需要准备sqljdbc4.jar包

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

需要添加的依赖

1 <dependency> 
2     <groupId > org.mybatis.generator </groupId >
3     <artifactId > mybatis-generator </artifactId >
4     <version > 1.3.7 </version >
5 </dependency >
6 <dependency> 
7     <groupId > org.mybatis.generator </groupId >
8     <artifactId > mybatis-generator-maven-plugin </artifactId >
9     <version > 1.3.7 </version >
10 </dependency >

需要的一个配置文件:

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         <commentGenerator>
10             <property name="suppressDate"value="true"/>
11             <property name="suppressAllComments"value="true"/>
12         </commentGenerator>
13         <!--数据库链接地址账号密码-->
14         <jdbcConnection driverClass="com.mysql.jdbc.Driver"connectionURL="jdbc:mysql://localhost/mymessages"userId="root"password="root">
15         </jdbcConnection>
16         <javaTypeResolver>
17             <property name="forceBigDecimals"value="false"/>
18         </javaTypeResolver>
19         <!--生成Model类存放位置-->
20         <javaModelGenerator targetPackage="lcw.model"targetProject="src">
21             <property name="enableSubPackages"value="true"/>
22             <property name="trimStrings"value="true"/>
23         </javaModelGenerator>
24         <!--生成映射文件存放位置-->
25         <sqlMapGenerator targetPackage="lcw.mapping"targetProject="src">
26             <property name="enableSubPackages"value="true"/>
27         </sqlMapGenerator>
28         <!--生成Dao类存放位置-->
29         <javaClientGenerator type="XMLMAPPER"targetPackage="lcw.dao"targetProject="src">
30             <property name="enableSubPackages"value="true"/>
31         </javaClientGenerator>
32         <!--生成对应表及类名-->
33         <table tableName="message"domainObjectName="Messgae"enableCountByExample="false"enableUpdateByExample="false"enableDeleteByExample="false"enableSelectByExample="false"selectByExampleQueryId="false"></table>
34     </context>
35 </generatorConfiguration>

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

上面配置文件中的:

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

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

下面是sqlserver的配置方式

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="sqljdbc4-4.2.jar"/>
8     <context id="DB2Tables"targetRuntime="MyBatis3">
9         <commentGenerator>
10             <property name="suppressDate"value="true"/>
11             <property name="suppressAllComments"value="true"/>
12         </commentGenerator>
13         <!--数据库链接地址账号密码-->
14         <jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"connectionURL="jdbc:sqlserver://127.0.0.1:1433;databaseName=aliceDataBase"userId="sa"password="****">
15         </jdbcConnection>
16         <javaTypeResolver>
17             <property name="forceBigDecimals"value="false"/>
18         </javaTypeResolver>
19         <!--生成Model类存放位置-->
20         <javaModelGenerator targetPackage="com.alice.bean"targetProject="src">
21             <property name="enableSubPackages"value="true"/>
22             <property name="trimStrings"value="true"/>
23         </javaModelGenerator>
24         <!--生成映射文件存放位置-->
25         <sqlMapGenerator targetPackage="com.alice.mapper"targetProject="src">
26             <property name="enableSubPackages"value="true"/>
27         </sqlMapGenerator>
28         <!--生成Dao类存放位置-->
29         <javaClientGenerator type="XMLMAPPER"targetPackage="com.alice.dao"targetProject="src">
30             <property name="enableSubPackages"value="true"/>
31         </javaClientGenerator>
32         <!--生成对应表及类名-->
33         <table tableName="department"domainObjectName="Department"enableCountByExample="false"enableUpdateByExample="false"enableDeleteByExample="false"enableSelectByExample="false"selectByExampleQueryId="false"></table>
34     </context>
35 </generatorConfiguration>

生成语句文件:

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

2、使用方法

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

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

但是win10出现的是powershell命令窗口,所以我是直接用windows+R,输入cmd命令,然后进入其文件夹下面,执行此命令,上述是网上的窗口,下面是自己的运行窗口,所以目录可能不一样

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

看下效果图:

这是用网上m'ysql对应配置文件的版本

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

下面是使用sqlserver版本的,,我自己创建的

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

生成相关代码mysql版本的:

Message.java

1 packagelcw.model;
2 
3 public classMessgae {
4     privateInteger id;
5 
6     privateString title;
7 
8     privateString describe;
9 
10     privateString content;
11 
12     publicInteger getId() {
13         returnid;
14 }
15 
16     public voidsetId(Integer id) {
17         this.id =id;
18 }
19 
20     publicString getTitle() {
21         returntitle;
22 }
23 
24     public voidsetTitle(String title) {
25         this.title = title == null ? null: title.trim();
26 }
27 
28     publicString getDescribe() {
29         returndescribe;
30 }
31 
32     public voidsetDescribe(String describe) {
33         this.describe = describe == null ? null: describe.trim();
34 }
35 
36     publicString getContent() {
37         returncontent;
38 }
39 
40     public voidsetContent(String content) {
41         this.content = content == null ? null: content.trim();
42 }
43 }

MessgaeMapper.xml

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

MessgaeMapper.java

1 packagelcw.dao;
2 
3 importlcw.model.Messgae;
4 
5 public interfaceMessgaeMapper {
6     intdeleteByPrimaryKey(Integer id);
7 
8     intinsert(Messgae record);
9 
10     intinsertSelective(Messgae record);
11 
12 Messgae selectByPrimaryKey(Integer id);
13 
14     intupdateByPrimaryKeySelective(Messgae record);
15 
16     intupdateByPrimaryKey(Messgae record);
17 }

免责声明:文章转载自《使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ACE安装和配置JWT黑名单和白名单下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

在workbench中导入.sql文件!(导入数据库文件)

第一步,登陆mysql workbench 第二步,打开自己的数据 ,此处默认(root) 打开数据库后页面 : 第三步,新建一个schema ,随便给个名字,这里起名为test : 可以看到test 内的table ,views,routines,等选项都是没有任何内容的。 第四步 ,在file 下打开你需要导入的.sql 文件。...

将Kafka收到的数据传入到redis中

首先得配置GateWay中的config.properties 然后再看一下TBox中的properties main方法中  Test中的config.properties  Test中 先启动网管(GateWay),再启动终端(TBox),然后再运行KafkaTest 启动TBox   在启动Test 可以看到 接收到数据了  然...

使用gradle 整合mybatis逆向工程 mybatis-generator

废话不多说, 总共需要配置修改两个文件 项目地址 :  链接:https://pan.baidu.com/s/1UCKpOtpwH18NNvuLSL5cXQ提取码:usoq  build.gradle文件: buildscript { ext { springBootVersion = '2.1.1.RELEASE' } re...

Docker制作tomcat镜像之Dockerfile

基于CentOS7、Tomcat1.7、JDK1.8制作Tomcat镜像 1.拉取centos7镜像 docker pull centos:centos7 2.将jdk1.8和tomcat1.7解压到 /data1/software目录下     3.编写Dockerfile,命名为dockerfile-tomcat,内容如下: FROM centos:...

Mybatis

JDBCJDBC相关概念 JAVA程序都是通过JDBC连接数据库的,通过SQL对数据库编程,JDBC是由SUN公司提出的一些列规范,只定义了接口规范,具体实现由各个数据库厂商去实现,它是一种典型的桥接模式。 桥接模式是一种结构型设计模式,它的主要特点是把抽象与行为实现分离开来,分别定义接口,可以保持各部分的独立性以及应对他们的功能扩展。 JDBC规范...

sql2005分页

ALTER Procedure [dbo].[Pager2005] @TableName varchar(1000), --表名 @Fields varchar(5000) = '*', --字段名(全部字段为*) @OrderField varchar(5000), --排序字段(必须!支持多字段) @sqlWhere varchar(8000) =...