SpringBoot的初始Web搭建我就不多说了,在此基础上进行使用hikari(日文意为:“光”,读法 "西卡利") 连接池的使用及tk.Mybatis(国内大佬基于Mybatis框架封装的tk.Mybatis框架) 自动生成代码的实践踩坑记录如下:
说明:1.我的主文件入口地址为:package com.funtl.hello.spring.boot
2.指定生成的Mapper继承模板接口地址为:com.tk.mybatis.mapper.MyMapper
3.generatorConfig.xml 配置接口地址为:main esourcesgeneratorgeneratorConfig.xml
4.application.yml地址为:main esourcesapplication.yml
5.jdbc.properties 连接数据库配置地址为:main esourcesjdbc.properties
整体目录结构如下:
一、pom的配置如下:
SpringBoot搭建Web项目确实很爽,但是也存在一些很尴尬的问题,比如版本更新太快,很多插件都对应不上版本,导致在初学情况下很多问题根本解决不了,所以建议初学者看教程一定要看最新的内容,要不然就会遇到很多想破脑壳也解决不了的问题。
<?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.3.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.funtl</groupId> <artifactId>hello-spring-boot</artifactId> <version>1.0.0-SNAPSHOT</version> <name>hello-spring-boot</name> <description>Demo project for Spring Boot</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.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> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>${hikaricp.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <exclusions> <exclusion> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.1.5</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <configuration> <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>4.1.4</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
二、application.yml配置如下:
其中包括一些最基础配置及hikari、mybatis的配置(红色的地方结合自身实际配置,后面内容相同)。
spring: application: name: hello-spring-boot0909 datasource: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/testjavadb?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: Tanhao0508 hikari: minimum-idle: 5 idle-timeout: 600000 maximum-pool-size: 10 auto-commit: true pool-name: MyHikariCP max-lifetime: 1800000 connection-timeout: 30000 connection-test-query: SELECT 1 mybatis: type-aliases-package: com.funtl.hello.spring.boot.domain mapper-locations: classpath:mapper/*.xml
三、编辑生成的Mapper继承模板接口
该接口不能被扫描到,否则会因为与原文件名冲突而导致报错,我的路径为:com.tk.mybatis.mapper.MyMapper
package com.tk.mybatis.mapper; import tk.mybatis.mapper.annotation.RegisterMapper; import tk.mybatis.mapper.common.BaseMapper; import tk.mybatis.mapper.common.ExampleMapper; import tk.mybatis.mapper.common.Marker; import tk.mybatis.mapper.common.RowBoundsMapper; /** * create by: tanhao * description: 自己的Mapper * 特别注意,该接口不能被扫描到,否则会报错 * create time: 2020/9/12 17:13 * @Param: null * @return */ @RegisterMapper public interface MyMapper<T> extends BaseMapper<T>, ExampleMapper<T>, RowBoundsMapper<T>, Marker { }
四、MyBatis 自动生成的配置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> <!-- 引入 application.properties --> <properties resource="jdbc.properties" /> <!-- MyBatis3Simple:不生成 Example相关类及方法 defaultModelType="flat" --> <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <!-- 指定生成 Mapper 的继承模板 --> <plugin type="tk.mybatis.mapper.generator.MapperPlugin"> <property name="mappers" value="com.tk.mybatis.mapper.MyMapper"/> </plugin> <!--注意context内的文件要按序放--> <commentGenerator> <property name="suppressDate" value="true"/> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- jdbc 连接配置 --> <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.connectionURL}" userId="${jdbc.username}" password="${jdbc.password}"> </jdbcConnection> <javaTypeResolver> <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) --> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成实体类的包名和位置 ,targetPackage指的是包名,targetProject值得是路径位置--> <!-- 对于生成的pojo所在包,pojo其实就是domain Entity--> <javaModelGenerator targetPackage="com.funtl.hello.spring.boot.domain" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 对于生成的mapper.xml所在目录 --> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/> <!-- 配置mapper对应的java映射 也可以叫dao层 --> <javaClientGenerator targetPackage="com.funtl.hello.spring.boot.mapper" targetProject="src/main/java" type="XMLMAPPER"/> <!-- table可以有多个,每个数据库中的表都可以写一个table,tableName表示要匹配的数据库表,也可以在tableName属性中通过使用%通配符来匹配所有数据库表,只有匹配的表才会自动生成文件,*代表全部表--> <table tableName="user"> <property name = "ignoreQualifiersAtRuntime" value="true" /> <generatedKey column="id" sqlStatement="Mysql" identity="true"/> </table> </context> </generatorConfiguration>
该配置会生成对应的Mapper、dto、dao文件, 都需要根据自身实际情况修改。
五、jdbc.properties参数配置
jdbc.driverClass=com.mysql.cj.jdbc.Driver jdbc.connectionURL=jdbc:mysql://localhost:3306/testjavadb?serverTimezone=UTC&generateSimpleParameterMetadata=true&useUnicode=true&characterEncoding=utf8&useSSL=false&nullCatalogMeansCurrent=true jdbc.username: root jdbc.password: Tanhao0508
其中需要注意的是:
nullCatalogMeansCurrent=true 如果没配置这个参数可能会让我们自动生成的内容与数据库实际字段不对应
serverTimezone=UTC 设置时差
六、刷新下Maven仓库的插件依赖,然后双击
mybatis-generator下的mybatis-generator:generate,就可以生成对应的mapper,dto,dao 代码文件
七、使用测试文件测试接口能否使用:
需要注意的是因为主文件入口会扫描不到mapper包,所以需要在主文件入口加一下scan参数:
@MapperScan(basePackages = "com.funtl.hello.spring.boot.mapper")
HelloSpringBootApplicationTests如下:
package com.funtl.hello.spring.boot; import com.funtl.hello.spring.boot.domain.User; import com.funtl.hello.spring.boot.mapper.UserMapper; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; @SpringBootTest class HelloSpringBootApplicationTests { @Autowired private UserMapper userMapper; @Test public void contextLoads() { System.out.println("Hello Spring Boot Test"); } @Test public void TestSelectAll(){ List<User> users = userMapper.selectAll(); for (User user : users) { System.out.println(user); } } }
八:运行HelloSpringBootApplicationTests
打印结果:
歪维股德,查询成功! 西卡利是没问题的
mybatis