SpringBoot整合thymeleaf+代码⽣成器(最全,最详细)
引⾔
vbs脚本停止先吐槽⼀下,⽹上⼀⼤堆的帖⼦在我整合的时候⽆法解答我遇到的问题(莫⾮是脸⿊?),最后还是到了问题的解决点,真是幸运。正⽂
推荐直接上我的git,⿇烦给个星星,谢谢啦。
先上POM代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /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.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</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>
<!--整合thymeleaf依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--整合mybatis依赖-->
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
json格式转换成图片<version>1.3.2</version>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
properties是什么文件<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<!--mysql驱动包-->
<dependency>
正则匹配英文和数字<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--mybatis⾃动⽣成代码插件-->
<plugin>
<groupId&ator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<!-- 是否覆盖,true表⽰会替换⽣成的JAVA⽂件,false则不覆盖 -->
<overwrite>true</overwrite>
</configuration>
<dependencies>
<!--mysql驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
</plugins>
<resources>
<resource>
<!--这个不加的话,代码⽣成器启动会报错-->
<directory>src/main/resources</directory>
<!--扫描resources下的配置⽂件-->
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<!--这个不加的话,thymeleaf就不到模板HTML-->
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>
注意:
很多⼈可能疑惑为什么有两个 <directory>src/main/resources</directory>,在这⾥,我必须吐槽⼀下Springboot静态资源的⽅式(⽹上很多说明,我这⾥就不解释了,反正我集成后如果不加上这⾏代码就会⼀直报BUG:thymeleaf不到HTML,404);
上结构图:
上配置⽂件代码application.properties:
#配置thymeleaf缓存开发期间先关闭,否则影响测试
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:templates/
spring.thymeleaf.suffix=.html
de=LEGACYHTML5
ding=UTF-8
#配置端⼝号以及拦截策略
server.port=8080
#开始配置mysql连接驱动以及数据库连接池参数
spring.datasource.name=mysql_test
pe=com.alibaba.druid.pool.DruidDataSource
input属性还是元素spring.datasource.druid.filters=stat
spring.datasource.druid.sql.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true spring.datasource.druid.username=root
spring.datasource.druid.password=root
#这⾥可以不⽤配置,有默认参数,根据⾃⼰需求
spring.datasource.druid.initial-size=1
spring.datasource.druid.min-idle=1
spring.datasource.druid.max-active=20
spring.datasource.druid.max-wait=6000
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.validation-query=SELECT 'x'
spring.st-while-idle=true
spring.st-on-borrow=false
spring.st-on-return=false
spring.datasource.druid.pool-prepared-statements=false
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
#开始配置mybatis
mybatis.mapper-locations=classpath:mapper/*.xml
generator.properties:
## 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.sql.jdbc.Driver
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-////DTD MyBatis Generator Configuration 1.0//EN"
"/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 导⼊配置⽂件 -->
<properties resource="generator.properties"/>
<!-- defaultModelType="flat" 设置复合主键时不单独为主键创建实体 -->
<context id="MySql" defaultModelType="flat">
<!-- ⽣成的POJO实现java.io.Serializable接⼝ -->
<plugin type="ator.plugins.SerializablePlugin" />
<!--注释-->
<commentGenerator>
<!-- 将数据库中表的字段描述信息添加到注释 -->
<property name="addRemarkComments" value="true"/>
<!-- 注释⾥不添加⽇期 -->
<property name="suppressDate" value="true"/>
</commentGenerator>
<!-- 数据库连接,直接通过${}读取application.properties⾥的配置 -->
<jdbcConnection
driverClass="${spring.datasource.driver-class-name}"
connectionURL="${spring.datasource.url}"
userId="${spring.datasource.username}"
password="${spring.datasource.password}"/>
<!-- ⽣成POJO对象,并将类放到com.demo.pojo包下 -->
<javaModelGenerator targetPackage="com.demo.pojo" targetProject="src/main/java"></javaModelGenerator>
<!-- ⽣成mapper xml⽂件,并放到resources下的mapper⽂件夹下 -->
<sqlMapGenerator targetPackage="resources.mapper" targetProject="src/main"></sqlMapGenerator>
<!-- ⽣成mapper xml对应dao接⼝,放到com.demo.dao包下-->
<javaClientGenerator targetPackage="com.demo.dao" targetProject="src/main/java" type="XMLMAPPER"></javaClientGenerator>
<!-- table标签可以有多个,⾄少⼀个,tableName指定表名,可以使⽤_和%通配符 -->
<table tableName="user">
<!-- 是否只⽣成POJO对象 -->
<property name="modelOnly" value="false"/>
<!-- 数据库中表名有时我们都会带个前缀,⽽实体⼜不想带前缀,这个配置可以把实体的前缀去掉 -->
linux红帽认证考试<!-- <domainObjectRenamingRule searchString="^Tbl" replaceString=""/>-->
</table>
</context>
</generatorConfiguration>
⾄于l则是空的,感兴趣的朋友可以⾃⾏学习yml的配置⽅式
上代码
DemoApplication(SpringBoot默认的启动器):
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论