springboot整合mybatis(使用MyBatis Generator)
引入依赖
<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&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)的更多相关文章
- SpringBoot整合Redis、mybatis实战,封装RedisUtils工具类,redis缓存mybatis数据 附源码
创建SpringBoot项目 在线创建方式 网址:https://start.spring.io/ 然后创建Controller.Mapper.Service包 SpringBoot整合Redis 引 ...
- springboot整合druid、mybatis
目的: 1.springboot配置数据库连接池druid 测试druid中url监控 2.springboot整合mybatis 测试查删案例 3.springboot整合pagehelper sp ...
- SpringBoot 整合jdbc和mybatis
摘要 该文章主要为记录如何在SpringBoot项目中整合JDBC和MyBatis,在整合中我会使用简单的用法和测试用例,毕竟该文章目的是为了整合,而不是教大家如何去使用.希望大家多多包涵. 通用配置 ...
- 003 SpringBoot整合SpringMVC、Mybatis 案例
1.不使用骨架创建一个maven工程 2.修改POM.xml文件 <?xml version="1.0" encoding="UTF-8"?> &l ...
- Springboot整合Ehcache 解决Mybatis二级缓存数据脏读 -详细
前面有写了一篇关于这个,但是这几天又改进了一点,就单独一篇在详细说明一下 配置 application.properties ,启用Ehcache # Ehcache缓存 spring.cache.t ...
- springboot整合jpa和mybatis实现主从复制
百度多方参考终于配出我自己的了,以下仅供参考 参考https://www.cnblogs.com/cjsblog/p/9712457.html 代码 首先数据源配置 spring.datasource ...
- SpringBoot:整合Druid、MyBatis
目录 简介 JDBC 导入依赖 连接数据库 CRUD操作 自定义数据源 DruidDataSource Druid 简介 配置数据源 配置 Druid 数据源监控 配置 Druid web 监控 fi ...
- SpringBoot整合Redis、ApachSolr和SpringSession
SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...
- springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)
这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...
随机推荐
- python2和python3 安装pip冲突问题
系统:win10 问题:安装python2和Python3后 在cmd 中 pip和python命令会冲突 原因:先安装的python3,python3会自动配置path安装pip,pip和pytho ...
- CentOS7搭建SVN服务器
首先,你得有个VPS,我用的是搬瓦工. 安装步骤如下:1.yum install subversion2.查看安装版本 svnserve --version 3.创建SVN版本库目录 mkdir ...
- PageAdmin Cms V2.0 getshell 0day
黑小子在土司公布了“PageAdmin cms getshell Oday”,并给出了一个漏 洞的利用EXP.经过危险漫步在虚拟机里测试,存在漏洞的是PageAdmin Cms的次最薪版本PageAd ...
- CPU漏洞补丁KB4056892 卸载及忽略办法
2018.1.4微软发布了针对intel CPU漏洞的补丁 KB4056892 性能降低不说, 针对一般平民根本没多大意义, 另外还会导致一些软件无法正常使用, (我是使用蓝叠经典版, 启动就会蓝屏) ...
- [Day17]常用API(System、Math、Arrays、BigInteger、BigDecimal)
1.基本类型包装类 1.1 8种基本类型对应的包装类 字节型 byte Byte 短整型 short Short 整型 int Integer 长整型 long Long 字符型 char Chara ...
- [dev][socket] unix domain socket删除socket文件
问题 在使用unix domain socket的时候,bind之后,会在本地路径里 产生一个与path对应的socket文件. 如何正确的在用完socket之后,对其销毁呢? 方案 使用 unlin ...
- 《PHP内核剖析 - FPM》
一:概述 - FPM 定义 - FPM(FastCGI Process Manager)是PHP FastCGI运行模式的一个进程管理器. - FastCGI - Web服务器(如:Nginx. ...
- atmega16功耗实测
晶振32.768K 电压 电流 1.82 10ua 2.00 15ua 2.5 28ua 3.3 ...
- 兼容在安装linux系统过程中不支持非原装的光模块的命令
1 .通过ifconfig -a发现有网卡找不到,并且配置没有问题,那么很可能是光模块有问题2 .dmesg | grep 82599EB ,通过这个命令过滤发现有如下信息: [ 7142.12197 ...
- nginx域名跳转到www下