SpringBoot与Mybatis整合

Posted gdpuzxs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot与Mybatis整合相关的知识,希望对你有一定的参考价值。

  (1)pom.xml中引入jar包,如下:这里不需要引入spring-boot-starter-jdbc依赖,因为mybatis-spring-boot-starter中已经包含了此依赖

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>

  (2)在application.properties配置数据库连接信息,如下:

#mysql数据库配置
spring.datasource.url=jdbc:mysql://172.31.19.20:3306/springboot
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

  (3)新建User实体类,如下:

package springboot.domain;

public class User {

    private String id;
    private String name;
    private String age;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
    }
    
}

  (4)新建UserMapper类

package springboot.dao;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import springboot.domain.User;

@Mapper
public interface UserMapper {

    @Insert("insert into user(name,age) values(#{name},#{age})")
    int addUser(@Param("name")String name,@Param("age")String age);
    
    @Select("select * from user where id =#{id}")
    User findById(@Param("id") String id);
    
    @Update("update user set name=#{name} where id=#{id}")
    void updataById(@Param("id")String id,@Param("name")String name);
    
    @Delete("delete from user where id=#{id}")
    void deleteById(@Param("id")String id);
    
}

  这里使用mybatis注解版,相比以前少了一个与dao层相对应的xml文件,还是挺方便的。

  (5)Service层代码如下:

package springboot.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import springboot.dao.UserMapper;
import springboot.domain.User;


@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;
    
    
    public User findById(String id){
        return userMapper.findById(id);
    }
    
    public int addUser(String name,String age){
        return userMapper.addUser(name,age);
    }
    
    public void updataById(String id,String name){
         userMapper.updataById(id,name);
    }
    
    public void deleteById(String id){
        userMapper.deleteById(id);
    }
}

  (6)Controller层,代码如下:

package springboot.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springboot.domain.User;
import springboot.service.UserService;

@RestController
public class HelloController {
    
    @Autowired
    private UserService userService;
    
    @RequestMapping("/adduser")
    public int addUser(@RequestParam("name")String name,@RequestParam("age")String age){
        return userService.addUser(name, age);
    }
    @RequestMapping("/findUser")
    public User findUser(@RequestParam("id") String id){
        return userService.findById(id);
    }
    @RequestMapping("/updataById")
    public String updataById(@RequestParam("id") String id,@RequestParam("name") String name){
        try {
            userService.updataById(id, name);
        } catch (Exception e) {
            return "error";
        }
        return "success";
    }
    
    @RequestMapping("/deleteById")
    public String deleteById(@RequestParam("id") String id){
        try {
            userService.deleteById(id);
        } catch (Exception e) {
            return "error";
        }
        return "success";
    }
}

以上是关于SpringBoot与Mybatis整合的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot2 系列教程 | SpringBoot 整合 Mybatis

SpringBoot 整合MyBatis案例详解

SpringBoot整合mybatis快速入门

Springboot系列之Springboot与Mybatis整合

SpringBoot与Mybatis整合实例详解

springboot整合mybatis,redis,代码