Springboot通用mapper和mybatis-generator代码示例
实现功能:根据数据库中的表,自动生成model、dao和对应的xml文件。xml中实现通用mapper中CURD功能
1、引入依赖
<properties> <mybatis.generator.version>1.3.7</mybatis.generator.version> <tk.mybatis.version>4.1.5</tk.mybatis.version> </properties> <dependencies> <!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <!--通用mapper--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>${tk.mybatis.version}</version> </dependency> <!--mybatis-generator--> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>${mybatis.generator.version}</version> </dependency> <!--mysql数据库--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--druid数据源--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <dependencies> <build> <plugins> <!--配置Maven中 mybatis-generator 插件--> <!--加入tk.mybatis的依赖--> <plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>${mybatis.generator.version}</version><configuration> <!--可移动--> <verbose>true</verbose> <!--可覆盖--> <overwrite>true</overwrite></configuration><dependencies> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>${tk.mybatis.version}</version> </dependency></dependencies> </plugin> </plugins> </build>
2、配置通用mapper
方式1:配置要扫描的mapper
@SpringBootApplication@MapperScan(basePackages = 'com.liang.mapper')public class MyappApplication { public static void main(String[] args) { SpringApplication.run(MyappApplication.class, args); }}
方式2:扫描的mapper
@Configurationpublic class MybatisConfig { @Bean(name = 'mapperScannerConfigurer') public MapperScannerConfigurer MapperScannerConfigurer1() { MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mapperScannerConfigurer.setBasePackage('com.liang.mapper'); Properties properties = new Properties(); properties.setProperty('notEmpty', 'false'); properties.setProperty('IDENTITY', 'MYSQL'); properties.setProperty('mappers', Mapper.class.getName()); mapperScannerConfigurer.setSqlSessionFactoryBeanName('sqlSessionFactory'); mapperScannerConfigurer.setProperties(properties); return mapperScannerConfigurer; }}
3、application.yaml中配置mybatis
#Mybatis(注意不是 mybatis.config-location 这个配置)mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.liang.pojo configuration-properties: { notEmpty: false, IDENTITY: ’MYSQL’, mappers: 'tk.mybatis.mapper.common.Mapper' }
4、添加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> <!-- 数据库驱动--> <classPathEntry location='E:ideaMavenMavenRepositorymysqlmysql-connector-java8.0.22mysql-connector-java-8.0.22.jar' /> <context targetRuntime='MyBatis3Simple'> <!--beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号; --> <property name='beginningDelimiter' value='`'></property> <property name='endingDelimiter' value='`'></property> <!-- 生成的Java文件的编码 --> <property name='javaFileEncoding' value='utf-8'/> <!--添加Tk-mapper插件--> <plugin type='tk.mybatis.mapper.generator.MapperPlugin' > <property name='mappers' value='tk.mybatis.mapper.common.Mapper'/> </plugin> <jdbcConnection driverClass='com.mysql.cj.jdbc.Driver' connectionURL='jdbc:mysql://localhost:3306/temp_db?serverTimezone=Asia/Shanghai&characterEncoding=utf-8&useUnicode=true&useSSL=false' userId='root' password='123456'> </jdbcConnection> <!-- java类型处理器 用于处理DB中的类型到Java中的类型,默认使用JavaTypeResolverDefaultImpl; 注意一点,默认会先尝试使用Integer,Long,Short等来对应DECIMAL和 NUMERIC数据类型; --> <javaTypeResolver > <property name='forceBigDecimals' value='false' /> </javaTypeResolver> <!--生成entity类存放位置--> <javaModelGenerator targetPackage='com.liang.pojo' targetProject='src/main/java'> <property name='enableSubPackages' value='false' /> <property name='trimStrings' value='true' /> </javaModelGenerator> <!--生成映射文件存放位置 --> <sqlMapGenerator targetPackage='mapper' targetProject='src/main/resources'> <property name='enableSubPackages' value='true' /> </sqlMapGenerator> <!--生成Dao类存放位置--> <javaClientGenerator type='XMLMAPPER' targetPackage='com.liang.mapper' targetProject='src/main/java'> <property name='enableSubPackages' value='true' /> </javaClientGenerator> <!--要生成的Table,注意不同的数据库,sqlStatement--> <!--sqlStatement 不同的数据库配置 http://mybatis.org/generator/configreference/generatedKey.html--> <table domainObjectName='School2' tableName='t_school'> <!--<generatedKey column='id' sqlStatement='MySql' identity='true' />--> <generatedKey column='id' sqlStatement='SELECT LAST_INSERT_ID() AS id' identity='true' /> </table> </context></generatorConfiguration>
5、完成
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持好吧啦网。
相关文章: