本文共 7171 字,大约阅读时间需要 23 分钟。
首先来,温故而知新!!!
之前的预备知识:
复习完之前,那么接接下来乘风破浪吧!!!
本次用IDEA带领你快速上手spring boot+mybatis进行用户的增删改查操作。
本次所用的依赖包含spring boot的核心web以及mysql,mybatis依赖包。
其依赖在pom.xml中如下所示:
org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.3 mysql mysql-connector-java
本次通过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
在resources下新建mybatis-config.xml文件,其主要内容如下
代码包含了
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 "修改失败"; } }}
数据库设计如下表所示
之后在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}
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
那么,这期就结束了!!!
继续学习去!!!
转载地址:http://megbf.baihongyu.com/