springboot整合mybatis 有两种方式。
第一种:使用mybatis官方提供的Spring Boot整合包实现。
第二种:使用mybatis-spring整合的方式,也就是我们传统的方式。
在这里采用比较简单的方式第一种
(一),添加依赖,包含(分页,逆向工程)
<!-- mybatis依赖 驱动 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>1.5.9</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
在build里的plugins添加
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<configuration>
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
(二)添加数据源
在生成的application.properties里添加
#数据源
spring.datasource.password=Root123456!
spring.datasource.username=lzk
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/ant
# Mybatis
#mybatis.config-location=classpath:/mybatis.xml
mybatis.type-aliases-package=com.college.food.entity.mybatis
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#逆向工程使用
mapper.plugin = tk.mybatis.mapper.generator.MapperPlugin
mapper.Mapper = tk.mybatis.mapper.common.Mapper
#mapper.mappers=com.college.food.mybatis
mapper.not-empty=false
mapper.identity=MYSQL
(三)逆向工程生成实体和mapper
在resources下新建 generatorConfig.xml ,然后在新建一个mapper的文件夹,java新建实体文件夹和mybatis接口文件夹
generatorConfig.xml如下,数据源写自己的
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--<properties resource="application.properties"/>-->
<!--<context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">-->
<context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<plugin type="${mapper.plugin}">
<property name="mappers" value="${mapper.Mapper}"/>
</plugin>
<commentGenerator>
<property name="suppressAllComments" value="true"/>
<property name="suppressDate" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="org.mariadb.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ant"
userId="lzk"
password="Root123456!">
</jdbcConnection>
<javaModelGenerator targetPackage="entity" targetProject="src/main/java"/>
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
<javaClientGenerator targetPackage="mybatis" targetProject="src/main/java" type="XMLMAPPER" />
<table tableName="t_user" domainObjectName="User" enableSelectByExample="false" enableDeleteByExample="false" enableUpdateByExample="false"><columnRenamingRule searchString="^c_|^d_|^n_" replaceString="" /></table>
</context>
</generatorConfiguration>
(四)生成实体,xml 等文件,双击击maven的 mybatis-generator:generate
生成后会发现原来新建的文件夹下多了几个文件
(五)在启动类添加mapper.xml扫描(不然会报错)
package com.example.lzk.userservice;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@MapperScan(basePackages = "mybatis")
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}
(六)测试
package com.example.lzk.userservice.rest;
import entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mybatis.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import response.AjaxResult;
@Api(value = "User接口" ,description = "User接口")
@RestController
@RequestMapping("/test")
public class TestController {
Logger logger = LoggerFactory.getLogger(TestController.class);
@Autowired
private UserMapper userMapper;
@ApiOperation(value="测试", notes="返回JSON形式数据")
@RequestMapping(value = "/test",method = RequestMethod.POST)
public AjaxResult test(String sn, String mm){
AjaxResult result=new AjaxResult();
User user=userMapper.selectByPrimaryKey((long)1);
try {
result.setData(user);
result.setCode(AjaxResult.RESULT_CODE_0000);
return result;
}catch (Exception e){
logger.info(e.getMessage());
result.setCode(e.getMessage());
return result;
}
}
}
到这就整合完毕了,简单的增删改查里边封装的都有