spring boot 项目搭建
1.idea 直接选择spring insertall 创建项目。
2.注释掉:(如果只是跑demo不会有问题,如果要使用数据库就可能会报错或者jar包冲突),代码如下:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
3.如果选择了mybatis项目就注释掉:(mybatis的使用时需要配置的,据说小demo不配置mybatis的话会报错,但是我试了试似乎并没有)代码如下:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
这样,写个controller,写个html 页面,小demo就成型了
thymeleft 管理页面
pom.xml 添加代码:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
application.properties配置
#thymeleaf start
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
#static 文件夹下的静态文件访问路径
spring.mvc.static-path-pattern=/**
#thymeleaf end
-------------------------------------------雷区警戒线--------------------------------------------
坑1:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
填坑:
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
在启动项添加这句,便可以解决。
原理:
spring boot 会默认加载org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration这个类,而DataSourceAutoConfiguration类使用了@Configuration注解向spring注入了dataSource bean,又因为项目(eureka-server模块和短信模块)中并没有关于dataSource相关的配置信息,所以当spring创建dataSource bean时因缺少相关的信息就会报错。
所以@SpringBootApplication注解上加上exclude,解除自动加载DataSourceAutoConfiguration。
坑2:
项目布好了,习惯性写了一个包装view界面(HTML啊,jsp啊之类的),然后跑项目,发现路径咋写都进不了页面。
填坑:
不要在没有配置的情况下把页面写到其他包里,系统默认读取的包是src\main\resources\templates包,所以作为小白还是乖乖照案例写吧。
------------------------------------------------------我不是系统的生产者,我只是bug的搬运工------------------------------------