IDEA创建一个Springboot项目
File—New—Project,选择Spring Initializr:
点击Next,依次输入包名项目名
点击Next,勾选需要的Pom依赖
点击Next,输入项目名及项目存储位置,点击Finish
二、导入依赖以及配置mybatis-generator插件
打开Pom文件,导入需要的依赖,这里使用Mysql8.0.15+Druid连接池
<?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 https://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.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.miaoshaproject</groupId>
<artifactId>miaosha</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>miaosha</name>
<description>miaoshaproject</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.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter druid 数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<!--将mapper文件打包进去-->
<resources>
<resource>
<!--指定根目录 到源文件夹 一般如下-->
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>mybatis generator</id>
<phase>package</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<!--允许移动生成的文件-->
<verbose>true</verbose>
<!--允许自动覆盖的文件-->
<overwrite>true</overwrite>
<configurationFile>
src/main/resources/mybatis-generator.xml
</configurationFile>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
</plugin>
</plugins>
</build>
</project>
三、配置mybatis-generator.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="testTables" targetRuntime="MyBatis3">
<!--去掉注释 -->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/miaosha?serverTimezone=Asia/Shanghai"
userId="root"
password="root">
</jdbcConnection>
<!-- 指定生成的位置
targetPackage:生成的类要放的包,真实的包受enableSubPackages属性控制;
targetProject:目标项目,指定一个存在的目录下,生成的内容会放到指定目录中,如果目录不存在,则不会自动建目录
-->
<javaModelGenerator targetPackage="com.miaoshaproject.dataobject" targetProject="src/main/java">
<!-- 在targetPackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false;如果多个数据库改为true分目录 -->
<property name="enableSubPackages" value="true"/>
<!-- 设置是否在getter方法中,对String类型字段调用trim()方法 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 指定mapper映射文件生成的位置
targetPackage、targetProject同javaModelGenerator中作用一样-->
<sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 指定mapper接口生成的位置(dao)
targetPackage、targetProject同javaModelGenerator中作用一样
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.miaoshaproject.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 指定数据库表
domainObjectName:生成的domain类的名字,当表名和domain类的名字有差异时一定要设置,如果不设置,直接使用表名作为domain类的名字;
可以设置为packageName.domainName,那么会自动把domainName类再放到packageName包里面;
后面几个参数为对数据的复杂操作,一般为手写,设置为false;
-->
<table tableName="user_info" domainObjectName="UserDO"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="user_password" domainObjectName="UserPasswordDO"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
四、配置application.yml
项目创建完成之后会有一个application.properties文件,shift+F6修改后缀名为yml即可。
server:
port: 8190
mybatis:
mapper-locations: classpath:mapper/*.xml
spring:
datasource:
name: miaosha
url: jdbc:mysql://localhost:3306/miaosha?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: root
配置端口号,映射文件位置以及数据库连接
五、运行mybatis-generator
在IDEA右侧点击Maven Projects,找到Plugins里的mybatis-generator,运行mybatis-generator:generate命令
六、最终的项目结构
七、测试
package com.miaoshaproject;
import com.miaoshaproject.dao.UserDOMapper;
import com.miaoshaproject.dataobject.UserDO;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication(scanBasePackages={"com.miaoshaproject"})
@RestController
@MapperScan(value = {"com.miaoshaproject.dao"})
public class MiaoshaApplication {
@Autowired
private UserDOMapper userDOMapper;
@RequestMapping("/test")
public String test(){
UserDO userDO = userDOMapper.selectByPrimaryKey(1);
return userDO.getName();
}
public static void main(String[] args) {
SpringApplication.run(MiaoshaApplication.class, args);
}
}
八、整合过程中可能遇到的问题
1、yml格式与properties格式不同,注意层级关系。
2、Springboot的Application启动类从自身所在包自上而下扫描组件,所以要将启动类放在其他包的外层。
3、rescources文件要标注资源包,否则里面的mapper文件可能扫描不到。
4、注意mysql的版本号在依赖里和插件里的一致性。
5、注意 / 和 \ ,复制文件路径的斜线和配置里的斜线是不一样的。
6、不要重复生成generator,会导致代码堆叠,一般删除后再次生成,并且要谨慎覆盖。
7、确认target文件目录下是否有生成的xml文件,如果没有的话要在pom文件里添加resources标签。
8、@Autowired注解在IDEA里报红线,如果运行正常的话可以降低其报错级别为黄色警告。
如果觉得有用的话点个赞呗~