天天看点

springboot项目中使用mybatis逆向工程生成代码实例

第一次写博客,权当是对自己学习的记录!!!

开发工具
         eclipse
 数据库mysql
           

1 先简单说一下eclipse中如何使用mybatis的逆向工程来快速生成代码

利用eclipse搭建springboot项目的前提是eclipse安装了Spring插件,具体如下:help–>eclipse marketplace 在也页面中输入Spring搜索后install当前插件并重启eclipse

2 在创建springboot项目的时候,选择

springboot项目中使用mybatis逆向工程生成代码实例
点击finish完成项目创建
           
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.kyrie</groupId>
	<artifactId>mybatis-generator</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>mybatis-generator</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</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>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<!-- mybatis-generator  插件 -->
			<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>
			
		</plugins>
	</build>

</project>

           

在项目的pom.xml文件中引入mybatis的generator插件

3 下面配置两个重要的配置文件,

① 第一个是springboot项目的全局变量:application.yml

在配置application.yml文件的时候需要注意冒号后面的空格

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver  //mysql数据库驱动
    url: jdbc:mysql://127.0.0.1:3306/newtest   //数据库名
    username: root
    password: root
server:
  port: 8081
#mybatis的配置
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.kyrie.matx.entity

           

② 第二个重要的配置文件是generator的配置文件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="application.yml" />
    <!-- mysql驱动的位置 这个是MySQL连接的jar包,你需要指定你自己计算机上的jar包的位置-->
    <classPathEntry location="C:/Users/tengxiao.ma/.m2/repository/mysql/mysql-connector-java/5.1.46/mysql-connector-java-5.1.46.jar" />
    <context id="Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- 是否生成注释代时间戳 -->
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!-- JDBC连接 其中connectionURL后面的newtest改为你创建的数据库,紧跟在后面是数据库连接的账户和密码-->
        <jdbcConnection
                driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/newtest"
                userId="root"
                password="root">
        </jdbcConnection>

        <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
         NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- 生成实体类地址 这里需要你改动,其中targetPackage需要根据你自己创建的目录进行改动 -->
        <javaModelGenerator targetPackage="com.kyrie.matx.entity" targetProject="src/main/java">
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </javaModelGenerator>

        <!-- 生成mapper xml文件 这里不需要改动 -->
        <sqlMapGenerator targetPackage="mapper"  targetProject="src/main/resources">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

        <!-- 生成mapper xml对应Client   这里需要改动targetPackage,依据你自己的工程-->
        <javaClientGenerator targetPackage="com.kyrie.matx.dao" targetProject="src/main/java" type="XMLMAPPER">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <!-- 配置表信息 -->
        <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample
            是否生成 example类   个人觉得生成那么多的example类很繁琐,因此设置为false-->

        <table schema="newtest" tableName="category"
               domainObjectName="Category" enableCountByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               enableUpdateByExample="false">
        </table>

        <table schema="newtest" tableName="product"
               domainObjectName="Product" enableCountByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               enableUpdateByExample="false">
        </table>
    </context>
</generatorConfiguration>

           

4 运行generator,完成代码的自动生成

右击项目名:Run as–>maven build,在Goals:mybatis-generator:generate 如下

springboot项目中使用mybatis逆向工程生成代码实例

apply–>run

5

springboot项目中使用mybatis逆向工程生成代码实例

运行结果如下表示代码生成成功,但是在当前的页面是看不到的,需要删除项目(不要删除工作空间的项目),之后import这个maven项目

6 完成这一系列工作之后,项目的具体结构如下:

springboot项目中使用mybatis逆向工程生成代码实例

继续阅读