SpringBoot从入门到精通教程(六)
之前学了,这么多东西
thyemeaf
、MyBatis
还有 配置文件等等,今天我们就来做一个小案例
CRUD,程序员的必备
项目结构
pom.xml
<!-- mybatis 相关依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- thymeleaf 相关依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
3. application.properties
logging.level.org.springframework=DEBUG
#springboot mybatis
mybatis.mapper-locations = classpath:mapper/*Mapper.xml
mybatis.config-location = classpath:mapper/config/sqlMapConfig.xml
mybatis.type-aliases-package = com.spiritmark.demo.model
#数据库
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/springboot_test?serverTimezone=GMT
spring.datasource.username = root
spring.datasource.password = 123qwe
- UserController.java
package com.spiritmark.demo.controller;
import java.util.List;
import com.spiritmark.demo.model.User;
import com.spiritmark.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/index")
public String showUser(Model model){
List<User> list = userService.findAll();
model.addAttribute("list",list);
return "index";
}
// 进入添加界面
@RequestMapping("/intoAdd")
public String intoAdd(){
return "add";
}
// 添加用户
@RequestMapping("/add")
public String add(User user){
userService.add(user);
return "redirect:/index";
}
// 删除用户
@RequestMapping("/delete")
public String delete(int id){
userService.delete(id);
return "redirect:/";
}
// 进入修改用户
@RequestMapping("/intoUpdate")
public String intoUpdate(Model model,int id){
User user = userService.findById(id);
model.addAttribute("user",user);
return "update";
}
// 修改用户
@RequestMapping("/update")
public String update(User user){
userService.update(user);
return "redirect:/index";
}
}
- UserMapper.java
package com.spiritmark.demo.mapper;
import com.spiritmark.demo.model.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
// 获取所有
List<User> findAll();
// 新增
void add(User user);
// 删除
void delete(int id);
// 编辑
void update(User user);
// 查询
User findById(int id);
}
- UserServiceImpl.java
package com.spiritmark.demo.service.impl;
import java.util.List;
import java.util.Map;
import com.spiritmark.demo.mapper.UserMapper;
import com.spiritmark.demo.model.User;
import com.spiritmark.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
// 获取所有
@Override
public List<User> findAll() {
return userMapper.findAll();
}
// 新增
@Override
public void add(User user) {
userMapper.add(user);
}
// 删除
@Override
public void delete(int id) {
userMapper.delete(id);
}
// 修改
@Override
public void update(User user) {
userMapper.update(user);
}
// 查询
@Override
public User findById(int id) {
return userMapper.findById(id);
}
}
- UserService.java
package com.spiritmark.demo.service;
import com.spiritmark.demo.model.User;
import java.util.List;
public interface UserService {
// 获取所有
List<User> findAll();
// 新增
void add(User user);
// 删除
void delete(int id);
// 编辑
void update(User user);
// 查询
User findById(int id);
}
- UserMapper.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.spiritmark.demo.mapper.UserMapper">
<select id="findAll" resultType="com.spiritmark.demo.model.User">
select * from user
</select>
<insert id="add" parameterType="com.spiritmark.demo.model.User">
insert into user (username, password)
values (#{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR})
</insert>
<delete id="delete" parameterType="java.lang.Integer">
delete from user where id= #{id,jdbcType=INTEGER}
</delete>
<select id="findById" resultType="com.spiritmark.demo.model.User">
select * from user where id= #{id,jdbcType=INTEGER}
</select>
<update id="update" parameterType="com.spiritmark.demo.model.User">
update user set
username= #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR}
where id= #{id,jdbcType=INTEGER}
</update>
</mapper>
index.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title></title>
</head>
<body>
<a th:href="@{/intoAdd}">添加</a>
<table>
<thead>
<tr>
<th>id</th>
<th>username</th>
<th>password</th>
<th>op</th>
</tr>
</thead>
<tbody>
<tr th:each="user : ${list}">
<td th:text="${user.id}"></td>
<td th:text="${user.username}"></td>
<td th:text="${user.password}"></td>
<td><a th:href="@{/delete(id=${user.id})}">删除</a><a th:href="@{/intoUpdate(id=${user.id})}">修改</a></td>
</tr>
</tbody>
</table>
</body>
</html>
add.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title></title>
</head>
<body>
<form th:action="@{/add}" method="post">
<input type="text" id="username" name="username">
<input type="text" id="password" name="password">
<input type="submit">
</form>
</body>
</html>
update.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title></title>
</head>
<body>
<form th:action="@{/update}" th:object="${user}" method="post">
<input type="hidden" name="id" th:value="*{id}">
<input type="text" name="username" id="username" th:value="*{username}">
<input type="text" name="password" id="password" th:value="*{password}">
<input type="submit">
</form>
</body>
</html>