ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

springboot+mybatis实现数据增删改查

2020-02-04 11:37:38  阅读:334  来源: 互联网

标签:springboot void 改查 UserEntity id mybatis import password public


springboot+mybatis实现数据增删改查

上一篇讲到使用idea创建springboot项目 那么下面我们来讲讲使用mybatis实现增删改查(curd)吧。

1、数据库创建

2、pom.xml文件 加入依赖mysql 等等

 <!--mysql依赖-->
        <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>
            <scope>runtime</scope>
        </dependency>

3、在配置文件(application.yml)中加入数据库的配置  注意用户名、密码、库名、包名路径改成自己的

server:
  port: 8111
  servlet:
    context-path: /

spring:
  datasource:
    username: root
    password: '*******'
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/lzw?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false

mybatis:
  mapper-locations: classpath:./mapper/*Mapper.xml
  type-aliases-package: com.example.bys.entity

 4、在src文件夹下新建UserEntity类  与数据库的字段对应

package com.example.bys.entity;

public class UserEntity {
    private int userid;
    private String username;
    private String password;
    private String email;

    public UserEntity(String username, String password, String email) {
        this.userid = userid;
        this.username = username;
        this.password = password;
        this.email = email;
    }

    public UserEntity() {
    }

    public int getUserid() {
        return userid;
    }

    public void setUserid(int userid) {
        this.userid = userid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

5、新建mapper文件夹并新建UserMapper接口

package com.example.bys.mapper;

import com.example.bys.entity.UserEntity;
import org.apache.catalina.User;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserMapper {
    /**
     *
     * @return 查询所有用户
     */
    List<UserEntity> selectAllUser();

    /**
     *
     * @param id 用户id
     * @return 查询单个用户
     */
    UserEntity selectOneUser(int id);

    /**
     *
     * @param userEntity 插入用户
     * @return 插入的用户
     */
    void insertUser(UserEntity userEntity);

    /**
     *
     * @param id 删除用户的id
     */
    void deleteUser(int id);

    /**
     *
     * @param userEntity 更新用户的数据
     */
    void updateUser(UserEntity userEntity);
}

6、新建service文件夹并新建UserServiceImpl类 去实现上面的mapper接口

package com.example.bys.service;


import com.example.bys.entity.UserEntity;
import com.example.bys.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl {

    @Autowired
    UserMapper userMapper;

    public List<UserEntity> selectAllUser() {
        return userMapper.selectAllUser();
    }

    public UserEntity selectOneUser(int id) {
        return userMapper.selectOneUser(id);
    }
    public void insertUser(UserEntity userEntity){
        userMapper.insertUser(userEntity);
    }
    public void deleteUser(int id){
        userMapper.deleteUser(id);
    }
    public void updateUser(UserEntity userEntity){
        userMapper.updateUser(userEntity);
    }


}

7、在controller文件夹下新建UserController类 在里面写对应的逻辑

package com.example.bys.controller;

import com.example.bys.entity.UserEntity;
import com.example.bys.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServiceImpl userService;

    @RequestMapping("/selectAllUser")
    public List<UserEntity> selectAllUser(){
        return userService.selectAllUser();
    }

    @RequestMapping("/selectOneUser")
    public UserEntity selectOneUser(int id){
        return userService.selectOneUser(id);
    }

    @PostMapping("/insertUser")
    public void insertUser(@RequestBody UserEntity userEntity){
         userService.insertUser(userEntity);
         System.out.println("insert成功");
    }

    @RequestMapping("/deleteUser")
    public void deleteUser(int id){
        userService.deleteUser(id);
        System.out.println("delete成功");
    }

    @PostMapping("/updateUser")
    public void updateUser(@RequestBody UserEntity userEntity){
        userService.updateUser(userEntity);
        System.out.println("update成功");
    }




}

8、在resource文件夹下新建mapper文件夹并新建映射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.example.bys.mapper.UserMapper">


    <select id="selectAllUser" resultType="com.example.bys.entity.UserEntity">
        select * from user
    </select>

    <select id="selectOneUser" resultType="com.example.bys.entity.UserEntity">
        select * from user where userid = #{id}
    </select>

    <insert id="insertUser" parameterType="com.example.bys.entity.UserEntity">
        insert into user (username,password,email) values (#{username},#{password},#{email})
    </insert>

    <delete id="deleteUser" parameterType="java.lang.Integer">
        delete from user where userid = #{id}
    </delete>

    <update id="updateUser" parameterType="com.example.bys.entity.UserEntity">
        update user set username=#{username},password=#{password},email=#{email} where userid=#{userid}
    </update>


</mapper>

9、最后在启动类中加上一句扫描mapper包的位置 然后直接run起来就好了 

@MapperScan("com.example.bys.mapper")  //扫描mapper包

 

10、好啦到这里已经基本完成啦。。。下面用postman测试工具来测试(蛮好用的工具大家可以下载一个来玩一下)

测试查找全部用户 注意路径和Usercontroller里面的要一致

测试查找单个用户  

测试新增用户

测试删除用户

测试修改用户

工程代码已上传至Github 大家自行下载吧。。。。。。。

https://github.com/Goodnamelzw/springboot-mybatis

 

代码敌敌畏 发布了87 篇原创文章 · 获赞 58 · 访问量 8万+ 私信 关注

标签:springboot,void,改查,UserEntity,id,mybatis,import,password,public
来源: https://blog.csdn.net/sm16111/article/details/104159281

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有