Springboot 集成 Mybatis 访问mysql

                                         Springboot  集成 Mybatis 访问 mysql

  • IDE 我们是用的是             IntelliJ IDEA 2019.2
  • 数据库我们使用的是         mysql

 

1 创建新的 工程

Springboot 集成 Mybatis 访问mysql_第1张图片

2 选择对应的依赖

Springboot 集成 Mybatis 访问mysql_第2张图片

 

刚刚创建好的目录是这样的

Springboot 集成 Mybatis 访问mysql_第3张图片

 

这里是 数据库 中的 已经有的数据数据

Springboot 集成 Mybatis 访问mysql_第4张图片

 

现在我们要创建 四个文件夹 分别是

  • controller                 对外 数据访问提供的  web 入口,里面的脚本 以 controller 结尾
  • entity                       放的是数据库对应的表结构
  • mapper                   是一个接口类   提供内部 访问 分装好 胆未实现
  • service                    暴露给项目 接口,提供具体数据库的 接口

 

 

我们在 entity  文件夹中床架一个 叫 User 的Class   他的字段要和 数据库中的字段对应上

Springboot 集成 Mybatis 访问mysql_第5张图片

package com.example.mybatis.entity;

import java.util.Date;

public class User {

    private Long user_id;
    private String user_name;
    private Integer user_age;

    public Long getUser_id() {
        return user_id;
    }

    public void setUser_id(Long user_id) {
        this.user_id = user_id;
    }

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public Integer getUser_age() {
        return user_age;
    }

    public void setUser_age(Integer user_age) {
        this.user_age = user_age;
    }

    @Override
    public String toString() {
        return "User{" +
                "user_id=" + user_id +
                ", user_name='" + user_name + '\'' +
                ", user_age=" + user_age +
                '}';
    }
}

 

我们在 mapper  文件夹中床架一个 叫 UserMapper  的 Class   这里主要是 对数据库 具体的操作

这里面有对应的 怎删改查

Springboot 集成 Mybatis 访问mysql_第6张图片

package com.example.mybatis.mapper;

import com.example.mybatis.entity.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import java.util.List;

@Component(value = "UserMapper")      //这个地方有个注解 不要忘记了 等下回用到
public interface UserMapper {

    @Select("SELECT * FROM tbl_user")
    List getUsers();

    @Select("SELECT * FROM tbl_user WHERE id =#{user_id}")
    User  getOne(Long user_id);

    @Update("UPDATE tlb_user SET user_name =#{user_name}, user_age=#{user_age} WHERE id = #{user_id} )")
    void updateUser(User user);

    @Delete("DELETE FROM tlb_user WHERE id=#{user_id}")
    void deleteUser(Long user_id);

}

 

我们在 service  文件夹中床架一个 叫 UserService  的 Class   这里分装好   提供给 controller 使用的 接口

Springboot 集成 Mybatis 访问mysql_第7张图片

package com.example.mybatis.service;

import com.example.mybatis.entity.User;
import com.example.mybatis.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    UserMapper userMapper;

    public List getUsers(){
        return  userMapper.getUsers();
    }

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


    public void updateUser(User user){
        userMapper.updateUser(user);
    }

    public void deleteUser(Long userId){
        userMapper.deleteUser(userId);
    }
}

我们在 controller 文件夹中床架一个 叫 UserController  的 Class   对外界提供通信的

Springboot 集成 Mybatis 访问mysql_第8张图片

package com.example.mybatis.controller;

import com.example.mybatis.entity.User;
import com.example.mybatis.service.UserService;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    private  static  final Log log = LogFactory.getLog(UserController.class);

    @Autowired
    UserService userService;

    @GetMapping
    public List getAllUser(){
         return  userService.getUsers();
    }

}

在这里 我们的 四个 文件夹 就处理完了,

 

接着我们在对我们的 启动类 添加一个 注解,然他关联上我们的 mapper文件夹

Springboot 集成 Mybatis 访问mysql_第9张图片

 

最后 我们在提供  上 数据库的访问 账户密码 等等   application.properties

Springboot 集成 Mybatis 访问mysql_第10张图片

server.port=8081

#日志配置
logging.file= target/app.log
logging.level.root=WARN
logging.level.com.restfulapi.octopus=DEBUG

#集成mysql数据库的配置
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.0.170:3306/api_super?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=admin
spring.datasource.password=adminpw

 

最后再贴上 pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.9.RELEASE
         
    
    com.example
    mybatis
    0.0.1-SNAPSHOT
    mybatis
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-data-jdbc
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.0
        

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

        
            mysql
            mysql-connector-java
        



    

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


 

通过工具测试 发现我们的 controller 方法 可以被调用并且 返回数据库中的数据了。

 

Springboot 集成 Mybatis 访问mysql_第11张图片

下载地址

你可能感兴趣的:(Spring,Boot,RESTful,API,《SpringBoot,入门到精通,第三讲,静态资源》)