10springBoot结合Thymeleaf小案例
2020-01-26 本文已影响0人
wshsdm
1 创建mysql数据库
DROP TABLE IF EXISTS `tuser`;
CREATE TABLE `tuser` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`uname` varchar(10) DEFAULT NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
2 创建SpringBoot项目,并添加相应的组件
2.1 pom.xml
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.4.1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.20</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</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>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
2.2 创建对应实体类
package com.demo.po;
public class Tuser {
private int uid;
private String uname;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
}
2.3 编写springBoot属性文件
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?serverTimezone=UTC&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
mybatis.mapper-locations=classpath:mapper/user.mapper.xml
mybatis.type-aliases-package=com.demo.po
# thymeleaf
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
#spring.thymeleaf.content-type=text/html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.cache=false
2.4 编写自定义配置类
package com.demo.cfg;
import javax.sql.DataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration
@MapperScan("com.demo.dao")
public class MyDaofig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource getDs() {
return new DruidDataSource();
}
}
2.5 编写DAO接口
package com.demo.dao;
import java.util.List;
import com.demo.po.Tuser;
public interface UserDao {
public int save(Tuser u);
public List<Tuser> findall();
public Tuser findbyid(int uid);
public int update(Tuser u);
public int delete(int uid);
}
2.6 编写mybatis操作映射文件
src/main/resources/mapper/user.mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.demo.dao.UserDao">
<insert id="save" parameterType="tuser">
insert into tuser values(null,#{uname})
</insert>
<select id="findall" resultType="tuser">
select * from tuser
</select>
<select id="findbyid" resultType="tuser" parameterType="int">
select * from tuser where uid=#{x}
</select>
<update id="update" parameterType="tuser">
update tuser set uname=#{uname} where uid=#{uid}
</update>
<delete id="delete" parameterType="int">
delete from tuser where uid=#{uid}
</delete>
</mapper>
2.7 业务bean
package com.demo.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.demo.dao.UserDao;
import com.demo.po.Tuser;
@Service
public class UserService {
@Autowired
private UserDao dao;
public void save(Tuser u) {
dao.save(u);
}
public List<Tuser> finall() {
return dao.findall();
}
public Tuser findbyid(int uid) {
return dao.findbyid(uid);
}
public int update(Tuser u) {
return dao.update(u);
}
public int delete(int uid) {
return dao.delete(uid);
}
}
3 视图部分
3.1 控制器代码
package com.demo.action;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.demo.po.Tuser;
import com.demo.service.UserService;
@Controller
public class UserAction {
@Autowired
private UserService userSer;
@RequestMapping("/toadd")
public String toadd(){
return "add";
}
@RequestMapping("/add")
public String add(Tuser user) {
userSer.save(user);
return "redirect:list";
}
@RequestMapping("/list")
public ModelAndView findall() {
ModelAndView mv = new ModelAndView("list");
List<Tuser> li = userSer.finall();
mv.addObject("list", li);
return mv;
}
}
3.2 页面代码部分
src/main/resources/templates
add.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="webjars/bootstrap/4.4.1/css/bootstrap.min.css">
<script type="text/javascript" src="webjars/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript" src="webjars/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<form action="add" method="post">
user name: <input type="text" name="uname"><br>
<button class="btn btn-info">添加</button>
</form>
</body>
</html>
3.3 list.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="webjars/bootstrap/4.4.1/css/bootstrap.min.css">
<script type="text/javascript" src="webjars/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript" src="webjars/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<a href="toadd" th:href="@{/toadd}" class="btn btn-primary">添加</a>
<table class="table table-hover">
<thead>
<tr>
<th>#</th>
<th>User Name</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr th:each="xuser : ${list}">
<th scope="row" th:text="${xuser.uid}">1</th>
<td th:text="${xuser.uname}">neo</td>
<td><a th:href="@{/getid(id=${xuser.uid})}">edit</a></td>
<td><a th:href="@{/delete(id=${xuser.uid})}">delete</a></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
4 sts生成的测试代码
package com.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpbootmybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpbootmybatisApplication.class, args);
}
}