1. 程式人生 > >SpringBoot與Mybatis整合

SpringBoot與Mybatis整合

ror toolbar public prop drive ping img select 技術

(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整合