用Maven插件生成Mybatis代码/数据库
现在代码管理基本上是采用Maven管理,Maven的好处此处不多说,大家用百度搜索会有很多介绍,本文介绍一下用Maven工具如何生成Mybatis的代码及映射的文件。
一、配置Maven pom.xml 文件
在pom.xml增加以下插件:
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin>
配置好Maven插件,下面需要配置插件需要配置文件
二、插件配置文件
Maven的项目配置文件存放路径如下图:

插件默认会读到src/main/resources目录下的generatorConfig.xml 文件。
具体配置如下范例:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" > <generatorConfiguration> <classPathEntry location="D:\repo\mysql\mysql-connector-java\5.1.6\mysql-connector-java-5.1.6.jar" /> <context id="context1" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://xxxxxxx:8406/CL_DEMO?useUnicode=true&characterEncoding=UTF-8" userId="root" password="password" /> <javaModelGenerator targetPackage="xxx.account.model" targetProject="D:\workspace\项目名\src\main\java" /> <sqlMapGenerator targetPackage="xxxx.account.persistence" targetProject="D:\workspace\项目名\包名\src\main\resources" /> <javaClientGenerator targetPackage="xxxx.account.persistence targetProject="D:\\workspace\项目名\src\main\java" type="XMLMAPPER" /> <table schema="CL_DEMO" tableName="tb_user" /> <table schema="CL_DEMO" tableName="tb_role" /> <table schema="CL_DEMO" tableName="tb_permission" /> <table schema="CL_DEMO" tableName="tb_role_user" /> <table schema="CL_DEMO" tableName="tb_permission_role" /> </context> </generatorConfiguration>
在本例中,使用的是MySQL数据库,需要指定MySQL数据库的JDBC驱动。
1、指定连接数据库的URL;
2、指定生成数据模型对象的包名,如com.xxx.xxx.model, targetProject 指定是项目及存放model的目录。
3、sqlMapGenerator 需要设置包名,和存放映射文件的路径。如果用maven 管理,一般xml文件放在src/main/resources目录下。
4、javaClientGenerator 需要设置包名及路径。
6、接下来需要配置你需要生成的表名。
三、生成代码
配置搞掂后,现在就可以生成代码了,

如果是在eclipse 中,选择pom.xml文件,击右键先择Run AS——>Maven Build… ——>在Goals框中输入:mybatis-generator:generate
如果在命令行输入Maven命令即可,注意:一定是当前项目目录下运行该命令:
mvn mybatis-generator:generate
代码生成完,大功告别成。
用Maven插件生成Mybatis代码/数据库的更多相关文章
- 用Maven插件生成Mybatis代码
现在代码管理基本上是采用Maven管理,Maven的好处此处不多说,大家用百度搜索会有很多介绍,本文介绍一下用Maven工具如何生成Mybatis的代码及映射的文件. 一.配置Maven pom.xm ...
- 使用mybatis-generator工具自动生成mybatis代码
使用mybatis-generator工具自动生成mybatis代码 步骤如下: 1.引入maven 依赖,在项目pom.xml文件中添加 <plugin> <groupId> ...
- rpc框架: thrift/avro/protobuf 之maven插件生成java类
thrift.avro.probobuf 这几个rpc框架的基本思想都差不多,先定义IDL文件,然后由各自的编译器(或maven插件)生成目标语言的源代码,但是,根据idl生成源代码这件事,如果每次都 ...
- 【mybatis源码学习】利用maven插件自动生成mybatis代码
[一]在要生成代码的项目模块的pom.xml文件中添加maven插件 <!--mybatis代码生成器--> <plugin> <groupId>org.mybat ...
- 使用maven插件生成grpc所需要的Java代码
1.首先需要编写自己需要的.proto文件,本文重点不在这里,.proto可以参考grpc官方例子 https://grpc.io/docs/quickstart/java.html 2.创建自己的J ...
- 【转】Intellij IDEA 14中使用MyBatis-generator 自动生成MyBatis代码
Intellij IDEA 14 作为Java IDE 神器,接触后发现,非常好用,对它爱不释手,打算离开eclipse和myeclipse,投入Intellij IDEA的怀抱. 然而在使用的过程中 ...
- IDEA 中使用MyBatis-generator 自动生成MyBatis代码
0.在Intellij IDEA创建maven项目 1. 在maven项目的pom.xml 添加mybatis-generator-maven-plugin 插件 <build> < ...
- JAVA入门[7]-Mybatis generator(MBG)自动生成mybatis代码
一.新建测试项目 新建Maven项目MybatisDemo2,修改pom.xml引入依赖.dependencies在上节基础上新增 <dependency> <groupId> ...
- 在maven项目中使用mybatis-generator-maven-plugin生成mybatis代码
项目整体的目录结构如下: pom.xml如下: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...
随机推荐
- nth-child() 选择器
给tabel tb1 td添加样式 #tb1 tr td:nth-child(2n+1) {/*奇数行样式*/ } #tb1 tr td:nth-child(2n) {/*偶数行样式*/ }
- [USACO2004][poj2373]Dividing the Path(DP+单调队列)
http://poj.org/problem?id=2373 题意:一条直线分割成N(<=25000)块田,有一群奶牛会在其固定区域吃草,每1把雨伞可以遮住向左右延伸各A到B的区域,一只奶牛吃草 ...
- HTML5——将图片拖拽上传
如下图所示: 代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- Java基础-final和static的区别
很多时候会容易把static和final关键字混淆,static作用于成员变量用来表示只保存一份副本,而final的作用是用来保证变量不可变.看下面这个例子: public class Test { ...
- 关于OOM那些事儿
最近在aliyun上crontab里放的一个java脚本把机器搞翻了,ssh连不上T_T 发现OOM了,真是无语.并不懂Java的内存模型,转一篇备用吧. 转载自:http://www.cnblogs ...
- Angulajs系列-01-入门
1.解决什么问题? a, controller的各种的创建 b,体验angular的双向绑定 2.怎么解决 2.1 引入angularjs 下载地址 2.2 创建controller的方法 2.2.1 ...
- POJ2411 Mondriaan's Dream
Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, af ...
- .net String.Format数字格式化输出
内容转载自:http://www.cnblogs.com/lqb/archive/2008/08/04/1259498.html 前面内容这个做的总结的很全,今后有新增的我继续往后补充.请留意我增加的 ...
- TCP/IP详解 笔记一
概述: Tcp-ip让网络上的计算机进行通信,而不管计算机和操作系统是否一样. 分层结构: Tcp/ip协议族是多层协议的组合,而tcp和ip只是其中的两个协议而已. 一个通信举例: 注意图的右上方: ...
- javascript自动转换大小写
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD ...