SpringBoot整合mybatis-generator-maven-plugin的方法
前言:
SpringBoot版本:2.2.6
mybatis-generator-maven-plugin版本:1.4.0
plugin使用mysql版本:5.1.47
本次主要记录日和使用该插件自动生成pojomapperxml文件(单表)
1创建SpringBoot工程
这里不做具体记录,创建工程往常教程很多,
下面贴出重要文件和注意事项
1.1pom.xml
4.0.0 show.mrkay boot-mybatis-generator 1.0-SNAPSHOT jar org.springframework.boot spring-boot-starter-parent 2.2.6.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test mysql mysql-connector-java com.alibaba druid-spring-boot-starter 1.1.9 org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.0 org.springframework.boot spring-boot-starter-aop org.slf4j jcl-over-slf4j org.slf4j slf4j-log4j12 org.springframework.boot spring-boot-maven-plugin org.mybatis.generator mybatis-generator-maven-plugin 1.4.0 true true mysql mysql-connector-java 5.1.47
1.2application.yml
server: port:8080 spring: datasource: driver-class-name:com.mysql.jdbc.Driver url:jdbc:mysql://localhost:3306/generaltor?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT username:root password:lk0313 druid: db-type:com.alibaba.druid.pool.DruidDataSource #下面不配置就是采用默认配置 #filters:stat #maxActive:20 #initialSize:1 #maxWait:60000 #minIdle:1 #timeBetweenEvictionRunsMillis:60000 #minEvictableIdleTimeMillis:300000 #validationQuery:select'x' #testWhileIdle:true #testOnBorrow:false #testOnReturn:false #poolPreparedStatements:true #maxOpenPreparedStatements:20 mybatis: #该配置项配置了MyBatis配置文件保存路径 mapper-locations:classpath*:show/makey/dao/*.xml #配置扫描对应实体所在包,将mapper的实现类注入容器我们在启动器类上加了@MapperScan注解这里就不需要了 logging: file: path:/logs #type-aliases-package:show.mrkay.dao
1.3generatorConfig.xml
步骤:
1导入依赖
2创建*generaltorConfig.xml配置文件
3配置yml配置文件
注意:
pom中generalto-maven-plugs中必须指定mysql驱动,并且明确版本
generaltorConfig.xml中的
标签中需要指定tableName和生成的实体名字
2创建数据库表
--建表语句: /* SQLyogEnterprisev12.09(64bit) MySQL-5.5.40:Database-generaltor ********************************************************************* */ /*!40101SETNAMESutf8*/; /*!40101SETSQL_MODE=''*/; /*!40014SET@OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS,UNIQUE_CHECKS=0*/; /*!40014SET@OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS,FOREIGN_KEY_CHECKS=0*/; /*!40101SET@OLD_SQL_MODE=@@SQL_MODE,SQL_MODE='NO_AUTO_VALUE_ON_ZERO'*/; /*!40111SET@OLD_SQL_NOTES=@@SQL_NOTES,SQL_NOTES=0*/; CREATEDATABASE/*!32312IFNOTEXISTS*/`generaltor`/*!40100DEFAULTCHARACTERSETutf8*/; /*Tablestructurefortable`student`*/ DROPTABLEIFEXISTS`student`; CREATETABLE`student`( `id`int(32)NOTNULLAUTO_INCREMENT, `name`varchar(15)DEFAULTNULL, `sex`varchar(2)DEFAULTNULL, `phone`int(15)DEFAULTNULL, PRIMARYKEY(`id`) )ENGINE=InnoDBAUTO_INCREMENT=2DEFAULTCHARSET=utf8; /*Dataforthetable`student`*/ insertinto`student`(`id`,`name`,`sex`,`phone`)values(1,'lk','男',12345578); /*!40101SETSQL_MODE=@OLD_SQL_MODE*/; /*!40014SETFOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS*/; /*!40014SETUNIQUE_CHECKS=@OLD_UNIQUE_CHECKS*/; /*!40111SETSQL_NOTES=@OLD_SQL_NOTES*/;
3运行
如果使用IDEA我们只需要找到右边侧栏中的maven(没有记得调出了)
找到plugins–>mybatis-generaltor–>mybatis-generaltor:generaltor之后双击即可,此时刷新一下项目就自动生成我们想要的,mapper和xml以及pojo
4测试
创建穿点controller和service来完成一次测试,
packageshow.mrkay.controller; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.web.bind.annotation.GetMapping; importorg.springframework.web.bind.annotation.Mapping; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RestController; importorg.springframework.web.servlet.support.JstlUtils; importshow.mrkay.dao.StudentMapper; importshow.mrkay.pojo.Student; importshow.mrkay.service.UserService; importjava.util.List; /** *@ClassName:UserController *@description:用户Controller *@Author:MrKay *@Date:2020/11/8 */ @RestController @RequestMapping("/user") publicclassUserController{ @Autowired privateUserServiceuserService; @GetMapping("/alllist") publicListselectAll(){ returnuserService.selectAll(); } }
packageshow.mrkay.service; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.stereotype.Service; importshow.mrkay.dao.StudentMapper; importshow.mrkay.pojo.Student; importjava.util.List; /** *@ClassName:UserService *@description:UserService *@Author:MrKay *@Date:2020/11/8 */ @Service publicclassUserService{ @Autowired privateStudentMapperstudentMapper; publicListselectAll(){ List students=studentMapper.selectAll(); returnstudents; } }
packageshow.mrkay; importorg.mybatis.spring.annotation.MapperScan; importorg.mybatis.spring.annotation.MapperScans; importorg.springframework.boot.SpringApplication; importorg.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("show.mrkay.dao") publicclassGeneratorApplication{ publicstaticvoidmain(String[]args){ SpringApplication.run(GeneratorApplication.class,args); } }
注意:
启动器类中需要加入@MapperScan(“mapper接口所在包”)注解,这样我们就不需要手动创建实现类了,mybatis会自动帮我们创建
启动之后:
地址栏输入localhost:8080/user/alllist
得到查询结果:
[{"id":1,"name":"lk","sex":"男","phone":12345578}]
5完整代码
https://gitee.com/mrkay0313/boot-mybatis-generaltor.git
6总结
便于自己以后使用,做的一个demo
到此这篇关于SpringBoot整合mybatis-generator-maven-plugin的文章就介绍到这了,更多相关SpringBoot整合mybatis-generator-maven-plugin内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!