1、创建工程
1.1
1.2
1.3
1.4
1.5 创建好的项目:
开始第2步:添加其他依赖:
2.1 访问中央仓库中心:https://mvnrepository.com/
添加阿里连接池
2.2
2.3
添加到pom文件中:
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
2.4 添加Mybatis 的代码生成插件
2.5
2.6
添加到pom里,并修改:
<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.5</version> <configuration>
<configurationFile>
${basedir}/src/main/resources/mybatis-generatorConfig.xml
</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</build>
3.1修改appllication.properties文件为 application.yml文件:
然后添加以下配置内容:
spring:
datasource:
url: jdbc:mysql://localhost:3306/springbootmybatis?useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
#注意这里的mybatis要靠左边,不然到时就会误以为spring的属性
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.demo.model
3.2 创建数据库,及其对应的表:
CREATE DATABASE springboot_mybatis;
CREATE TABLE t_user(
user_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
user_name VARCHAR(255) NOT NULL ,
password VARCHAR(255) NOT NULL ,
phone VARCHAR(255) NOT NULL )
ENGINE=INNODB AUTO_INCREMENT=1000
DEFAULT CHARSET=utf8;
3.3 创建Mybatis的逆向工程 mybatis-generatorConfig.xml配置文件,(注意与上面2.5中的pom路径及名称对应)内容如下:
添加以下配置内容:注意下载 <!-- 下载Mysql驱动,然后配置路径-->
mysql驱动下载地址(或者百度一下):https://dev.mysql.com/downloads/connector/j/
<?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>
<!-- 下载Mysql驱动,然后配置路径-->
<classPathEntry location="F:\jar\mysql-connector-java-5.1.47\mysql-connector-java-5.1.47.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:330/springboot_mybatis"
userId="root"
password="123456">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="com.example.demo.model" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<table tableName="t_user"
domainObjectName="User"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
4.配置运行命令:
4.1
4.2
4.3
5.1 运行刚才创建好的命令:
6.1 项目里多了model和dao以及对应的xml映射文件
总结:
1. mybatis-generator-maven-plugin插件在pom中的配置要与项目中的resource下的Mybatis逆向生成配置文件路径及名称一致
2.在Mybais逆向配置文件中个,记得下载Mysql的驱动包
mysql驱动下载(或者百度一下)地址:https://dev.mysql.com/downloads/connector/j/
参考博客:
https://blog.csdn.net/winter_chen001/article/details/77249029