自動建立JavaBean與mapper
阿新 • • 發佈:2018-10-31
1.配置:
1.1 resource (application.properties)
//標紅的記得改
## 資料來源配置 spring.datasource.url=jdbc:mysql://localhost:3306/20181020db?useUnicode=true&characterEncoding=utf8 spring.datasource.username= spring.datasource.password= spring.datasource.driver-class-name=com.mysql.jdbc.Driver ## Mybatis 配置 mybatis.typeAliasesPackage=com.czxy.domain mybatis.mapperLocations=classpath:com/czxy/dao/*.xml ## Mapper 配置 mapper.plugin=tk.mybatis.mapper.generator.MapperPlugin mapper.Mapper=tk.mybatis.mapper.common.Mapper mapper.mappers=com.czxy.utils.MapperUtils mapper.not-empty=false mapper.identity=MYSQL ## PageHelper pagehelper.helperDialect=mysql pagehelper.reasonable=true pagehelper.supportMethodsArguments=true pagehelper.params=count=countSql spring.activemq.broker-url=tcp://localhost:61616 spring.activemq.in-memory=false spring.activemq.pool.enabled=false spring.activemq.user=admin spring.activemq.password=admin
1.2 resource (config.properties)
# 資料庫配置 mysql.driver=com.mysql.jdbc.Driver mysql.url=jdbc:mysql://localhost:3306/20181020db?useUnicode=true&characterEncoding=UTF-8 mysql.username= mysql.password= # 通用Mapper配置 mapper.plugin=tk.mybatis.mapper.generator.MapperPlugin mapper.Mapper=com.czxy.utils.MapperUtils # Java介面和實體類 targetJavaProject=src/main/java targetModelPackage=com.czxy.domain # Mapper 生成路徑 targetMapperProject=src/main/java targetMapperPackage=com.czxy.dao # Mapper XML 生成路徑 targetResourcesProject=src/main/resources targetXMLPackage=com.czxy.dao # 指定要生成的表名 %為所有表 單表,請指定表名 targetTableName=t_cen
1.3 resource (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>
< properties resource="config.properties"/>
<context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<plugin type="${mapper.plugin}">
<property name="mappers" value="${mapper.Mapper}"/>
</plugin>
<jdbcConnection driverClass="${mysql.driver}"
connectionURL="${mysql.url}"
userId="${mysql.username}"
password="${mysql.password}">
</jdbcConnection>
<javaModelGenerator targetPackage="${targetModelPackage}" targetProject="${targetJavaProject}"/>
<sqlMapGenerator targetPackage="${targetXMLPackage}" targetProject="${targetResourcesProject}"/>
<javaClientGenerator targetPackage="${targetMapperPackage}" targetProject="${targetMapperProject}" type="XMLMAPPER" />
<table tableName="${targetTableName}" >
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
</table>
</context>
</generatorConfiguration>
2.1 pom.xml (本專案所依賴的jar包,我就直接全貼上過來了)
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<!-- 模板引擎 Thymeleaf 依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 通用mapper -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.0.3</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
3.MapperUtils.java(工具類)
package com.czxy.utils;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
/**
*@author Fang
*@create 2018/10/20 17:52
*@desc 自動生成 mapper 與 domain utils 類
**/
public interface MapperUtils<T> extends Mapper<T>,MySqlMapper<T> {
}