1、加入起步依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
2、配置数据库连接信息
mybatis.mapper-locations=classpath:com/example/springboot/mapper/*.xml
#配置数据库连接信息
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis?serverTimezone=GMT&characterEncoding=utf8
3、在项目目录下面放置GeneratorMapper.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>
<!--指定连接数据库的JDBC驱动包位置-->
<classPathEntry location="D:/MySQL/mysql-connector-java-5.1.44-bin.jar"/>
<!--配置table表格信息内容体,targetRuntime指定采用Mybatis3版本-->
<context id="tables" targetRuntime="Mybatis3">
<!--抑制生成注释,由于生成注释是英文,可以不让生成-->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--配置数据库连接信息-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/mybatis?serverTimezone=GMT"
userId="root"
password="123">
</jdbcConnection>
<!--生成model类,targetPackage指定model类包名,targetProject指定生成model放在哪个工程下-->
<javaModelGenerator targetPackage="com.example.springboot.model" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
<property name="trimString" value="false"/>
</javaModelGenerator>
<!--生成mybatis的Mapper.xml文件,targetPackage指定Mapper.xml包名,targetProject指定生成Mapper.xml放在哪个工程下-->
<sqlMapGenerator targetPackage="com.example.springboot.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!--生成mybatis的Mapper接口类文件,targetPackage指定Mapper接口类的包名,targetProject指定生成Mapper接口放在哪个工程下-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.springboot.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!--数据库表名及对应java模型类名-->
<table tableName="student"
domainObjectName="Student"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>
4、在pom.xml添加mybatis代码自动生成插件依赖
<!--mybatis代码自动生成插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>GeneratorMapper.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
5、在Maven项目里plugins的mybatis-generator,双击mybatis-generator:generate,会自动生成model和mapper(StudentMapper.java、StudentMapper.xml)
6、需要将StudentMapper.xml与StudentMapper.java编译在同一地方,所以需要在pom.xml添加:
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
网友评论