1.pom.xml引入相关依赖,这里是使用一个大佬的方法,他的github地址:点击打开链接
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.41</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <!--mapper的生成类--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>1.2.4</version> </dependency> <!--pagehelper--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.9</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> <scope>compile</scope> <optional>true</optional> </dependency>
2.application.properties 配置,使用阿里巴巴的druid数据源,大佬们都说这个比较好,mapper.mappers=com.smxy.lq.utils.MyMapper,MyMapper这个类是自己写的继承了两个开源作者写的类,这样就可以使用他写的增删改查的方法。
############################################################ # # mybatis 配置 # ############################################################ #mybatis mybatis.type-aliases-package=com.smxy.lq.pojo mybatis.mapper-locations=classpath:mapper/*.xml #mapper #mappers 多个接口时逗号隔开 mapper.mappers=com.smxy.lq.utils.MyMapper mapper.not-empty=false mapper.identity=MYSQL #pagehelper分页插件 pagehelper.helperDialect=mysql pagehelper.reasonable=true pagehelper.supportMethodsArguments=true pagehelper.params=count=countSql ############################################################ # # 配置数据源相关 使用阿里巴巴的druid数据源 # ############################################################ spring.datasource.url=jdbc:mysql://localhost:3306/springboot spring.datasource.username=root spring.datasource.password=123 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.druid.initial-size=1 spring.datasource.druid.min-idle=1 spring.datasource.druid.max-active=20 spring.datasource.druid.test-on-borrow=true spring.datasource.druid.stat-view-servlet.allow=true
3.配置generatorConfig.xml,用于生成sql语句的xml,java的pojo类,以及mapper接口
<?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="MyBatis3Simple" defaultModelType="flat"> <property name="beginningDelimiter" value="`" /> <property name="endingDelimiter" value="`" /> <plugin type="tk.mybatis.mapper.generator.MapperPlugin"> <property name="mappers" value="com.smxy.lq.utils.MyMapper" /> </plugin> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/springboot" userId="root" password="123"> </jdbcConnection> <!-- 对于生成的pojo所在包 --> <javaModelGenerator targetPackage="com.smxy.lq.pojo" targetProject="src/main/java" /> <!-- 对于生成的mapper所在目录 --> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources" /> <!-- 配置mapper对应的java映射 --> <javaClientGenerator targetPackage="com.smxy.lq.mapper" targetProject="src/main/java" type="XMLMAPPER" /> <table tableName="sb_user" ></table> </context> </generatorConfiguration>
4.写一个工具类接口继承tk.mybatis.mappe里面的Mapper, MySqlMapper类,创建mapper时实现自己写的MyMapper接口就可以使用mapper的通用方法了,不需要我们自己写增删改查的sql语句
数据的创建一个表
5.创建java工具类并Run as->java application,执行generatorConfig.xml文件
package com.smxy.lq.utils; import java.io.File; import java.util.ArrayList; import java.util.List; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; /** * * @Description 逆行生成工具类 * @author Bush罗 * @date 2018年4月15日 * */ public class GeneratorDisplay { public void generator() throws Exception{ List<String> warnings = new ArrayList<String>(); boolean overwrite = true; //指定 逆向工程配置文件 File configFile = new File("generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); } public static void main(String[] args) throws Exception { try { GeneratorDisplay generatorSqlmap = new GeneratorDisplay(); generatorSqlmap.generator(); } catch (Exception e) { e.printStackTrace(); } } }
6.生成相应的代码
扫描mapper下的包
7.测试,使用insert是会使用mapper自带的插入方法,里面还有许多自带的方法,可自行尝试