1 概述
通过使用SpringBoot、SpringMVC、MyBatis实现对user表的CRUD操作
2 创建项目
具体的创建步骤请参考前三章中的描述步骤及图片进行操作。从本章开始只展示关键的配置文件。
2.1 pom文件
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<groupId>com.liulg</groupId>
<artifactId>07-spring-boot-springmvc-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>d
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
</dependencies>
</project>
2.2 application.properties配置文件
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis.type-aliases-package=com.liulg.pojo
2.3 数据库表
CREATE TABLE ‘user’ (
‘id’ int(11) NOT NULL AUTO_INCREMENT,
‘name’ varchar(255) DEFAULT NULL,
‘age’ int(11) DEFAULT NULL,
PRIMARY KEY (‘id’)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3 添加用户
用户通过input.html页面填写并提交姓名和年龄信息,将信息保存到数据库中。
3.1 实体类
package com.liulg.pojo;
public class User {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
3.2 Mapper接口
package com.liulg.mapper;
import com.liulg.pojo.User;
public interface UserMapper {
public void insertUser(User user);
}
3.3 映射配置文件
<?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.liulg.mapper.UserMapper">
<insert id="insertUser" parameterType="user">
insert into user(name,age) values(#{name},#{age})
</insert>
</mapper>
3.4 Service
接口类
package com.liulg.service;
import com.liulg.pojo.User;
public interface UserService {
public void addUser(User user);
}
实现类
package com.liulg.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.liulg.mapper.UserMapper;
import com.liulg.pojo.User;
import com.liulg.service.UserService;
@Service
@Transactional
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
@Override
public void addUser(User user) {
userMapper.insertUser(user);
}
}
3.5 Controller
package com.liulg.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.liulg.pojo.User;
import com.liulg.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/{page}")
public String showPage(@PathVariable String page) {
return page;
}
/***
* 添加用户
* @param user
* @return
*/
@RequestMapping("/add")
public String addUser(User user) {
userService.addUser(user);
return "success";
}
}
3.6 页面
信息输入页面:input.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method="post" th:action="@{/user/add}">
姓名:<input type="text" name="name"/> <br/>
年龄:<input type="text" name="age"/> <br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
保存成功提示页面:success.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>提示信息</title>
</head>
<body>
保存成功!!!
</body>
</html>
3.7 启动类
package com.liulg;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.liulg.mapper") //mapper接口的扫描位置
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
4 查询用户
对应上一章节的代码文件位置,添加对应的方法。实现查询所有的用户信息。
4.1 Mapper接口
public List<User> selectAll();
4.2 映射配置文件
<select id="selectAll" resultType="user">
select * from user
</select>
4.3 Service
接口类
public List<User> findAllUser();
实现类
public List<User> findAllUser() {
return userMapper.selectAll();
}
4.4 Controller
/***
* 查找数据表中的所有用户信息
* @param model
* @return
*/
@RequestMapping("/findAll")
public String findAll(Model model) {
List<User> list = userService.findAllUser();
model.addAttribute("list", list);
return "showList";
}
4.5 页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>查询用户信息</title>
</head>
<body>
<table>
<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="@{/user/edit(id=${user.id})}">更新用户</a></td>
<td><a th:href="@{/user/delete(id=${user.id})}">删除</a></td>
</tr>
</table>
</body>
</html>
5 更新用户
在上一章节的所有用户信息列表页面,添加编辑按钮,通过用户ID查询用户信息,并将用户信息在编辑页面进行回显,然后修改用户信息,并提交保存,实现对用户信息的编辑更新。
5.1 Mapper接口
public User findUserById(Integer id);
public void updateUser(User user);
5.2 映射配置文件
<select id="findUserById" resultType="user">
select * from user where id=#{id}
</select>
<update id="updateUser" parameterType="user">
update user set name=#{name},age=#{age} where id=#{id}
</update>
5.3 Service
接口类
public User findUserById(Integer id);
public void updateUser(User user);
实现类
public User findUserById(Integer id) {
return userMapper.findUserById(id);
}
public void updateUser(User user) {
userMapper.updateUser(user);
}
5.4 Controller
/***
* 根据用户id查找用户信息
* @param id
* @return
*/
@RequestMapping("/edit")
public String edit(Integer id,Model model) {
User user = userService.findUserById(id);
model.addAttribute("user", user);
return "edit";
}
/***
* 更新用户信息
* @param id
* @return
*/
@RequestMapping("/update")
public String updateUser(User user,Model model) {
userService.updateUser(user);
return findAll(model);
}
5.5 页面
编辑页面:edit.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>编辑用户信息</title>
</head>
<body>
<form method="post" th:action="@{/user/update}">
<input type="hidden" name="id" th:field="${user.id}"/><br/>
姓名:<input type="text" name="name" th:field="${user.name}"/> <br/>
年龄:<input type="text" name="age" th:field="${user.age}"/> <br/>
<input type="submit" value="更新"/>
</form>
</body>
</html>
6 删除用户
在上一章节的所有用户信息列表页面,添加删除按钮,通过用户ID删除用户信息。
6.1 Mapper接口
public void deleteUser(Integer id);
6.2 映射配置文件
<delete id="deleteUser" >
delete from user where id=#{id}
</delete>
6.3 Service
接口类
public void deleteUser(Integer id);
实现类
@Override
public void deleteUser(Integer id) {
userMapper.deleteUser(id);
}
6.4 Controller
/***
* 根据用户id删除用户
* @param id
* @return
*/
@RequestMapping("/delete")
public String deleteUser(Integer id) {
userService.deleteUser(id);
return "redirect:/user/findAll";
}