創建一個 Springboot 項目
使用 MyBatis Generator 逆向生成代碼
1.配置 Maven pom.xml 文件
在 pom.xml 增加以下插件:
<build>
<plugins>
<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>
配置好 Maven 插件,下面需要配置插件需要配置文件
2.配置插件的要配置文件
在 maven 項目下的 src/main/resources
目錄下建立名爲 Maven 的項目配置文件存放路徑如下圖:generatorConfig.xml
和 generator.properties
配置文件,
Maven
的項目配置文件存放路徑如下圖 (圖片中的 generator.properties 寫成了 generatorCongif.properties,請修改回 generator.properties) :
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="generator.properties"></properties>
<!--指定特定數據庫的jdbc驅動jar包的位置-->
<classPathEntry location="${jdbc.driverLocation}"/>
<context id="default" targetRuntime="MyBatis3">
<!-- optional,旨在創建class時,對註釋進行控制 -->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--jdbc的數據庫連接 -->
<jdbcConnection
driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.connectionURL}"
userId="${jdbc.userId}"
password="${jdbc.password}">
</jdbcConnection>
<!-- 非必需,類型處理器,在數據庫類型和java類型之間的轉換控制-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- Model模型生成器,用來生成含有主鍵key的類,記錄類 以及查詢Example類
targetPackage 指定生成的model生成所在的包名
targetProject 指定在該項目下所在的路徑
-->
<javaModelGenerator targetPackage="com.donlex.mybatiswithspringboot.model"
targetProject="src/main/java">
<!-- 是否允許子包,即targetPackage.schemaName.tableName -->
<property name="enableSubPackages" value="false"/>
<!-- 是否對model添加 構造函數 -->
<property name="constructorBased" value="true"/>
<!-- 是否對類CHAR類型的列的數據進行trim操作 -->
<property name="trimStrings" value="true"/>
<!-- 建立的Model對象是否 不可改變 即生成的Model對象不會有 setter方法,只有構造方法 -->
<property name="immutable" value="false"/>
</javaModelGenerator>
<!--Mapper映射文件生成所在的目錄 爲每一個數據庫的表生成對應的SqlMap文件 -->
<sqlMapGenerator targetPackage="mapping"
targetProject="src/main/resources">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 客戶端代碼,生成易於使用的針對Model對象和XML配置文件 的代碼
type="ANNOTATEDMAPPER",生成Java Model 和基於註解的Mapper對象
type="MIXEDMAPPER",生成基於註解的Java Model 和相應的Mapper對象
type="XMLMAPPER",生成SQLMap XML文件和獨立的Mapper接口
-->
<javaClientGenerator targetPackage="com.donlex.mybatiswithspringboot.mapper"
targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--一個table對應數據庫中的一張表-->
<table tableName="info" domainObjectName="Info"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table tableName="order" domainObjectName="Order"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table tableName="product" domainObjectName="Product"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table tableName="receive" domainObjectName="Receive"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table tableName="shopping" domainObjectName="Shopping"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table tableName="user" domainObjectName="User"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
generator.propertites 代碼如下:
jdbc.driverLocation=D:\\Firefox\\download\\mysql-connector-java-5.1.44\\mysql-connector-java-5.1.44-bin.jar
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.connectionURL=jdbc:mysql:///shopstore
jdbc.userId=root
jdbc.password=123456
3.設置並運行插件
在 Intellij IDEA 中點擊 菜單 run 中 Edit Configurations,點擊 + 號,選擇 maven,會出現,在 Commond line 填上mybatis-generator:generate -e
,apply 和 ok。步驟參考下圖
最後點擊 generator運行,生成 model,mapper,mapping
逆向生成的目錄結構如下
添加service和controller
在與mapper同級的目錄下創建service
目錄,在裏面創建UserService
package com.donlex.mybatiswithspringboot.service;
import com.donlex.mybatiswithspringboot.mapper.UserMapper;
import com.donlex.mybatiswithspringboot.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service //注意添加這個註解
public class UserService {
@Autowired
private UserMapper userMapper;
public User getOneUser() {
User user = userMapper.selectByPrimaryKey(1);
return user;
}
}
在與mapper同級的目錄下創建controller
目錄,在裏面創建UserController
package com.donlex.mybatiswithspringboot.controller;
import com.donlex.mybatiswithspringboot.model.User;
import com.donlex.mybatiswithspringboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/query")
public String getAllUser() {
User user = userService.getOneUser();
return "Uid:"+user.getUid()+" Username:"+user.getUsername();
}
}
目錄結構如圖:
添加 application.yml 配置文件
刪除原有的 application.properties
文件,添加 application.yml
文件中的配置如下:
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/shopstore?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
## 該配置節點爲獨立的節點,如果將這個配置放在spring的節點下,導致配置無法被識別
mybatis:
mapper-locations: classpath:mapping/*.xml #注意:一定要對應mapper映射xml文件的所在路徑
設置掃描Mapper包
在啓動類中添加包掃描註解
package com.donlex.mybatiswithspringboot;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.donlex.mybatiswithspringboot.mapper")//注意添加這個註解,並且配置正確的包名
@SpringBootApplication
public class MybatiswithspringbootApplication {
public static void main(String[] args) {
SpringApplication.run(MybatiswithspringbootApplication.class, args);
}
}
啓動測試
啓動項目,訪問對應的 URL 即可。