1导入需要的jar包
<dependencies>
<!-- mp依赖 mybaits-plus 会自动维护mybaits jar包 所以不用加入了 防止版本冲突 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.3</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!-- Apache velocity -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<!-- sfl4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
</dependencies>
2 配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 数据源 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 事务管理器 事务管理器 -->
<bean id="dataSourceTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 基于注解的事务管理 基于注解的事务管理 基于注解的事务管理 基于注解的事务管理 -->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
<!-- 配置 SqlSessionFactoryBean -->
<!--
mybatis提供的:org.mybatis.spring.SqlSessionFactoryBean
改成mybatis-plus提供的 :com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean
-->
<bean id="sqlSessionFactoryBean" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 别名处理 别名处理 -->
<property name="typeAliasesPackage" value="com.shi.entity"></property>
<!--注入mybaits的全局策略配置-->
<property name="globalConfig" ref="globalConfiguration"></property>
</bean>
<!-- 配置 mybatis 扫描 mapper接口的路径 接口的路径 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.shi.dao"></property>
</bean>
<!--定义全局的mybatisPlus的全局策略配置-->
<bean id="globalConfiguration" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
<!--驼峰命名法:在2.3版本之后 dbColumnUnderline 默认就是true-->
<property name="dbColumnUnderline" value="true"></property>
<!--全局主键策略 0:表示自增主键 -->
<property name="idType" value="0"></property>
</bean>
</beans>
3 数据库的配置
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.69.11:3306/shihaifeng
jdbc.username=用户名
jdbc.password=密码
4 生成代码
package com.shi.test;
import com.baomidou.mybatisplus.enums.IdType;
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.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;
/**
* @author shfeng
* @create 2019-07-18 16:59
* 我的代码自动生成器
*/
public class MyGeneratorTest {
@Test
public void myGenerator(){
//1 全局配置
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setActiveRecord(false) //是否支持AR模式
.setAuthor("shiye") //作者
.setOutputDir("D:\\project\\shiTest\\shi_Mybatis_Plus\\src\\main\\java") //设置生成路径
.setFileOverride(true) //文件覆盖
.setIdType(IdType.AUTO) //主键策略 自增
.setServiceName("%sService") //设置生成的servic接口的名字首字目去除I
.setBaseResultMap(true) //生成sql mapper映射文件
.setBaseColumnList(true);
//2 数据库配置
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDbType(DbType.MYSQL) //设置数据库类型
.setDriverName("com.mysql.jdbc.Driver") //设置数据驱动
.setUrl("jdbc:mysql://192.168.69.11:3306/shihaifeng") //设置数据库url
.setUsername("") //设置用户名
.setPassword(""); //设置密码
//3 策略配置
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setCapitalMode(true) //全局大写命名
.setDbColumnUnderline(true) //指定表名字段名是否使用下划线
.setNaming(NamingStrategy.underline_to_camel) //数据库表映射到实体的命名方法:驼峰命名法
.setTablePrefix("") //表的前缀
.setInclude("machine_item"); //需要生成的表
//4 包名策略配置
PackageConfig packageConfig = new PackageConfig();
packageConfig.setParent("com.shi") //全局父包
.setController("controller")
.setService("service")
.setServiceImpl("service.impl")
.setMapper("dao") //dao接口
.setXml("dao") //mapper映射文件
.setEntity("entity");
//5 整合配置
AutoGenerator autoGenerator = new AutoGenerator();
autoGenerator.setGlobalConfig(globalConfig)
.setDataSource(dataSourceConfig)
.setStrategy(strategyConfig)
.setPackageInfo(packageConfig);
//6 执行
autoGenerator.execute();
}
}