赞
踩
Mybatis Generator是一个mybatis工具项目,用于生成mybatis的model,mapper,dao持久层代码。MybatisGenerator提供了maven plugin,ant target,java三种方式启动。现在主流的构建工具是gradle,虽然mybatisgenerator没有提供gradle的插件,但gradle可以调用ant任务,因此,gradle也能启动MybatisGenerator。
环境说明
· 数据库:mysql
· 数据库配置文件:src/main/resources/mybatis/db-mysql.properties
· 项目中使用了mybatistk.mybatis:mapper:3.3.2插件
一、数据库准备
- /*
- Navicat MySQL Data Transfer
- Source Server : aaaa
- Source Server Version : 50045
- Source Host : localhost:3306
- Source Database : studentdb
- Target Server Type : MYSQL
- Target Server Version : 50045
- File Encoding : 65001
- Date: 2015-09-28 16:57:18
- */
-
- SET FOREIGN_KEY_CHECKS=0;
-
- -- ----------------------------
- -- Table structure for `admin`
- -- ----------------------------
- DROP TABLE IF EXISTS `admin`;
- CREATE TABLE `admin` (
- `Id` int(11) NOT NULL auto_increment,
- `username` varchar(20) default NULL,
- `password` varchar(20) default NULL,
- `name` varchar(20) default NULL,
- PRIMARY KEY (`Id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
-
- -- ----------------------------
- -- Records of admin
- -- ----------------------------
- INSERT INTO `admin` VALUES ('1', 'admin', 'admin', '超级管理员');
-
- -- ----------------------------
- -- Table structure for `course`
- -- ----------------------------
- DROP TABLE IF EXISTS `course`;
- CREATE TABLE `course` (
- `Id` int(11) NOT NULL,
- `name` varchar(20) default NULL,
- `teacher_id` int(11) default NULL,
- PRIMARY KEY (`Id`),
- KEY `teacher_course` (`teacher_id`),
- CONSTRAINT `teacher_course` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`Id`) ON DELETE CASCADE ON UPDATE CASCADE
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-
- -- ----------------------------
- -- Records of course
- -- ----------------------------
- INSERT INTO `course` VALUES ('1', '面向对象程序设计', '201');
- INSERT INTO `course` VALUES ('2', '软件项目管理', '202');
- INSERT INTO `course` VALUES ('3', '基于ssh框架项目开发', '203');
-
- -- ----------------------------
- -- Table structure for `score`
- -- ----------------------------
- DROP TABLE IF EXISTS `score`;
- CREATE TABLE `score` (
- `Id` int(11) NOT NULL auto_increment,
- `student_id` int(11) default NULL,
- `course_id` int(11) default NULL,
- `score` double(6,1) default NULL,
- PRIMARY KEY (`Id`),
- KEY `stu_score` (`student_id`),
- KEY `course_score` (`course_id`),
- CONSTRAINT `course_score` FOREIGN KEY (`course_id`) REFERENCES `course` (`Id`) ON DELETE CASCADE ON UPDATE CASCADE,
- CONSTRAINT `stu_score` FOREIGN KEY (`student_id`) REFERENCES `student` (`Id`) ON DELETE CASCADE ON UPDATE CASCADE
- ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
-
- -- ----------------------------
- -- Records of score
- -- ----------------------------
- INSERT INTO `score` VALUES ('1', '101', '1', '100.0');
- INSERT INTO `score` VALUES ('2', '102', '1', '99.0');
- INSERT INTO `score` VALUES ('4', '101', '3', '222.0');
-
- -- ----------------------------
- -- Table structure for `student`
- -- ----------------------------
- DROP TABLE IF EXISTS `student`;
- CREATE TABLE `student` (
- `Id` int(11) NOT NULL,
- `name` varchar(20) default NULL,
- `password` varchar(20) default NULL,
- `sex` int(20) default NULL,
- `clazz` varchar(20) default NULL,
- `birthday` varchar(20) default NULL,
- PRIMARY KEY (`Id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-
- -- ----------------------------
- -- Records of student
- -- ----------------------------
- INSERT INTO `student` VALUES ('101', '林大雷', '1111', '1', '13软件2', '1996-03-24');
- INSERT INTO `student` VALUES ('102', '萧炎亮', '0000', '0', '13软件1', '1995-08-01');
- INSERT INTO `student` VALUES ('103', '叶凡凯', '0000', '1', '13软件1', '1994-01-23');
- INSERT INTO `student` VALUES ('104', '李牧尘', '1111', '0', '13软件1', '1997-12-05');
- INSERT INTO `student` VALUES ('105', '刘红枫', '0000', '1', '13软件2', '1995-11-15');
-
- -- ----------------------------
- -- Table structure for `teacher`
- -- ----------------------------
- DROP TABLE IF EXISTS `teacher`;
- CREATE TABLE `teacher` (
- `Id` int(11) NOT NULL,
- `name` varchar(20) default NULL,
- `password` varchar(20) default NULL,
- `sex` int(11) default NULL,
- `birthday` varchar(20) default NULL,
- `course_id` int(11) default NULL,
- `professional` varchar(20) default NULL,
- PRIMARY KEY (`Id`),
- KEY `course_teacher` (`course_id`),
- CONSTRAINT `course_teacher` FOREIGN KEY (`course_id`) REFERENCES `course` (`Id`) ON DELETE CASCADE ON UPDATE CASCADE
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-
- -- ----------------------------
- -- Records of teacher
- -- ----------------------------
- INSERT INTO `teacher` VALUES ('201', '李青山', '0000', '1', '1965-01-01', '1', '教授');
- INSERT INTO `teacher` VALUES ('202', '唐嫣然', '0000', '1', '1968-01-01', '2', '教授');
- INSERT INTO `teacher` VALUES ('203', '萧玄茂', '0000', '1', '1978-01-01', '3', '高级教师');
二、db-mysql.properties内容
- # JDBC 驱动类名
-
- # JDBC URL: jdbc:mysql:// + 数据库主机地址 + 端口号 + 数据库名
- jdbc.url=jdbc:mysql://localhost:3306/test
- # JDBC 用户名及密码
- jdbc.user=root
- jdbc.pass=123
- jdbc.driverClassName=com.mysql.jdbc.Driver
- # 生成实体类所在的包
- package.model=com.ssm.model
- # 生成 mapper 类所在的包
- package.mapper=com.ssm.dao
- # 生成 mapper xml 文件所在的包,默认存储在 resources 目录下
- package.xml=mapper
三、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>
- <context id="MysqlContext" targetRuntime="MyBatis3" defaultModelType="flat">
- <property name="beginningDelimiter" value="`"/>
- <property name="endingDelimiter" value="`"/>
-
- <commentGenerator>
- <property name="suppressDate" value="true"/>
- </commentGenerator>
- <jdbcConnection driverClass="${driverClass}"
- connectionURL="${connectionURL}"
- userId="${userId}"
- password="${password}">
- </jdbcConnection>
- <javaModelGenerator targetPackage="${modelPackage}" targetProject="${src_main_java}"/>
- <sqlMapGenerator targetPackage="${sqlMapperPackage}" targetProject="${src_main_resources}"/>
- <javaClientGenerator targetPackage="${mapperPackage}" targetProject="${src_main_java}" type="XMLMAPPER"/>
- <!-- sql占位符,表示所有的表 -->
-
- <table tableName="%" enableCountByExample="false" enableUpdateByExample="false"
- enableDeleteByExample="false" enableSelectByExample="false"
- selectByExampleQueryId="false">
-
- </table>
-
- </context>
- </generatorConfiguration>
四、build.gradle的配置
1.步骤说明
1、添加 configurations { mybatisGenerator } 2、添加依赖 mybatisGenerator 'org.mybatis.generator:mybatis-generator-core:1.3.2' mybatisGenerator 'mysql:mysql-connector-java:5.1.38' mybatisGenerator 'tk.mybatis:mapper:3.3.1' 3、添加任务 def getDbProperties = { def properties = new Properties() file("src/main/resources/mybatis/db-mysql.properties").withInputStream { inputStream -> properties.load(inputStream) } properties; } task mybatisGenerate << { def properties = getDbProperties() ant.properties['targetProject'] = projectDir.path ant.properties['driverClass'] = properties.getProperty("jdbc.driverClassName") ant.properties['connectionURL'] = properties.getProperty("jdbc.url") ant.properties['userId'] = properties.getProperty("jdbc.user") ant.properties['password'] = properties.getProperty("jdbc.pass") ant.properties['src_main_java'] = sourceSets.main.java.srcDirs[0].path ant.properties['src_main_resources'] = sourceSets.main.resources.srcDirs[0].path ant.properties['modelPackage'] = properties.getProperty("package.model") ant.properties['mapperPackage'] = properties.getProperty("package.mapper") ant.properties['sqlMapperPackage'] =properties.getProperty("package.xml") ant.taskdef( name: 'mbgenerator', classname: 'org.mybatis.generator.ant.GeneratorAntTask', classpath: configurations.mybatisGenerator.asPath ) ant.mbgenerator(overwrite: true, configfile: 'src/main/resources/mybatis/generatorConfig.xml', verbose: true) { propertyset { propertyref(name: 'targetProject') propertyref(name: 'userId') propertyref(name: 'driverClass') propertyref(name: 'connectionURL') propertyref(name: 'password') propertyref(name: 'src_main_java') propertyref(name: 'src_main_resources') propertyref(name: 'modelPackage') propertyref(name: 'mapperPackage') propertyref(name: 'sqlMapperPackage') } } }
2.完整build.gradle配置
group 'person.xjl' version '1.0-SNAPSHOT' apply plugin: 'java' apply plugin: 'war' sourceCompatibility = 1.8 ext { spring_version = "4.3.14.RELEASE" } repositories { maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } mavenCentral() } //1、添加 configurations { mybatisGenerator } dependencies { testCompile group: 'junit', name: 'junit', version: '4.11' // https://mvnrepository.com/artifact/org.springframework/spring-webmvc //springmvc + Spring Configuration compile "org.springframework:spring-web:$spring_version" compile "org.springframework:spring-webmvc:$spring_version" compile "org.springframework:spring-aop:$spring_version" compile "org.springframework:spring-aspects:$spring_version" compile "org.springframework:spring-beans:$spring_version" compile "org.springframework:spring-context:$spring_version" compile "org.springframework:spring-context-support:$spring_version" compile "org.springframework:spring-core:$spring_version" compile "org.springframework:spring-expression:$spring_version" compile "org.springframework:spring-jdbc:$spring_version" compile "org.springframework:spring-messaging:$spring_version" compile "org.springframework:spring-orm:$spring_version" compile "org.springframework:spring-tx:$spring_version" compile "org.springframework:spring-test:$spring_version" //MyBatis compile "org.mybatis:mybatis:3.4.1" //mybatis spring 插件 compile "org.mybatis:mybatis-spring:1.3.1"// https://mvnrepository.com/artifact/javax.servlet/jstl compile group: 'javax.servlet', name: 'jstl', version: '1.2' // https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0' // https://mvnrepository.com/artifact/javax/javaee-api compile group: 'javax', name: 'javaee-api', version: '7.0' // https://mvnrepository.com/artifact/mysql/mysql-connector-java compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.21' //公共资源包 compile "commons-logging:commons-logging:1.2" compile "commons-lang:commons-lang:2.6" compile "org.apache.commons:commons-collections4:4.0" compile "commons-beanutils:commons-beanutils:1.8.3" compile "commons-dbcp:commons-dbcp:1.4" compile "commons-pool:commons-pool:1.6" compile group: 'log4j', name: 'log4j', version: '1.2.17' //2、添加依赖 mybatisGenerator 'org.mybatis.generator:mybatis-generator-core:1.3.2' mybatisGenerator 'mysql:mysql-connector-java:5.1.21' mybatisGenerator 'tk.mybatis:mapper:3.3.1' } //3、添加任务 def getDbProperties = { def properties = new Properties() file("src/main/resources/mybatis/db-mysql.properties").withInputStream { inputStream -> properties.load(inputStream) } properties; } task mybatisGenerate << { def properties = getDbProperties() ant.properties['targetProject'] = projectDir.path ant.properties['driverClass'] = properties.getProperty("jdbc.driverClassName") ant.properties['connectionURL'] = properties.getProperty("jdbc.url") ant.properties['userId'] = properties.getProperty("jdbc.user") ant.properties['password'] = properties.getProperty("jdbc.pass") ant.properties['src_main_java'] = sourceSets.main.java.srcDirs[0].path ant.properties['src_main_resources'] = sourceSets.main.resources.srcDirs[0].path ant.properties['modelPackage'] = properties.getProperty("package.model") ant.properties['mapperPackage'] = properties.getProperty("package.mapper") ant.properties['sqlMapperPackage'] =properties.getProperty("package.xml") ant.taskdef( name: 'mbgenerator', classname: 'org.mybatis.generator.ant.GeneratorAntTask', classpath: configurations.mybatisGenerator.asPath ) ant.mbgenerator(overwrite: true, configfile: 'src/main/resources/mybatis/generatorConfig.xml', verbose: true) { propertyset { propertyref(name: 'targetProject') propertyref(name: 'userId') propertyref(name: 'driverClass') propertyref(name: 'connectionURL') propertyref(name: 'password') propertyref(name: 'src_main_java') propertyref(name: 'src_main_resources') propertyref(name: 'modelPackage') propertyref(name: 'mapperPackage') propertyref(name: 'sqlMapperPackage') } } }
五、运行
在gradle中刷新,点开module下的other,里找到mybatisGenarate,双击
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。