整合过程:https://www.isdxh.com/68.html

一、增——增加用户

1.创建实体类

package com.dxh.pojo;
public class Users {
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;
}
}

2.创建mapper接口以及映射配置文件

package com.dxh.mapper;
import com.dxh.pojo.Users;
public interface UsersMapper {
void insertUser(Users users);
}
<?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.dxh.mapper.UsersMapper">
<!-- 在properties文件中配置过别名了,所以parameterType不需要写Users的包的名称了 -->
<insert id="insertUser" parameterType="Users">
insert into users(name,age) values (#{name},#{age})
</insert>
</mapper>

3.创建业务层

package com.dxh.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import com.dxh.mapper.UsersMapper;
import com.dxh.pojo.Users;
import com.dxh.service.UsersService; @Service
@Transactional
public class UserServiceImpl implements UsersService{ @Autowired
private UsersMapper usersMapper; @Override
public void addUser(Users users) {
this.usersMapper.insertUser(users);
}
}
package com.dxh.service;

import com.dxh.pojo.Users;

public interface UsersService {
void addUser(Users users);
}

4.创建Controller

package com.dxh.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.dxh.pojo.Users;
import com.dxh.service.UsersService; @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) {
this.usersService.addUser(users);
return "ok";
}
}

5.编写页面:src/main/resources/templates/ input.html 和 ok.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>AddUser</title>
</head>
<body>
<form th:action="@{/users/addUser}" method="post">
userName: <input type="text" name="name"/></br>
userAge: <input type="text" name="age"/></br>
<input type="submit" value="SUBMIT">
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>OK</title>
</head>
<body>
addUser Success!
</body>
</html>

6.启动类:

  • 新增注解:@MapperScan("com.dxh.mapper") //用于扫描mybatis的Mapper接口
package com.dxh;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
@MapperScan("com.dxh.mapper") //用于扫描mybatis的Mapper接口
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}

二、查——查询用户

1.在mapper接口中以及映射配置文件中添加相关代码

List<Users> selectUserAll();
<select id="selectUserAll" resultType="Users">
SELECT id,name,age from users
</select>

2.在业务层中添加查询方法

List<Users> findUserAll();
	@Override
public List<Users> findUserAll() {
return this.usersMapper.selectUserAll();
}

3.编写controller

/**
* 查询全部用户
*/
@RequestMapping("/findUserAll")
public String findUserAll(Model model) {
List<Users> list = this.usersService.findUserAll();
model.addAttribute("list",list);
return "showUsers";
}

4.创建页面

在src/main/resources/templates/showUsers.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>展示用户数据</title>
</head>
<body>
<table border="1" style="width:300px">
<tr>
<th>ID</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>
</tr>
</table>
</body>
</html>

5.访问

http://localhost:8080/users/findUserAll

三、改——用户更新

分为两部分,一是数据回显,二是提交页面

1.修改Mapper文件和映射配置

Users selectUsersById(Integer id);
<select id="selectUsersById" resultType="Users" parameterType="int">
SELECT id,name,age from users where id = #{id}
</select>

2.修改业务层代码

Users findUserById(Integer id);
	@Override
public Users findUserById(Integer id) {
return this.usersMapper.selectUsersById(id);
}

3.编写Controller

	/**
* 根据用户id查询用户
*/
@RequestMapping("/findUserById")
public String findUserById(Integer id ,Model model) {
Users user = this.usersService.findUserById(id);
model.addAttribute("user",user);
return "updatePage";
}

4.在src/main/resources/templates/updatePage.html创建html页面

<!DOCTYPE html>
<html>
<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}">
userName: <input type="text" name="name" th:field="${user.name}"/></br>
userAge: <input type="text" name="age" th:field="${user.age}"/></br>
<input type="submit" value="SUBMIT">
</form>
</body>
</html>

5.编辑showUsers.html文件

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>展示用户数据</title>
</head>
<body>
<table border="1" style="width:300px">
<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>
</td>
</tr>
</table>
</body>
</html>

6.修改Mapper接口和映射配置文件

void updateUser(Users users);
	<update id="updateUser" parameterType="Users">
update users set name=#{name} , age=#{age} where id=#{id}
</update>

7.修改业务层代码

void updateUser(Users users);
	@Override
public void updateUser(Users users) {
this.usersMapper.updateUser(users);
}

8.编写controller

	@RequestMapping("/editUser")
public String editUser(Users users,Model model) {
this.usersService.updateUser(users);
return "ok";
}

四、删——删除用户

1.修改Mapper以及映射配置文件

void deleteUserByid(Integer id);
	<delete id="deleteUserByid">
delete from users where id = #{id}
</delete>

2.修改业务层方法

void deleteUserById(Integer id);
	@Override
public void deleteUserById(Integer id) {
this.usersMapper.deleteUserByid(id);
}

3.修改controller

	/**
* 删除用户
*/
@RequestMapping("/delUser")
public String delUser(Integer id) {
this.usersService.deleteUserById(id);
return "redirect:/users/findUserAll";
}

4.修改showUsers.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>展示用户数据</title>
</head>
<body>
<table border="1" style="width:300px">
<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></br>
<a th:href="@{/users/delUser(id=${user.id})}">删除用户</a>
</td>
</tr>
</table>
</body>
</html>

5.完成!

【SpringBoot】11-1.Springboot整合Springmvc+Mybatis增删改查操作(下)的更多相关文章

  1. springmvc+spring3+hibernate4框架简单整合,简单实现增删改查功能

    转自:https://blog.csdn.net/thinkingcao/article/details/52472252 C 所用到的jar包     数据库表 数据库表就不用教大家了,一张表,很简 ...

  2. 用SpringBoot+MySql+JPA实现对数据库的增删改查和分页

    使用SpringBoot+Mysql+JPA实现对数据库的增删改查和分页      JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述 ...

  3. MyBatis增删改查

    MyBatis的简介: MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名 ...

  4. MyBatis增删改查模板

    1. 首先,和Spring整合一下 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=& ...

  5. springMVC实现增删改查

    首先需要准备好一张数据库表我这里用emp这张表:具体代码: /* SQLyog 企业版 - MySQL GUI v8.14 MySQL - 5.1.73-community ************* ...

  6. MyBatis批量增删改查操作

      前文我们介绍了MyBatis基本的增删该查操作,本文介绍批量的增删改查操作.前文地址:http://blog.csdn.net/mahoking/article/details/43673741 ...

  7. 【Mybatis】简单的mybatis增删改查模板

    简单的mybatis增删改查模板: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE map ...

  8. MyBatis学习之简单增删改查操作、MyBatis存储过程、MyBatis分页、MyBatis一对一、MyBatis一对多

    一.用到的实体类如下: Student.java package com.company.entity; import java.io.Serializable; import java.util.D ...

  9. 最简单的mybatis增删改查样例

    最简单的mybatis增删改查样例 Book.java package com.bookstore.app; import java.io.Serializable; public class Boo ...

随机推荐

  1. notepad快捷使用

    1.快捷键 参考:https://www.php.cn/tool/notepad/428638.html notepad++是经常使用的一款编辑器软件,在编辑特殊文本的时候(html,java...) ...

  2. Linux就该这么学28期——Day02 2.1-2.3

    本文记录必须掌握的Linux命令,部分内容引用自https://www.linuxprobe.com/basic-learning-02.html 工作中可使用https://www.linuxcoo ...

  3. RHSA-2017:1842-重要: 内核 安全和BUG修复更新(需要重启、存在EXP、本地提权、代码执行)

    [root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) 修复命令: 使用root账号登陆She ...

  4. OpenSSL加密系统简介

    加密基本原理 OpenSSL移植到arm开发板参考  http://blog.chinaunix.net/uid-27717694-id-3530600.html 1.公钥和私钥: 公钥和私钥就是俗称 ...

  5. win10简单方法安装杜比v4音效!win10 1909适用!

    先下载这个! 链接: https://pan.baidu.com/s/1zAOOf-1aCJsjBgy36SiGWA 密码: s9n7 这个是杜比V4文件,257MB大小,适用32位64位系统!下 载 ...

  6. linux 虚拟机下 安装redis

    虚拟机安装linux,打开,挂起就好: 使用ssh连接,这里使用的是Moba Xterm 可以ssh 可以ftp  满足你的日常开发所需,开发必备.每个人都有自己顺手的工具,你喜欢就好 虚拟机挂一边就 ...

  7. 手工实现docker的vxlan

    前几天了解了一下docker overlay的原理,然后一直想验证一下自己的理解是否正确,今天模仿docker手工搭建了一个overlay网络.先上拓扑图,其实和上次画的基本一样.我下面提到的另一台机 ...

  8. 机器分配----线性dp难题(对于我来说)

    题目: 总公司拥有高效设备M台, 准备分给下属的N个分公司.各分公司若获得这些设备,可以为国家提供一定的盈利.问:如何分配这M台设备才能使国家得到的盈利最大?求出最大盈利值.其中M <= 15, ...

  9. 【应用服务 App Service】当使用EntityFrameWorkCore访问Sql Server数据库时,在Azure App Service会出现Cannot create a DbSet for ** because this type is not included in the model for the context的错误

    问题情形 使用EF Core访问数据库,在本地运行正常,发布到App Service后,偶尔出现了Cannot create a DbSet for ** because this type is n ...

  10. 【Azure DevOps系列】Azure DevOps使用Docker将.NET应用程序部署在云服务器

    Docker持续集成 本章我们要实现的是通过我们往代码仓库push代码后,我们将每次的push进行一次docker自动化打包发布到docker hub中,发布到之后我将进行部署环节,我们将通过ssh方 ...