SpringBoot 集成mybatis完整流程

SpringBoot 集成mybatis,使用maven自动生成工具,生成数据库表对应Java类,Mapper接口及Mapper.xml

 

1.Pom.xml中配置依赖和mybatis自动生成工具,生成数据库表对应Java类,Mapper接口及Mapper.xml

        
            mysql
            mysql-connector-java
            runtime
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        
 
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.6
                
                   src/main/resources/mybatis-generator/generateMapper.xml
                   
                   true
                   true
                
 

 

2.配置Pom.xml编译配置(不是必须,idea中xml文件不进行编译配置,target目录中找不到.xml文件,导致报错找不到某个sql,使用eclipse可以跳过此步) ,build标签下添加如图代码:

       
            
                src/main/java
                
                    **/*.xml
                
            
        

 

3.在springboot核心配置文件中配置mapper.xml文件的路径和数据源信息

server.port=9090
server.servlet.context-path=/springboot_mybatis_demo

mybatis.mapper-locations=classpath:com/mybatis/springboot/mapper/*.xml

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springbootdb?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root

 

4.配置Mybatis 生成文件的配置文件





    
    
    
    
        
        
            
            
        
        
        
        
        
            
            
        
        
        
            
        
        
        
            
        


        
        

tb_student表结构如下图,可视化工具注册比较麻烦,凑合看下。

SpringBoot 集成mybatis完整流程_第1张图片

 

5.双击如图所示位置:

SpringBoot 集成mybatis完整流程_第2张图片

 我的报了个错:(无报错可跳过该部分)

 The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure   either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you     want to utilize time zone support.

解决方法:给time_zone设置值

SpringBoot 集成mybatis完整流程_第3张图片

再次双击生成:

SpringBoot 集成mybatis完整流程_第4张图片

SpringBoot 集成mybatis完整流程_第5张图片

6.在StudentMapper中添加findAll




  
    
    
    
  

  

 7.给生成的Mapper接口文件添加注解@Mapper

@Mapper
public interface StudentMapper {
    int insert(Student record);

    int insertSelective(Student record);

    List findAll();
}

8.定义service 接口与实现类

public interface StudentService {
    List findAll();
}
package com.mybatis.springboot.service;

import com.mybatis.springboot.mapper.StudentMapper;
import com.mybatis.springboot.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper mapper;

    @Override
    public List findAll() {
        return mapper.findAll();
    }

}

9.定义controller

package com.mybatis.springboot.controller;

import com.mybatis.springboot.service.StudentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ListController {
    @Autowired
    private StudentServiceImpl service;
    @GetMapping("/list/findAll")
    public Object findStudents() {
        return service.findAll();
    }
}

10.启动springboot,访问浏览器

SpringBoot 集成mybatis完整流程_第6张图片

你可能感兴趣的:(随笔)