【开发篇】10分钟快速上手spring boot+mybatis增删改查
发布日期:2021-06-29 15:32:24
浏览次数:3
分类:技术文章
本文共 7171 字,大约阅读时间需要 23 分钟。
【开发篇】10分钟快速上手spring boot+mybatis增删改查
首先来,温故而知新!!!
之前的预备知识:
复习完之前,那么接接下来乘风破浪吧!!!
本次用IDEA带领你快速上手spring boot+mybatis进行用户的增删改查操作。
一、新建spring boot 项目
本次所用的依赖包含spring boot的核心web以及mysql,mybatis依赖包。
其依赖在pom.xml中如下所示:
org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.3 mysql mysql-connector-java
二、项目中配置mysql和mybatis
本次通过application.yml文件来配置信息,将application.properties文件删除,重新新建一个application.yml文件。其内容如下:
spring: profiles: active: dev
之后新建一个application-dev.yml文件,包含项目启动的端口,数据库的连接参数,以及mybatis的配置。
其内容如下:
# 1.项目启动的端口server: port: 8080# 2.数据库连接参数spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/user?serverTimezone=UTC&useSSL=false username: root password: 123# 3.Mybatis的配置mybatis: # mybatis配置文件所在位置 config-location: classpath:mybatis-config.xml # mapper配置文件所在位置 mapper-locations: classpath:mapper/*.xml # 实体类所在的位置 type-aliases-package: com.example.area_mybatis.entity
三、Mybatis配置文件
在resources下新建mybatis-config.xml文件,其主要内容如下
四、代码部分编写
代码包含了
- entity: 实体类的编写
- mapper:数据处理层
- service:业务逻辑层
- controller:控制器
1.entity/User
package com.example.area_mybatis.entity;/** * @author : codingchao * @date : 2020-08-15 12:04 * @Description: **/public class User { private int id; private String name; private String password; private String number; public User(String name, String password, String number) { this.name = name; this.password = password; this.number = number; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; }}
2.mapper/UserMapper
package com.example.area_mybatis.mapper;import com.example.area_mybatis.entity.User;import org.apache.ibatis.annotations.Mapper;import org.springframework.stereotype.Repository;import java.util.List;/** * @author : codingchao * @date : 2020-08-15 12:05 * @Description: **/@Repository@Mapperpublic interface UserMapper { // 通过name查询 ListfindUserByName(String name); // 通过id查询 User findUserById(Integer id); // 列出全部的user信息 public List ListUser(); // 插入user信息 public int insertUser(User user); // 删除user信息 public int delete(int id); // 更新user信息 public int Update(User user);}
3.service/UserService
package com.example.area_mybatis.service;import com.example.area_mybatis.entity.User;import java.util.List;public interface UserService { public ListfindByName(String name); public User findById(Integer id); public List ListUser(); public User insertUser(String name, String number,String password); public int delete(Integer id); public int Update(Integer id,String number);}
4.service/impl/UserServiceImpl
package com.example.area_mybatis.service.impl;import com.example.area_mybatis.entity.User;import com.example.area_mybatis.mapper.UserMapper;import com.example.area_mybatis.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;/** * @author : codingchao * @date : 2020-08-15 12:05 * @Description: **/@Servicepublic class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; public ListfindByName(String name){ return userMapper.findUserByName(name); } public User findById(Integer id){ return userMapper.findUserById(id); } public List ListUser(){ return userMapper.ListUser(); } public User insertUser(String name, String number,String password){ User user = new User(name,number,password); userMapper.insertUser(user); return user; } public int delete(Integer id){ return userMapper.delete(id); } public int Update(Integer id,String number){ User user = userMapper.findUserById(id); user.setNumber(number); return userMapper.Update(user); }}
5.在controller/CRUD
package com.example.area_mybatis.controller;import com.example.area_mybatis.entity.User;import com.example.area_mybatis.service.impl.UserServiceImpl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import java.util.List;/** * @author : codingchao * @date : 2020-08-15 12:05 * @Description: **/@RestController@RequestMapping(value="/crud",method = {RequestMethod.GET,RequestMethod.POST})public class CRUD { @Autowired private UserServiceImpl userService; @GetMapping("/listUser") public ListListUser(){ return userService.ListUser(); } @PostMapping(value="listUserByName") public List ListUserByNAME(@RequestParam("name") String name){ return userService.findByName(name); } @PostMapping(value="delete") public String delete(@RequestParam("id") Integer id){ int result = userService.delete(id); if (result >= 1){ return "删除成功"; }else{ return "删除失败"; } } @PostMapping(value = "insert") public User insert(@RequestParam("name")String name, @RequestParam("number")String number, @RequestParam("password")String password){ return userService.insertUser(name,number,password); } @PostMapping(value = "update") public String update(@RequestParam("id") Integer id, @RequestParam("number")String number){ int result = userService.Update(id,number); if (result >= 1){ return "修改成功"; }else{ return "修改失败"; } }}
五、数据库设计以及UserMapper数据库语句编写
数据库设计如下表所示
之后在resource下新建mapper文件夹,在其下新建UserMapper.xml文件
INSERT INTO user( name,password,number ) VALUES( #{name},#{password},#{number} ) update user where id=#{id} name=#{name}, password=#{password}, number=#{number} delete from user where id=#{id}
六、postman测试
1.测试查询全部信息
http://localhost:8080/crud/listUser
2.通过名字来查询用户
http://localhost:8080/crud/listUserByName?name=A
3.新增用户
http://localhost:8080/crud/insert?name=B&number=202002&password=123456
4.删除用户
http://localhost:8080/crud/delete?id=3
那么,这期就结束了!!!
继续学习去!!!
转载地址:https://codingchaozhang.blog.csdn.net/article/details/108023660 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月12日 13时05分09秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
「第三篇」全国电子设计竞赛,这些你必须知道的比赛细节,文末附上近十年电赛题目下载...
2019-04-29
5G小科普(漫画版,So easy!)
2019-04-29
「第四篇」电赛控制题可以准备一些什么?
2019-04-29
「第六篇」对于电赛,我们应该看重什么?
2019-04-29
树莓派翻车了
2019-04-29
这位电子工程师,你不能错过。
2019-04-29
「重磅猜题之第二篇」2019年大学生电子设计竞赛
2019-04-29
干货分享 JVM 之第 3 篇 —— Java 内存结构相关
2019-04-29
基于 Hystrix 高并发服务限流第 2 篇 —— 服务隔离(线程池隔离、信号量隔离)
2019-04-29
SpringBoot 整合 JWT 实现统一认证
2019-04-29
TypeError: this.getOptions is not a function
2019-04-29
el-table 二维数组合并行
2019-04-29
UR5e机械臂运行一直阻塞在waitForServer
2019-04-29
ROS把pkg1下的某个头文件和源文件生成动态链接库供pkg2调用
2019-04-29
使用urdf_tutorial快速可视化urdf文件
2019-04-29
SQl 数据完整性(随堂博客)
2019-04-29
左连接、右连接、内连接
2019-04-29
MySQL DQL语句基础(随堂博客)
2019-04-29
MySQL基础练习
2019-04-29