目标:
- 实现添加功能
- 实现查询功能
- 实现删除功能
- 实现修改功能
- 添加日期转换器
1.搭建项目
1.1pom文件:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</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-test</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.19</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<!-- 如果不添加此节点src/main/java目录下的所有配置文件都会被漏掉。 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
1.2链接数据库application.yml:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3305/springboot
username: root
password:
type: com.alibaba.druid.pool.DruidDataSource
logging:
level:
com:
by:
mapper: DEBUG
1.3构建启动类:SpringbootMybatisApplication
@SpringBootApplication
@MapperScan("com.by.mapper")
public class SpringbootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisApplication.class, args);
}
}
2.添加功能实现:
2.1add_user.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>添加用户</title>
</head>
<body>
<h3>新增用户</h3>
<hr/>
<form action="/user/addUser" method="post">
姓名:<input type="text" name="name"/><br/>
密码:<input type="text" name="pwd"/><br/>
性别:<input type="radio" name="sex" value="1"/>女
<input type="radio" name="sex" value="0"/>男<br/>
生日:<input type="text" name="birth"/><br/>
<input type="submit" value="确定"/><br/>
</form>
</body>
</html>
2.2对应实现controller,service,mapper层代码
2.2.1controller层代码:
/*
* Copyright (c) 2020, 2024, All rights reserved.
*
*/
package com.by.controller;
import com.by.pojo.User;
import com.by.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.*;
import java.util.List;
/**
* <p>Project: springboot - UserController</p>
* <p>Powered by scl On 2024-01-15 16:25:51</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
/**
* 添加
*
* @param user
* @return
*/
@RequestMapping("/addUser")
public String addUser(User user) {
userService.addUser(user);
return "redirect:/user/selectUser";
}
}
2.2.2实现service层代码:
/*
* Copyright (c) 2020, 2024, All rights reserved.
*
*/
package com.by.service;
import com.by.pojo.User;
import java.util.List;
public interface UserService{
void addUser(User user);
}
实现类:
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public void addUser(User user) {
userMapper.addUser(user);
}
}
2.2.3实现mapper层代码:
package com.by.mapper;
import com.by.pojo.User;
import java.util.List;
/**
* <p>Project: springboot - UserMapper</p>
* <p>Powered by scl On 2024-01-15 14:51:39</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
public interface UserMapper {
void addUser(User user);
}
映射文件:
<?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.by.mapper.UserMapper">
<insert id="addUser" parameterType="com.by.pojo.User">
insert into user(name,sex,pwd,birth) values(#{name},#{sex},#{pwd},#{birth})
</insert>
</mapper>
3.查询功能实现
3.1select_user.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
<link rel="shortcut icon" href="../resources/favicon.ico" th:href="@{/static/favicon.ico}"/>
<style type="text/css">
table {border-collapse: collapse; font-size: 14px; width: 80%; margin: auto}
table, th, td {border: 1px solid darkslategray;padding: 10px}
</style>
</head>
<body>
<div style="text-align: center">
<span style="color: darkslategray; font-size: 30px">欢迎光临!</span>
<hr/>
<a href="/add_user">添加</a>
<table class="list">
<tr>
<th>id</th>
<th>姓名</th>
<th>密码</th>
<th>性别</th>
<th>生日</th>
<th>操作</th>
</tr>
<tr th:each="user : ${userList}">
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.pwd}"></td>
<td th:text="${user.sex==1}?'女':'男'"></td>
<td th:text="${#dates.format(user.birth,'yyyy-MM-dd')}"></td>
<td>
<a th:href="@{/user/deleterUserById/{id}(id=${user.id})}">删除</a>
<a th:href="@{/user/getUserById/{id}(id=${user.id})}">修改</a>
</td>
</tr>
</table>
</div>
</body>
</html>
3.2实现对应的controller、service、mapper层代码
3.2.1controller层代码
/**
* 查询所有用户信息
*
* @param model
* @return
*/
@RequestMapping("/selectUser")
public String selectUser(Model model) {
List<User> userList = userService.selectUser();
model.addAttribute("userList", userList);
return "select_user";
}
3.2.2service层代码
List<User> selectUser();
@Override
public List<User> selectUser() {
return userMapper.selectUser();
}
3.2.3mapper层代码
List<User> selectUser();
<select id="selectUser" resultType="com.by.pojo.User">
select *
from user;
</select>
4.删除功能实现
4.1实现对应的controller、service、mapper层代码
4.1.1controller层代码
/**
* 根据id删除用户
*
* @param id
* @return
*/
@GetMapping("/deleterUserById/{id}")
public String deleterUserById(@PathVariable Integer id) {
userService.deleterUserById(id);
return "redirect:/user/selectUser";
}
4.1.2service层代码
void deleterUserById(Integer id);
@Override
public void deleterUserById(Integer id) {
userMapper.deleterUserById(id);
}
4.1.3mapper层代码
void deleterUserById(Integer id);
<delete id="deleterUserById" parameterType="java.lang.Integer">
delete from user where id =#{id}
</delete>
5.修改功能实现
5.1update_user.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>添加用户</title>
</head>
<body>
<h3>新增用户</h3>
<hr/>
<form action="/user/updateUserById" method="post">
<input type="hidden" name="id" th:value="${user.id}">
姓名:<input type="text" name="name" th:value="${user.name}"/><br/>
密码:<input type="text" name="pwd" th:value="${user.pwd}"/><br/>
性别:<input type="radio" name="sex" value="1" th:checked="${user.sex==1}?'true':'false'"/>女
<input type="radio" name="sex" value="0" th:checked="${user.sex==0}?'true':'false'"/>男<br/>
生日:<input type="text" name="birth" th:value="${#dates.format(user.birth,'yyyy-MM-dd')}"/><br/>
<input type="submit" value="确定"/><br/>
</form>
</body>
</html>
5.2实现对应的controller、service、mapper层代码
5.2.1controller层代码
/**
* 数据回显
*
* @param id
* @return
*/
@GetMapping("/getUserById/{id}")
public String getUserById(@PathVariable Integer id, Model model) {
User user = userService.getUserById(id);
model.addAttribute("user", user);
return "update_user";
}
/**
* 修改
*
* @param
* @return
*/
@PostMapping("/updateUserById")
public String updateUserById(User user) {
userService.updateUserById(user);
return "redirect:/user/selectUser";
}
5.2.2service层代码
User getUserById(Integer id);
void updateUserById(User user);
@Override
public User getUserById(Integer id) {
return userMapper.getUserById(id);
}
@Override
public void updateUserById(User user) {
userMapper.updateUserById(user);
}
5.2.3mapper层代码
User getUserById(Integer id);
void updateUserById(User user
<select id="getUserById" resultType="com.by.pojo.User" parameterType="java.lang.Integer">
select * from user where id=#{id}
</select>
<update id="updateUserById" parameterType="com.by.pojo.User">
update user
<set>
<if test="name!=null and name!=''">
name=#{name},
</if>
<if test="sex!=null">
sex=#{sex},
</if>
<if test="pwd!=null and pwd!=''">
pwd=#{pwd},
</if>
<if test="birth!=null">
birth=#{birth},
</if>
</set>
where id =#{id}
</update>
6.添加日期转换器
DateConverter:(日期转换器)
/*
* Copyright (c) 2020, 2024, All rights reserved.
*
*/
package com.by.converter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* <p>Project: springboot - DateConverter</p>
* <p>Powered by scl On 2024-01-15 17:09:15</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
/**
* 日期转换器
*/
@Component
public class DateConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
try {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.parse(source);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
WebmvcConfig:(将日期转换器装到容器中)文章来源:https://www.toymoban.com/news/detail-797594.html
/*
* Copyright (c) 2020, 2024, All rights reserved.
*
*/
package com.by.config;
import com.by.converter.DateConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.FormatterRegistry;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* <p>Project: springboot - WebmvcConfig</p>
* <p>Powered by scl On 2024-01-15 17:17:46</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
/**
* 将日期转换器注入到容器中
*/
@Component
public class WebmvcConfig implements WebMvcConfigurer {
@Autowired
private DateConverter dateConverter;
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(dateConverter);
}
}
文章来源地址https://www.toymoban.com/news/detail-797594.html
到了这里,关于Spring Boot整和MyBatis!!!的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!