spring集成mybatis-plus
一、mybatis-plus
使用mybatis-plus可以轻松实现通用crue、通用service,不用再在xml、dao、service里写增删改查的代码(需要写特殊方法的时候可以按原先的来),通用的crue加入了多达17种方法
配合代码生成,只要建好表、实体类,1秒钟就可以生成各个层的类
二、官方例子
三、简单配置方法
1、pom.xml
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>仓库最高版本号</version> </dependency>
2、spring-context-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!-- 加载配置属性文件 --> <context:property-placeholder location="classpath:library.properties" ignore-unresolvable="true" /> <!-- 配置SessionFactory --> <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:org/sy/library/mapper/xml/**/*.xml"/> <property name="typeAliasesPackage" value="org.sy.library.entity.*"/> <property name="configLocation" value="classpath:conf/mybatis/mybatis-config.xml"/> <property name="globalConfig" ref="globalConfig"/> <property name="plugins"> <array> <!-- 分页插件配置 --> <bean id="paginationInterceptor" class="com.baomidou.mybatisplus.plugins.PaginationInterceptor"></bean> </array> </property> </bean> <!-- 配置mybatisConfig --> <bean id="mybatisConfig" class="com.baomidou.mybatisplus.MybatisConfiguration"> <property name="mapUnderscoreToCamelCase" value="true"/> </bean> <!-- 定义 MP 全局策略 --> <bean id="globalConfig" class="com.baomidou.mybatisplus.entity.GlobalConfiguration"> <!-- 全局ID类型: 0, "数据库ID自增", 1, "用户输入ID", 2, "全局唯一ID", 3, "全局唯一ID"--> <property name="idType" value="2"/> </bean> <!-- MyBatis 动态扫描 --> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="org.sy.library.mapper"/> </bean> <!-- 配置数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> ............. </bean> </beans>
3、mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL Map Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!-- 下划线转驼峰 --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> </configuration>
三、代码生成
package org.sy.library.common.auto; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.config.DataSourceConfig; import com.baomidou.mybatisplus.generator.config.GlobalConfig; import com.baomidou.mybatisplus.generator.config.PackageConfig; import com.baomidou.mybatisplus.generator.config.StrategyConfig; import com.baomidou.mybatisplus.generator.config.TemplateConfig; import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert; import com.baomidou.mybatisplus.generator.config.rules.DbType; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; /** * 实体生成器 */ public class CustomGenerator { public static void main(String[] args) { AutoGenerator mpg = new AutoGenerator(); //全局配置 GlobalConfig gc = new GlobalConfig(); gc.setOutputDir("C://utopia-company//开发库//utopia//library//library-api//src//main//java"); gc.setFileOverride(false); gc.setEnableCache(false); gc.setActiveRecord(false); gc.setAuthor("sy"); mpg.setGlobalConfig(gc); //数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setDbType(DbType.MYSQL); dsc.setTypeConvert(new MySqlTypeConvert()); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("xxxxxxx"); dsc.setUrl("jdbc:mysql://xxx.xxx.xxx.xxx:3306/utopia?useUnicode=true&characterEncoding=utf-8&useSSL=false"); mpg.setDataSource(dsc); //策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setInclude(new String[] { "library_user_book" }); // 需要生成的表 strategy.setTablePrefix("library_");// 此处可以修改为您的表前缀 strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略 mpg.setStrategy(strategy); //包配置 PackageConfig pc = new PackageConfig(); pc.setParent("org.sy.library"); //pc.setModuleName("test"); mpg.setPackageInfo(pc); //目录配置 TemplateConfig tc = new TemplateConfig(); tc.setController(null); mpg.setTemplate(tc); //执行生成 mpg.execute(); } }