由于项目需要,基于spring boot可以快速搭建简化spring的相关配置和开发过程。
Thymeleaf是一个XML/XHTML/HTML5模板引擎,由于浏览器解释html时,忽略未定义的标签属性,因此thymeleaf的模板可以静态运行,在浏览器中可以查看静态效果,语法比较清晰简单。美工制作一套静态的页面效果,程序员很容易在上面修改为动态的。
1.maven pom如下
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.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.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.26</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.spring boot例子代码:
UserController.java
@EnableAutoConfiguration
public class UserController {
@Autowired
private UserService userService;
@Autowired
private ConsumeInfoService consumeInfoService;
@Autowired
private PayCheckInfoService payCheckInfoService;
@ResponseBody
@RequestMapping({ "/home" })
public User home() {
User user = userService.selectById(1);
System.out.println("用户信息" + user);
return user;
}
UserService.java:
@Service
public class UserService {
@Autowired
@Qualifier("userMapper")
private UserMapper userMapper;
public void save(User user) {
userMapper.save(user);
}
public User selectById(int id) {
return userMapper.selectById(id);
}
public List<User> queryAll(){
return userMapper.queryAll();
}
public void deleteUser(Integer id){
userMapper.deleteById(id);
}
public void updateById(User user){
userMapper.updateById(user);
}
}
UserMapper.java
@Mapper
public interface UserMapper {
int save(User user);
User selectById(Integer member_id);
int updateById(User user);
int deleteById(Integer member_id);
List<User> queryAll();
}
3.thymeleaf使用:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<tr th:each="prod:${users}">
<td th:text="${prod.member_id}"></td>
<td th:text="${prod.member_name}"></td>
<td th:text="${prod.member_birthday}"></td>
<td th:text="${prod.due_date}"></td>
<td th:text="${prod.phone_number}"></td>
<td th:text="${prod.available_amount}"></td>
<td th:text="${prod.total_consumption}"></td>
<td th:text="${prod.remarks}"></td>
页面效果: