基于SpringBoot集合Mybatis的增删改查实现,通过postman测试(1)

本篇介绍基于SpringBoot和Mybatis+mysql+maven的简单实现增删改查的案例,详细介绍如下,

代码地址附上:https://git.lug.ustc.edu.cn/nantian_javaBackground/springboot_mybatis/tree/SpringBoot_Mybatis

1---项目包介绍:

2---pom文件依赖设计:

      

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.sang</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- alibaba的druid数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.11</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2---数据库相关配置文件application.properties设计:

#mysql数据库连接
spring.datasource.url=jdbc:mysql://localhost:3306/taotao?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456

spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
mybatis.mapper-locations= classpath:mapper/*.xml

3---数据库设计,使用mysql数据库创建的简单数据表  用来测试数据:

     

4---实体类设计:User类

package com.sang.entity;

/**
 * Created by 
 * on 2019/11/13 16:56
 */
public class User {
    private int id;
    private String name;
    private String password;
    private String number;

    public User() {
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", number='" + number + '\'' +
                '}';
    }

    public User(int id, String name, String password, String number) {
        this.id = id;
        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;
    }
}

   5---service层设计:UserService类:

package com.sang.service;

import com.sang.entity.User;
import com.sang.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by 
 * on 2019/11/13 17:04
 */
@Service
public class UserService {

    @Autowired(required = false)
    private UserMapper userMapper;

    /**
     * 通过用心姓名查找用户
     * @param name
     * @return
     */
    public List<User> findUserByName(String name) {
        return userMapper.findUserByName(name);
    }

    /**
     * 通过用户信息插入用户   并返回插入的用户
     * @param user
     * @return
     */
    public User insertUser(User user){
        userMapper.insertUser(user);
        return user;
    }


    /**
     * 查询所有的用户
     * @return
     */
    public List<User> ListUser(){
        return userMapper.ListUser();
    }

    /**
     * 借助用户信息更新用户表
     * @param user
     * @return
     */
    public int Update(User user){
        return userMapper.Update(user);
    }

    /**
     * 借助id删除用户信息
     * @param id
     * @return
     */
    public int delete(int id){
        return userMapper.delete(id);
    }

}

6---mapper接口设计:UserMapper接口:

package com.sang.mapper;

import com.sang.entity.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * Created by 
 * on 2019/11/13 16:59
 */
@Mapper
public interface UserMapper {

    //通过姓名查找用户
    List<User> findUserByName(String name);

    //查询所有的用户信息
    public List<User> ListUser();

    //插入一条用户信心  返回数字表示是否成功
    public int insertUser(User user);

    //通过id删除用户信息
    public int delete(int id);

    //更新用户信息
    public int Update(User user);

}

     7---controller层设计:CRUD类:

package com.sang.controller;

import com.sang.entity.User;
import com.sang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by 
 * on 2019/11/13 17:18
 */
@RestController
@RequestMapping(value = "/CRUD",method = {RequestMethod.GET,RequestMethod.POST})
public class CRUD {

    @Autowired
    private UserService userService;

    /**
     * 通过员工id 删除员工------------GET----------------通过
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public String delete(int id){
        int result = userService.delete(id);
        if(result>=1){
            return "删除成功";
        }else{
            return "删除失败!";
        }
    }

    /**
     * 通过员工信息更新员工________________POST-----------通过
     * @param user
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(User user){
        int result=userService.Update(user);
        if(result>=1){
            return "修改成功!";
        }else{
            return "修改失败!";
        }
    }

    /**
     * 通过员工信息插入员工---------------POST----------------通过
     * @param user
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public User insert(User user) {
        return userService.insertUser(user);
    }

    /**
     * 返回用户的所有信息----------------------------------通过
     * @return
     */
    @RequestMapping("/ListUser")
    @ResponseBody
    public List<User> ListUser(){
        return userService.ListUser();
    }

    /**
     * 通过员工姓名查找员工
     * @param name
     * @return
     */
    @RequestMapping("/ListUserByname")
    @ResponseBody
    public List<User> ListUserByname(String name){
        return userService.findUserByName(name);
    }

}

8---资源包中UserMapper.xml文件设计:

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE mapper PUBLIC
        "-//mybatis.org//DTD com.example.Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sang.mapper.UserMapper">
    <resultMap id="result" type="com.sang.entity.User">
        <result property="name" column="name" />
        <result property="password" column="password" />
        <result property="number" column="number"/>
    </resultMap>

    <select id="ListUser" resultMap="result">
		SELECT * FROM user
	</select>

    <select id="findUserByName" resultMap="result">
		SELECT * FROM user where name=#{name}
	</select>

    <insert id="insertUser" parameterType="com.sang.entity.User"
            keyProperty="id" useGeneratedKeys="true">
		INSERT INTO user
		(
		id,name,password,number
		)
		VALUES (
		#{id},
		#{name, jdbcType=VARCHAR},
		#{password, jdbcType=VARCHAR},
		#{number}
		)
	</insert>

    <delete id="delete" parameterType="int">
		delete from user where id=#{id}
	</delete>

    <update id="Update" parameterType="com.sang.entity.User">
	update user set user.name=#{name},user.password=#{password},user.number=#{number} where user.id=#{id}
	</update>
</mapper>

 

9---SpringBoot启动类设计:DemoApplication类:

package com.sang;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

10--postman测试:

查询所有:http://localhost:8080/CRUD/ListUser

11----删除员工:借助id删除员工

12---插入员工

13:---通过姓名查找员工用户信息:

本地测得:

postman测得:

14---修改员工:借助员工id信息锁定员工  在进行对应信息的修改;

 

 

  • 7
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值