SpringBoot结合Mybatis

原创
小哥 3年前 (2022-11-07) 阅读数 74 #C#
文章标签 .net

SpringBoot整合Mybatis

1.基础工作

1,创建项目springboot项目,检查web+JDBC+MySQL驱动



2、导入 mybatis-springboot-starter 依赖

  
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.2
        

这里应该注意的是,版本对应。

3、编写 application.properties 配置文件

#数据库配置 根据自己的数据库username和password修改
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

#整合mybatis
#给pojo实体类别名
mybatis.type-aliases-package=com.kuang.pojo  
#接口配置文件的位置。 我的界面配置文件是UserMapper.xml 如下图所示
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml 

二、springboot整合mybatis案例

上述基础工作做得很好,事实上,基本框架已经建立,直接开始案例操作就足够了。

项目结构

1、编写pojo包下的实体类 User

package com.kuang.pojo;

public class User {

    private int id;
    private String name;
    public String pwd;

    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 getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public User(int id, String name, String pwd) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }

    public User() {
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name=" + name +  +
                ", pwd=" + pwd +  +
                };
    }
}

2,写入接口UserMapper

import com.kuang.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;

 /*添加了此注释 就表示了 这是一个Mybatis的mapper类
等同于先前使用的spring整合mybatis接口 也可以使用@MapperScan("com.kuang.mapper")*/
@Mapper

/**@Component 你也可以使用这个 万能的*/
@Repository
public interface UserMapper {

    List queryUserList();

    User queryUserById(int id);

    int addUser(User user);

    int updateUser(User user);

    int deleteUser(int id);

}

3,写入接口实现配置文件 UserMapper.xml




    

    

    
    insert into mybatis.user(id,name,pwd)
    values(#{id},#{name},#{pwd});

    

    
        update mybatis.user
        set name=#{name},pwd=#{pwd}
        where id=#{id};
    

    
     delete
     from mybatis.user
     where id=#{id};

    

配置文件 UserMapper.xml 的位置
4、编写contoller中 UserController

import com.kuang.mapper.UserMapper;
import com.kuang.pojo.User;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import sun.net.util.IPAddressUtil;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/queryUserList")
    public List queryUserList(){
        List userList = userMapper.queryUserList();

        for(User user:userList){
            System.out.println(user);
        }

        return userList;
    }

    @GetMapping("/queryUserById")
    public User queryUserById(){

        User user = userMapper.queryUserById(1);
        return user;

    }

    @GetMapping("/addUser")
    public String addUser(){
        userMapper.addUser(new User(21,"阿毛","123456"));
        return "添加成功";

    }

    @GetMapping("/updateUser")
    public String updateUser(){
        userMapper.updateUser(new User(1,"秃驴","123465"));
        return "更改成功";
    }

    @GetMapping("/deleteUser")
    public String deleteUser(){
        userMapper.deleteUser(1);
        return "删除成功";

    }

}

至此,springboot整合mybatis 添加、删除、修改和检查的操作全部结束。

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除

热门