springboot+maven+SSM(thymeleaf)整合教程
springboot+maven+SSM(thymeleaf)整合
首先我们来讲一下springboot,springboot主张无xml配置,但是还是需要一些最基础的信息配置的,例如sql账号密码的设置,在简洁你的账号密码还是需要你自己配置的,它是没办法帮你自动生成的。所以一般配置文件都是放到resources下的;
然后再提下maven,maven是一个为我们管理jar包的,他会为我们自动生成项目骨架,maven中的坐标、仓库、聚合和继承等等的我就不再提了,大家自行去学习;
咱们在搭建三层的时候,把三层都放在Application类所在的包下,原因很简单,SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描,如果Application类所在的包为:com.boot.app,则只会扫描com.boot.app包及其所有子包,如果service或dao所在包不在com.boot.app及其子包下,则不会被扫描到;
在这里再提几个注解:
@Controller :处理http请求
@RestController :Spring4之后新加的注解,返回原来的json需要@ResponseBody配合@Controller
@RequestMapping :配置URL映射
@PathVariable :获取URL中的数据
@RequestParam :获取请求参数的值
@GetMapping :组合注解
@repository来注解接口,我们照样可以注入到接口的实现类)
现在进入编码时间,首先是咱们的实体类:
实际上呢,我的实体类是根据数据库中的表自动创建的,如果要自动创建实体类而非手动,你需要引入依赖,这里就不再讲了,接下来是dao层接口:
这里我就只演示一个查询数量的方法,因为当你学会一个之后,你会发现你特别有成就感,这个时候去自主解决剩下的,你会感觉到那种学习的乐趣,下面是dao层的mapper.xml:
这边我用的是sql标签,其实只用select标签也是完全没问题的,但是我比较喜欢用一些新鲜的东西来充实我的技术点,接下来是业务层接口:
业务层实现类:
yml配置:
接下来是最重要的pom.xml配置:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion><groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging><name>demo</name>
<description>Demo project for Spring Boot</description><parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent><properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties><dependencies>
<!-- thymeleaf依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency><dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</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>
</plugins>
</build>
</project>
然后是控制层:
控制层用@RestController注解也可以实现,但是我这里引用的是thymeleaf模块,所以我这里用的是@Controller注解,Thymeleaf与Spring集成后,你可以:
在SpringMVC应用中完全替代JSP文件,就像控制JSP一样,使用SpringMvc的@Controller注解来映射;
Thymeleaf的模板文件在模板中使用SpringEL表达式来替换OGNL;
在模板中创建的表单,完全支持Beans和结果的绑定,包括使用PropertyEditor,转换,和验证等;
可以通过Spring来管理国际化文件显示国际化信息;
这就是一个最简单的thymeleaf模板,以上一切OK了后,你运行就可以去浏览器里面看到结果了,好了,项目整合到此结束!
谢谢观看!!