本文介绍如何将Maven和Mybatis-Generator配合使用。
image
简介Mybatis-Generator是Mybatis提供的一个便捷型插件,自动可以为项目生产对应的实体类,Mapper,dao层。
官网文档:http://www.mybatis.org/generator/index.html
入门案例本文使用SpringBoot结合Mybatis-Generator插件使用,数据库Mysql。
新建项目新建一个SpringBoot项目。
依赖文件在项目pom文件中,引入Mybatis-Generator插件,并且引入Mybatis和Mysql依赖。完整pom代码如下:
配置Mybatis-Generator配置4.0.0 com.dalaoyang springboot_generator0.0.1-SNAPSHOT jar springboot_generator springboot_generator org.springframework.boot spring-boot-starter-parent1.5.15.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-testtest org.springframework.boot spring-boot-starter-weborg.mybatis.spring.boot mybatis-spring-boot-starter1.3.1 org.springframework.boot spring-boot-devtoolsruntime mysql mysql-connector-javaruntime org.mybatis.generator mybatis-generator-maven-plugin1.3.2 mybatis-generator deploy generate src/main/resources/mybatis-generator/generatorConfig.xml true true mysql mysql-connector-java5.1.46 org.mybatis.generator mybatis-generator-core1.3.2 org.springframework.boot spring-boot-maven-pluginexec
在pom文件中配置的Mybatis-Generator 工具配置文件的位置新建一个generatorConfig.xml,(本文案例配置的位置是src/main/resources/mybatis-generator/generatorConfig.xml),配置文件代码如下,具体配置需要自行修改至自己的项目:
配置application.properties
配置项目的application.properties,其中数据库信息,Mapper地址之前都有过介绍,具体SpringBoot-Mybatis配置可以参考:
《SpringBoot+Mybatis+MySql学习》
本文配置如下:
## mapper xml 文件地址mybatis.mapper-locations=classpath*:mapper int deleteByPrimaryKey(Long id); int insert(User record); User selectByPrimaryKey(Long id); ListselectAll(); int updateByPrimaryKey(User record); }
UserMapper.xml代码如下:
delete from user where id = #{id,jdbcType=BIGINT} SELECT LAST_INSERT_ID() insert into user (user_name, user_password) values (#{userName,jdbcType=VARCHAR}, #{userPassword,jdbcType=VARCHAR})update user set user_name = #{userName,jdbcType=VARCHAR}, user_password = #{userPassword,jdbcType=VARCHAR} where id = #{id,jdbcType=BIGINT}
作者:dalaoyang
链接:https://www.jianshu.com/p/a8bfc14a3534



