SpringBoot集成Mybatis

SpringBoot集成Mybatis

1.pom.xml引入依赖
引入依赖
2.数据库建表(id设为自动递增)
user表
插入一条记录
插入数据

3.数据库配置
数据库配置
4.建包和类
包和类
5.Hello.java

package com.example.demo.Controller;

import com.example.demo.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Hello{

    @Autowired
    private UserService userService;

    @RequestMapping("/getUserInfo")
    public Object getUserById(Integer id){
        return userService.getUserById(id).toString();
    }

    @RequestMapping("/insertUserInfo")
    public Object insertUser(String username,String sex){
        userService.insertUser(username,sex);
        return "success";
    }

}

6.User.java

package com.example.demo.Entity;

public class User {
    private int id;
    private String username;
    private String sex;

    public void setId(int id){
        this.id = id;
    }

    public int getId() {
        return id;
    }

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

    public String getUsername(){
        return username;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getSex(){
        return sex;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

7.UserMapper.java

package com.example.demo.Mapper;

import com.example.demo.Entity.User;
import org.apache.ibatis.annotations.*;

@Mapper
public interface UserMapper {

    @Select("select * from user where id = #{id}")
    User getUserById(@Param("id") Integer id);

    @Insert("insert into user values(NULL,#{username},#{sex})")
    void insertUser(@Param("username") String username, @Param("sex") String sex);
}

8.UserService.java

package com.example.demo.Service;

import com.example.demo.Entity.User;

public interface UserService {

    User getUserById(Integer id);

    void insertUser(String username, String sex);
}

9.UserServiceImpl.java

package com.example.demo.Service;

import com.example.demo.Entity.User;
import com.example.demo.Mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserById(Integer id){
        return userMapper.getUserById(id);
    }

    @Override
    public void insertUser(String username, String sex) {
        userMapper.insertUser(username,sex);
    }
}

10.在浏览器中进行测试
(1)查询ID为1的用户信息
查询ID为1的用户信息
(2)插入用户Linda
插入用户Linda
(3)查询ID为2的用户信息
查询ID为2的用户信息
11.SpringBoot ResultFul风格 JSON格式 加特定请求,如Get、Post
Get用来查数据,Post用来插数据

Hello.java改为:

package com.example.demo.Controller;

import com.example.demo.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Hello{

    @Autowired
    private UserService userService;

    @GetMapping("/getUserInfo")
    public Object getUserById(Integer id){
        return userService.getUserById(id).toString();
    }

    @PostMapping("/insertUserInfo")
    public Object insertUser(String username,String sex){
        userService.insertUser(username,sex);
        return "success";
    }

}

注意:PostMapping需要测试工具(如PostMan)来测试,不能在浏览器地址栏中测试:
PostMapping浏览器测试

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值