1. pom 配置文件
<?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.zth.SpingBoot</groupId>
<artifactId>SpingBoot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpingBoot Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<!-- springBoot 的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- web 启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Mybatis 启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mysql 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
</dependencies>
<build>
<finalName>SpingBoot</finalName>
<!--xml的路径配置-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
注:这里要配置xml的路径,因为默认只会查找 src/main/resources 路径下的xml文件!!!!!
2. 添加 application.properties 全局配置文件
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/zth?useSSL=true
spring.datasource.username=root
spring.datasource.password=mysql
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis.type-aliases-package=com.zth.entity
3. 数据库表
4. java bean
public class Users {
private Integer id;
private String name;
private Integer age;
/*getter and setter*/
}
5. mapper 接口
public interface UsersMapper {
void insertUser(Users users);
List<Users> getAllUsers ();
Users getUsersById(Integer id);
void updateUser(Users users);
void deleteUserById(Integer id);
}
6. 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.zth.mapper.UsersMapper">
<insert id="insertUser" parameterType="users">
insert into users(name,age) values (#{name},#{age})
</insert>
<select id="getAllUsers" resultType="users">
select * from users
</select>
<select id="getUsersById" resultType="users">
select * from users where id = #{id}
</select>
<update id="updateUser" parameterType="users">
update users set name=#{name} ,age=#{age} where id=#{id}
</update>
<delete id="deleteUserById">
delete from users where id=#{id}
</delete>
</mapper>
7. service 的接口及实现类
public interface UsersService {
void addUser(Users users);
List<Users> getAllUser();
public Users getUserById(Integer id);
public void updateUser(Users users);
public void deleteUserById(Integer id);
}
实现类:
@Service
@Transactional
public class UserServiceImpl implements UsersService {
@Autowired
private UsersMapper usersMapper;
@Override
public void addUser(Users users) {
usersMapper.insertUser(users);
}
@Override
public List<Users> getAllUser() {
return usersMapper.getAllUsers();
}
@Override
public Users getUserById(Integer id) {
return usersMapper.getUsersById(1);
}
@Override
public void updateUser(Users users) {
usersMapper.updateUser(users);
}
@Override
public void deleteUserById(Integer id) {
usersMapper.deleteUserById(id);
}
}
8. 创建 Controller
@Controller
@RequestMapping("/users")
public class UsersController {
@Autowired
private UsersService usersService;
/**
* 页面跳转
*/
@RequestMapping("/{page}")
public String showPage(@PathVariable String page){
return page;
}
/**
* 添加用户
*/
@RequestMapping("/addUser")
public String addUser(Users users){
usersService.addUser(users);
return "ok";
}
/**
* 获取所有用户
*/
@RequestMapping("/getAll")
public String getAll(Model model){
List<Users> list = usersService.getAllUser();
System.out.println(list);
model.addAttribute("list",list);
return "showUsers";
}
/**
* 根据id查询
*/
@RequestMapping("/findUserById")
public String findUserById(Integer id,Model model){
Users user = this.usersService.getUserById(id);
model.addAttribute("user", user);
return "updateUser";
}
/**
* 修改用户
*/
@RequestMapping("/editUser")
public String editUser(Users users){
this.usersService.updateUser(users);
return "ok";
}
@RequestMapping("/delUser")
public String editUser(int id){
this.usersService.deleteUserById(id);
return "ok";
}
}
8.编写页面
用到了Thymeleaf语法,防止不识别 th 标签,加上命名空间
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
input.html:
<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>添加用户</title>
</head>
<body>
<form th:action="@{/users/addUser}" method="post">
用户姓名:<input type="text" name="name"/><br/>
用户年龄:<input type="text" name="age"/><br/>
<input type="submit" value="确定"/><br/>
</form>
</body>
</html>
ok.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>操作提示页面</title>
</head>
<body>
操作成功!!!
</body>
</html>
showUsers.html
<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>展示用户数据</title>
</head>
<body>
<table border="1" style="width:500px;">
<tr>
<th>用户ID</th>
<th>用户姓名</th>
<th>用户年龄</th>
<th>操作</th>
</tr>
<tr th:each="user : ${list}">
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.age}"></td>
<td>
<a th:href="@{/users/findUserById(id=${user.id})}">更新用户</a>
<a th:href="@{/users/delUser(id=${user.id})}">删除用户</a>
</td>
</tr>
</table>
</body>
</html>
updateUser.html:
<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form th:action="@{/users/editUser}" method="post">
<input type="hidden" name="id" th:field="${user.id}"/>
用户姓名:<input type="text" name="name" th:field="${user.name}"/><br/>
用户年龄:<input type="text" name="age" th:field="${user.age}"/><br/>
<input type="submit" value="确定"/><br/>
</form>
</body>
</html>
9. 运行结果:
添加:
显示所有:
修改:
文件结构: