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.panyuro</groupId>
<artifactId>springboot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>springboot Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<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>
<dependencies>
<!-- thymeleaf启动器 -->
<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>
<!--devtools配置,开启热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>springboot</finalName>
<!--xml的路径配置-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
注:这里要配置xml的路径,因为Idea默认只会查找src/main/resources路径下的xml文件!!!!!
2. 添加 application.properties 全局配置文件(这里主要是数据库、DataSource、包别名的配置)
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/spring
spring.datasource.username=root
spring.datasource.password=mysql
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis.type-aliases-package=com.my.pojo
3 数据库表设计
4.创建实体类(还要加上构造器和set、get方法)
public class User {
private int id;
private String name;
private String password;
5.创建 mapper 接口以及映射配置文件
public interface UserMapper {
public void insertUser(User user);
public List<User> findAllUser();
public User getUserById(Integer id);
public void updateUser(User user);
public void deleteUser(Integer id);
}
<?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.my.mapper.UserMapper">
<insert id="insertUser" parameterType="user">
insert into user(name,password) values(#{name},#{password})
</insert>
<select id="findAllUser" resultType="user" >
select id,name,password from user
</select>
<select id="getUserById" resultType="user">
select id,name,password from user where id=#{value}
</select>
<update id="updateUser" parameterType="user">
update user set name=#{name},password=#{password} where id=#{id}
</update>
<delete id="deleteUser" parameterType="Integer">
delete from user where id=#{value}
</delete>
</mapper>
6.创建业务层 service的接口及实现类
public interface UserService {
void addUser(User user);
public List<User> findAllUser();
public User getUserById(Integer id);
public void updateUser(User user);
public void deleteUser(Integer id);
}
@Service
@Transactional
public class UserServiceImp implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public void addUser(User user) {
this.userMapper.insertUser(user);
}
@Override
public List<User> findAllUser() {
return this.userMapper.findAllUser();
}
@Override
public User getUserById(Integer id) {
return this.userMapper.getUserById(id);
}
@Override
public void updateUser(User user) {
this.userMapper.updateUser(user);
}
@Override
public void deleteUser(Integer id) {
this.userMapper.deleteUser(id);
}
}
7.创建 Controller
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
public UserService userService;
//增加用户
@RequestMapping("/{page}")
public String showPage(@PathVariable String page){
return page;
}
@RequestMapping("/addUser")
public String addUser(User user){
this.userService.addUser(user);
return "ok";
}
//查找所有用户信息
@RequestMapping("/findAllUser")
public String findAllUser(Model model){
List<User> list=this.userService.findAllUser();
model.addAttribute("list",list);
return "showUsers";
}
//根据id查询该用户信息,用来做回显
@RequestMapping("/findUserById")
public String findUserById(Model model,Integer id){
User user=this.userService.getUserById(id);
model.addAttribute("user",user);
return "editUser";
}
//更新用户信息
@RequestMapping("/update")
public String updateUser(User user){
this.userService.updateUser(user);
return "ok";
}
//根据id删除该用户信息,并重定向到查询页面
@RequestMapping("/delete")
public String deleteUser(Integer id){
this.userService.deleteUser(id);
return "redirect:/user/findAllUser";
}
}
8.编写页面
用到了Thymeleaf语法,防止不识别th标签,加上命名空间
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
Thymeleaf语法参考:https://blog.youkuaiyun.com/qq_42922647/article/details/96784957
- input.html
<body>
<form th:action="@{/user/addUser}" method="post">
用户名:<input type="text" name="name"/><br/>
密码: <input type="text" name="password"/>
<input type="submit" value="提交"/>
</form>
</body>
-
ok.html
<body>
操作成功!!
</body>
-
showUsers.html
<body>
<table border="1">
<tr>
<th>用户ID</th>
<th>用户名</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.password}"></td>
<td><a th:href="@{/user/findUserById(id=${user.id})}">更新</a></td>
<td><a th:href="@{/user/delete(id=${user.id})}">删除</a></td>
</tr>
</table>
</body>
-
editUser.html
<body>
<form th:action="@{/user/update}" method="post">
<input type="hidden" name="id" th:field="${user.id}"/>
用户名:<input type="text" name="name" th:field="${user.name}" /><br/>
密码: <input text="text" name="password" th:field="${user.password}"/><br/>
<input type="submit" value="更新"/>
</form>
</body>
9.创建启动类
@SpringBootApplication
@MapperScan("com.my.mapper")//用户扫描MyBatis的Mapper接口
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
运行结果:
附:项目结构: