一、初始化项目
自行决定是否需要是个web项目
application.yml配置端口号
server:
port: 8089
二、集成Druid数据源
常见问题:
https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
-
依赖:
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
-
在application.yml配置文件中添加以下Druid相关的配置
spring: datasource: druid: url: jdbc:mysql://127.0.0.1:3306/user?serverTimezone=UTC&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&&&useSSL=true&tinyInt1isBit=false&allowMultiQueries=true username: root password: rootroot driver-class-name: com.mysql.cj.jdbc.Driver #连接池配置 initial-size: 5 max-active: 100 min-idle: 5 max-wait: 60000 pool-prepared-statements: true max-pool-prepared-statement-per-connection-size: 20 validation-query-timeout: 60000 validation-query: SELECT 1 FROM DUAL test-on-borrow: false test-on-return: false test-while-idle: true time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 100000 ###监控配置 begin### # WebStatFilter配置,说明请参考Druid Wiki,配置_配置WebStatFilter web-stat-filter: enabled: true url-pattern: /* exclusions: /druid/*,*.js,*.gif,*.jpg,*.png,*.css,*.ico # StatViewServlet配置,说明请参考Druid Wiki,配置_StatViewServlet配置 stat-view-servlet: enabled: true url-pattern: /druid/* reset-enable: false login-username: admin login-password: admin allow: 127.0.0.1 # 配置StatFilter filter: stat: db-type: mysql log-slow-sql: true slow-sql-millis: 5000 #不能删除,因此去掉 # # 配置WallFilter # wall: # enabled: true # db-type: mysql # config: # delete-allow: false # drop-table-allow: false
-
启动项目可以通过http://127.0.0.1:8089/druid访问Druid的web界面(端口可自行配置),账号密码是配置文件中配置的,这里配置的是admin/admin
三、 springboot集成tk.mybatis
- 依赖
<!-- pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>RELEASE</version>
</dependency>
- 增加自定义Mapper类的基类,继承tk.mybatis的Mapper接口,可在公共模块中添加以下接口
package com.example.config;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
public interface MysqlBaseMapper<T> extends Mapper<T>, MySqlMapper<T> {
}
- pom配置mybatis自动生成代码插件
<!--mybatis自动生成代码插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<dependencies>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>Generate MyBatis Artifacts</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>
- 在resource文件里增加mybatis自动生成代码配置文件mybatis-generator.xml(修改用户名密码,基础类value=“com.example.config.MysqlBaseMapper”,生成位置targetPackage=,表名tableName=“users”)
<?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"/>
<context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="com.example.config.MysqlBaseMapper"/>
</plugin>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/user"
userId="root"
password="rootroot">
</jdbcConnection>
<javaModelGenerator targetPackage="com.example.newauto1.model" targetProject="src/main/java"/>
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources/"/>
<javaClientGenerator targetPackage="com.example.newauto1.mapper" targetProject="src/main/java"
type="XMLMAPPER"/>
<table tableName="users"/>
</context>
</generatorConfiguration>
- 修改好自己的配置后,就可以点击maven里的以下插件,自动生成相应的mapper及model文件了
- 在启动类上增加@MapperScan({“com.example.newauto1.mapper”})注解,其中引号里的是生成的Mapper接口的包路径
- application.yml中增加Mapper文件的位置的配置
mybatis:
mapper-locations: classpath:/mapper/*.xml