引入依赖

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

使用MyBatis Generator

添加Maven插件

<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
</plugin>

这里需要加上mysql驱动的依赖,我第一次没有加,然后自动生成就一直报错。

添加配置文件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> <context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator> <!--数据库连接信息-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ssmweb?useSSL=false&amp;serverTimezone=UTC"
userId="root"
password="123456">
</jdbcConnection> <!--是否应强制对DECIMAL和NUMERIC字段使用java.math.BigDecimal-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver> <!--配置自动生成Model(entity)-->
<javaModelGenerator targetPackage="com.example.demo.model" targetProject="E:\intellij_idea\liull\springbootmybatisdemo\src\main\java">
<!--<property name="enableSubPackages" value="true" />-->
<property name="trimStrings" value="true" />
</javaModelGenerator> <!--配置自动生成Mapper映射文件-->
<sqlMapGenerator targetPackage="mapper" targetProject="E:\intellij_idea\liull\springbootmybatisdemo\src\main\resources"> </sqlMapGenerator> <!--配置自动生成dao-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.dao" targetProject="E:\intellij_idea\liull\springbootmybatisdemo\src\main\java"> </javaClientGenerator> <!--配置表信息及类信息-->
<table schema="ssmweb" tableName="emp" domainObjectName="Emp" >
<generatedKey column="emp_id" sqlStatement="MySql" identity="true" />
</table> </context>
</generatorConfiguration>

这里注意:在上面pom.xml中配置的mybatis-generator-maven-plugin插件,是默认加载/src/main/resources/目录下名为generatorConfig.xml。

运行插件

双击插件即可运行。

运行后目录:

正式整合mybatis

配置application.yml

spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssmweb?useSSL=false&serverTimezone=UTC
username: root
password: 123456 #mybatis配置
mybatis:
mapper-locations: classpath:mapper/*.xml

需要配置mapper映射文件位置,不然映射类无法找到映射文件位置,运行会报错。

扫描映射类

在springboot启动类上添加@MapperScan注解

@MapperScan(basePackages = {"com.example.demo.dao"})
@SpringBootApplication
public class SpringbootmybatisdemoApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootmybatisdemoApplication.class, args);
}
}

测试

service:

@Service
public class EmpService {
@Autowired
private EmpMapper empMapper; public List<Emp> getAll(){
return empMapper.selectByExample(null);
}
}

controller:

@RestController
public class EmpController { @Autowired
private EmpService empService; @GetMapping("/emps")
public List<Emp> getAll(){
return empService.getAll();
} }

结果:

springboot整合mybatis(使用MyBatis Generator)的更多相关文章

  1. SpringBoot整合Redis、mybatis实战,封装RedisUtils工具类,redis缓存mybatis数据 附源码

    创建SpringBoot项目 在线创建方式 网址:https://start.spring.io/ 然后创建Controller.Mapper.Service包 SpringBoot整合Redis 引 ...

  2. springboot整合druid、mybatis

    目的: 1.springboot配置数据库连接池druid 测试druid中url监控 2.springboot整合mybatis 测试查删案例 3.springboot整合pagehelper sp ...

  3. SpringBoot 整合jdbc和mybatis

    摘要 该文章主要为记录如何在SpringBoot项目中整合JDBC和MyBatis,在整合中我会使用简单的用法和测试用例,毕竟该文章目的是为了整合,而不是教大家如何去使用.希望大家多多包涵. 通用配置 ...

  4. 003 SpringBoot整合SpringMVC、Mybatis 案例

    1.不使用骨架创建一个maven工程 2.修改POM.xml文件 <?xml version="1.0" encoding="UTF-8"?> &l ...

  5. Springboot整合Ehcache 解决Mybatis二级缓存数据脏读 -详细

    前面有写了一篇关于这个,但是这几天又改进了一点,就单独一篇在详细说明一下 配置 application.properties ,启用Ehcache # Ehcache缓存 spring.cache.t ...

  6. springboot整合jpa和mybatis实现主从复制

    百度多方参考终于配出我自己的了,以下仅供参考 参考https://www.cnblogs.com/cjsblog/p/9712457.html 代码 首先数据源配置 spring.datasource ...

  7. SpringBoot:整合Druid、MyBatis

    目录 简介 JDBC 导入依赖 连接数据库 CRUD操作 自定义数据源 DruidDataSource Druid 简介 配置数据源 配置 Druid 数据源监控 配置 Druid web 监控 fi ...

  8. SpringBoot整合Redis、ApachSolr和SpringSession

    SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...

  9. springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)

    这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...

随机推荐

  1. Pandora 生成 Token

    生成 token 打数据到仓库 通过 api 签名工具实现 最后通过curl -XPOST -H "Content-Type: application/json" -H " ...

  2. java基础 第六章课后习题

    1.说明循环结构中break语句和continue语句的区别. 在循环结构中 break语句 是结束程序运行. continue语句是结束本句程序  不是结束程序. 2.使用for循环结构实现,从键盘 ...

  3. 一个简单的分布式session框架

    该代码只是用来学习原理的,有很多不完善之处. 代码:  git@github.com:sicw/EasySpringSession.git 一. 整体设置 1. 实现Filter,封装新的reques ...

  4. vue发送请求----vue-resource

    使用插件vue-resource 官方提供的接口,在vue官网找不到 但在github中可以找到 安装:cnpm install vue-resource --save 第一步:注意要加--save, ...

  5. HANA SQL备忘录

    1.改变元素列类型 ALTER TABLE <TABLE_NAME> ALTER (<COLUMN_NAME> <COLUMN_TYPE>);

  6. Python_tkinter(4)_上传文件

    1.上传单个文件 import tkinter as tk from tkinter import filedialog def upload_file(): selectFile = tk.file ...

  7. JMeter 压测基础(四)——Java工程测试

    JMeter Java工程测试 1.eclipse中新建工程 2.导入需要的jar包 从JMeter安装目录lib/ext中两个jar包buildPath到Java project中,分别是“Apac ...

  8. jmeter 之 if controller

    jmeter版本5.0.下面是jmeter5.0的if逻辑控制器的截图 标红字体的意思大概是,如果勾选了 下面的 interpret condition as variable expression ...

  9. JavaScript中的this所引用的对象和如何改变这个引用

    this是函数内部的一个特殊对象,它引用的是函数执行环境对象.也就是运行是基于函数的执行环境绑定. 1.在网页全局作用域中调用函数时,this引用window var color='black'; f ...

  10. 解决配置Windows Update失败问题

    大家都清楚电脑总是需要更新一些补丁,不过,很多系统用户发现更新了补丁之后,开机会出现windows update更新失败的情况,提示“配置Windows Update失败,还原更改,请勿关闭计算机”信 ...