SpringBoot整合Mybaties
- 创建maven工程
- 整合mybaties
- 逆向代码生成
创建maven工程
·1.通过idea创建maven工程如下图
2.生成的工程如下
以上我们就完成了一个maven工程,接下来我们改造成springboot项目。
这里主要分为三步:添加依赖,增加配置,增加注解
1.添加依赖
<!--添加springboot父依赖--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.4</version></parent><dependencies><!--web依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--test测试依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>
2.添加配置文件application.yml,增加端口号
3.修改启动类.增加@SpringBootApplication注解
和修改主类SpringApplication.run(Main.class,args);如下
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Main {public static void main(String[] args) {SpringApplication.run(Main.class,args);}
}
通过以上三步,恭喜springboot项目搭建完成,运行看下效果
整合mybaties
springboot整合任何中间件都是三步:依赖,配置,注解
1.依赖:
<dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.5</version></dependency><dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>1.3.7</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.21</version></dependency>
这里引入了三个依赖:第一个是springboot整合mybaties依赖,后面两个是逆向工程的相关依赖
2.配置增加如下配置到配置文件,根据需要修改用户名密码和数据库,并且注意mapper-locations: classpath:mappers/*.xml的mappers路径
spring:datasource:url: jdbc:mysql://localhost:3306/course?useSSL=false&serverTimezone=GMTusername: rootpassword: 565
#指定mybatis映射文件的地址
mybatis:mapper-locations: classpath:mappers/*.xml
2.注解
@Mapper,这里先用这个注解,在所有Mapper下的文件都加上该注解如下
逆向代码生成
增加配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfigurationPUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><generatorConfiguration><!-- mysql-connector-java 版本需要与pom中引得版本一致 --><classPathEntry location="E:\repository\mysql\mysql-connector-java\8.0.21\mysql-connector-java-8.0.21.jar" /><context id="DB2Tables" targetRuntime="MyBatis3"><!-- 不加注释,因为默认注释是英文的 --><commentGenerator><property name="addRemarkComments" value="true"/><property name="suppressDate" value="true"/></commentGenerator><!-- 数据库连接信息 --><jdbcConnection driverClass="com.mysql.jdbc.Driver"connectionURL="jdbc:mysql://localhost:3306/course?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC"userId="root"password="luyao520"></jdbcConnection><javaTypeResolver ><property name="forceBigDecimals" value="false" /></javaTypeResolver><!-- targetPackage 文件生成在指定包下,targetProject 路径--><javaModelGenerator targetPackage="edu.hbgy.rjz.entity" targetProject="src/main/java"><property name="enableSubPackages" value="true" /><property name="trimStrings" value="true" /></javaModelGenerator><sqlMapGenerator targetPackage="mappers" targetProject="src/main/resources"><property name="enableSubPackages" value="true" /></sqlMapGenerator><javaClientGenerator type="XMLMAPPER" targetPackage="edu.hbgy.rjz.dao" targetProject="src/main/java"><property name="enableSubPackages" value="true" /></javaClientGenerator><!-- domainObjectName 即生成后entity的名字 --><table tableName = "order" domainObjectName="Order" /></context>
</generatorConfiguration>
<classPathEntry location="E:\repository\mysql\mysql-connector-java\8.0.21\mysql-connector-java-8.0.21.jar" />这个路径要写自己jar包的路径
2.引入插件
在pom.xml中加入
<build><finalName>${artifactId}</finalName><plugins><plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.3.7</version><configuration><!-- generator 工具配置文件的位置 --><configurationFile>src/main/resources/generatorConfig.xml</configurationFile><verbose>true</verbose><overwrite>true</overwrite></configuration><!-- 添加这部分的依赖 --></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
如下所示:
加入之后maven插件如下所示
运行之后会生成代码,目录结构:
再次启动项目确认启动成功:
至此我们的整合工作结束
测试一下,创建一个controller包,并创建类UserController
package edu.hbgy.rjz.controller;import edu.hbgy.rjz.dao.UserMapper;
import edu.hbgy.rjz.entity.User;
import edu.hbgy.rjz.entity.UserExample;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;
import java.util.List;@RestController
public class UserController {@Resourceprivate UserMapper userMapper;@RequestMapping(value="user/del", method = RequestMethod.POST)public Integer del(@RequestBody User user){UserExample example = new UserExample();example.createCriteria().andIdEqualTo(user.getId());Integer count = userMapper.deleteByExample(example);return count;}
}
启动运行: