dayTwo SpringBoot和Mybatis的集成练习

关于参数

  • 1、路径参数
@RequestMapping(value = "/courses/{id}",method = RequestMethod.GET)
public Course getOne(@PathVariable("id") long id){
    return courseService.getOne(id);
}
  • 2、? 传参
  • 3、json对象传参
 @RequestMapping(value = "/user",method = RequestMethod.POST)
    public void addUser(@RequestBody User user){
        userService.insert(user);
    }

实战(进行数据库表的增删改查)

  • pom配置文件


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.3.RELEASE
         
    
    com.springboot
    spring-boot-mybatis
    0.0.1-SNAPSHOT
    spring-boot-mybatis
    SpringBoot Mybatis project for Spring Boot

    
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            mysql
            mysql-connector-java
            5.1.39
        

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        

        
            org.projectlombok
            lombok
            1.18.6
            true
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



  • 连接数据库application.propertis
## 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/db_spring?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=Root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#指定实体类映射的包
mybatis.type-aliases-package=com.springboot.mybatis.entity

  • User类
package com.springboot.mybatis.entity;

import lombok.Data;

@Data
public class User {
    private Long userId;
    private String mobile;
    private String password;
    private String username;
    private String avatar;
}

  • UserMapper类
package com.springboot.mybatis.mapper;

import com.springboot.mybatis.entity.Course;
import com.springboot.mybatis.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {
    @Select("SELECT * FROM t_sys_user ")
    @Results({
            @Result(property = "userId",column = "user_id"),
            @Result(property = "mobile",column = "mobile"),
            @Result(property = "password",column = "password"),
            @Result(property = "username",column = "username"),
            @Result(property = "avatar",column = "avatar")
    })
    List selectAll();

    @Select("SELECT * FROM t_sys_user WHERE user_id = #{userId}")
    @Results({
            @Result(property = "userId",column = "user_id"),
            @Result(property = "mobile",column = "mobile"),
            @Result(property = "password",column = "password"),
            @Result(property = "username",column = "username"),
            @Result(property = "avatar",column = "avatar")
    })
   User getOne(Long userId);

    @Delete("DELETE FROM t_sys_user WHERE user_id =#{userId}")
    void delete(Long userId);

    @Insert("INSERT INTO t_sys_user (user_id,mobile,password,username,avatar) VALUES(#{userId},#{mobile},#{password},#{username},#{avatar})")
    void insert(User user);

    @Update("UPDATE t_sys_user SET mobile=#{mobile},password=#{password} WHERE user_id =#{userId}")
    void update(User user);
}

  • UserService类
package com.springboot.mybatis.service;

import com.springboot.mybatis.entity.User;

import java.util.List;

public interface UserService {
    List selectAll();

    User getOne(Long userId);

    void delete(Long userId);

    void insert(User user);

    void update(User user);
}

  • UserServiceImpl类
package com.springboot.mybatis.service.impl;

import com.springboot.mybatis.entity.User;
import com.springboot.mybatis.mapper.UserMapper;
import com.springboot.mybatis.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public List selectAll() {
        return userMapper.selectAll();
    }

    @Override
    public User getOne(Long userId) {
        return userMapper.getOne(userId);
    }

    @Override
    public void delete(Long userId) {
        userMapper.delete(userId);
    }

    @Override
    public void insert(User user) {
        userMapper.insert(user);
    }

    @Override
    public void update(User user) {
        userMapper.update(user);
    }
}

  • 测试类
package com.springboot.mybatis.service.impl;

import com.springboot.mybatis.entity.Course;
import com.springboot.mybatis.entity.User;
import com.springboot.mybatis.service.UserService;
import com.springboot.mybatis.util.RandomUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

import java.util.List;

import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceImplTest {
    @Resource
    private UserService userService;

    @Test
    public void selectAll() {
        List userList=userService.selectAll();
        userList.forEach(user -> System.out.println(user));
    }

    @Test
    public void getOne() {
        User user = userService.getOne(1L);
        System.out.println(user);

    }

    @Test
    public void delete() {
        userService.delete(5L);
    }

    @Test
    public void insert() {
        User user = new User();
        user.setMobile("18463257485");
        user.setPassword("999");
        user.setUsername("王欣雅");
        user.setAvatar("9.jpg");

        userService.insert(user);
    }

    @Test
    public void update() {
        User user=userService.getOne(2L);
       user.setMobile("111111111");
       user.setPassword("9999");
       userService.update(user);
    }
}
  • 测试类运行成功后在controller运行
  • controller类
package com.springboot.mybatis.controller;

import com.springboot.mybatis.entity.Course;
import com.springboot.mybatis.entity.User;
import com.springboot.mybatis.service.UserService;
import com.springboot.mybatis.util.RandomUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/api")
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping(value = "/users",method = RequestMethod.GET)
    public List selectAll(){
        return userService.selectAll();
    }

    @RequestMapping(value = "/users/{id}",method = RequestMethod.GET)
    public User getOne(@PathVariable("id") long id){
        return userService.getOne(id);
    }

    @RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
    public void delete(@PathVariable("id") long id){
        userService.delete(id);
    }

    @RequestMapping(value = "/user",method = RequestMethod.POST)
    public void addUser(@RequestBody User user){
        userService.insert(user);
    }

    @RequestMapping(value = "/user",method = RequestMethod.PUT)
    public void updateUser(@RequestBody User user){
        userService.update(user);
    }
}

  • 增删改查的运行结果及postman


    Snipaste_2019-03-18_15-18-37.png
Snipaste_2019-03-18_15-20-52.png
查询所有.png
根据id查询.png
删除.png

本篇笔记出处@七夏港_f3ef

你可能感兴趣的:(dayTwo SpringBoot和Mybatis的集成练习)