SpringBoot学习-如何使⽤mybatis-generator-maven-plug。。
前提:已经创建了Spring Boot⼯程。
接下来讲解⼀下如何⾃动⽣成MySql数据库表的java代码。springboot aop
1⾸先打开项⽬的l,添加依赖。如下图所⽰:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId&le</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--Swagger组件-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>
<!--Eureka组件-->
<dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>  <artifactId>spring-cloud-starter-eureka</artifactId>  <version>1.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>  <artifactId>spring-cloud-starter-config</artifactId>  <version>1.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--ali数据库组件-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.11</version>
</dependency>
<!--MyBatis组件-->
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>  <version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>  <artifactId>spring-cloud-starter-feign</artifactId>  <version>1.1.6.RELEASE</version>
</dependency>
<!--MySQL连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>
<dependency>
<groupId&acle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>  </dependency>
</dependencies>
<build>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId&ator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
</project>
注:如果仅仅想通过MyBatis连接MySql数据库,⾃动⽣成java代码,有写依赖是不需要,我这⾥把项
⽬中⽤到的依赖都贴上来了(我没有验证,可能需mybatis-generator-maven-plugin,mybatis-spring-boot-starter,mysql-connector-java是必须的吧)。
等所有插件更新完成,Plugins会出现mybatis-generator插件,如下图:
2配置mysql数据源,在resource⽂件夹下新建配置⽂件generatorConfig.properties,内容如下:
jdbc.driverLocation=C:/Users/user/.m2/repository/mysql/mysql-connector-java/5.1.39/mysql-connector-java-5.1.39.jar
jdbc.sql.jdbc.Driver
jdbc.password=Yko6RBfA
说明:jdbc.driverLocation指向mysql-connector-java-5.1.39.jar所在⽬录。
jdbc.driverClass固定写sql.jdbc.Driver
jdbc.userId=⽤户名
jdbc.password=密码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-////DTD MyBatis Generator Configuration 1.0//EN"
"/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<properties resource="generatorConfig.properties"></properties>
<classPathEntry location="${jdbc.driverLocation}"></classPathEntry>
<context id="default" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"></property>
<property name="suppressAllComments" value="true"></property>
</commentGenerator>
<jdbcConnection
driverClass="${jdbc.driverClass}"
connectionURL="${tionURL}"
userId="${jdbc.userId}"
password="${jdbc.password}">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"></property>
</javaTypeResolver>
<javaModelGenerator targetPackage="del" targetProject="src/main/java">
<property name="enableSubPackages" value="false"></property>
<property name="constructorBased" value="true"></property>
<property name="trimStrings" value="true"></property>
<property name="immutable" value="false"></property>
</javaModelGenerator>
<sqlMapGenerator targetPackage="ample.demo.mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="false"></property>
</sqlMapGenerator>
<javaClientGenerator targetPackage="ample.demo.dao" targetProject="src/main/java" type="ANNOTATEDMAPPER">
<property name="enableSubPackages" value="true"></property>
</javaClientGenerator>
<table tableName="test_info">
<generatedKey column="id" sqlStatement="MySql" identity="true"></generatedKey>
</table>
</context>
</generatorConfiguration>
说明:这个⽂件引⽤了generatorConfig.properties中定义的变量。<javaModelGenerator targetPackage="del" targetProject="src/main/java"> ,其中,targetPackage是⽣成的实体类所在包⽬录,targetPackage是⽣成的源码所在⽬录。
<table tableName="test_info">tableName是数据库中表名称。
3最后在Maven Projects中选中mybatis-generator:generate,点击运⾏,运⾏完成,在相应⽬录下⾃动⽣成实体类、Mapper类和Provider类。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。