MyBatis 的代码生成器基于xml文件进行生成,可生成: 实体类、Mapper 接口、Mapper 映射文件。
MP 的代码生成器基于Java代码进行生成,可生成: 实体类(可以选择是否支持 AR)、Mapper 接口、Mapper 映射文件、 Service 层、Controller 层。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<!-- mp 依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.3</version>
</dependency>
<!-- mybatisplus逆向工程需要模板引擎,用freemaker也行 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
上面是必须的三个依赖,
为了可以在控制台直观的看到生成情况,可以添加日志包(slf4j-api和slf4j-log4j2),
为了让生成的代码不会报错,还可以根据情况添加spring相关的依赖、lombok依赖等。
/**
* @author: zhu
* @date: 2018/8/20 11:17
* mybatis-plus逆向工程示例代码
*/
public class test {
@Test
public void testGenerator(){
//1、全局配置
GlobalConfig config = new GlobalConfig();
config.setActiveRecord(true)//开启AR模式
.setAuthor("zhu")//设置作者
//生成路径(一般都是生成在此项目的src/main/java下面)
.setOutputDir("E:\\develop\\Java\\workspace\\ideaworkspace\\mpg\\src\\main\\java")
.setFileOverride(true)//第二次生成会把第一次生成的覆盖掉
.setIdType(IdType.AUTO)//主键策略
.setServiceName("%sService")//生成的service接口名字首字母是否为I,这样设置就没有I
.setBaseResultMap(true)//生成resultMap
.setBaseColumnList(true);//在xml中生成基础列
//2、数据源配置
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDbType(DbType.MYSQL)//数据库类型
.setDriverName("com.mysql.jdbc.Driver")
.setUrl("jdbc:mysql:///数据库名")
.setUsername("数据库用户名")
.setPassword("数据库密码");
//3、策略配置
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setCapitalMode(true)//开启全局大写命名
.setDbColumnUnderline(true)//表名字段名使用下划线
.setNaming(NamingStrategy.underline_to_camel)//下划线到驼峰的命名方式
.setTablePrefix("tb_")//表名前缀
.setEntityLombokModel(true)//使用lombok
.setInclude("表1","表2");//逆向工程使用的表
//4、包名策略配置
PackageConfig packageConfig = new PackageConfig();
packageConfig.setParent("com.zhu.mpg")//设置包名的parent
.setMapper("mapper")
.setService("service")
.setController("controller")
.setEntity("entity")
.setXml("mapper");//设置xml文件的目录
//5、整合配置
AutoGenerator autoGenerator = new AutoGenerator();
autoGenerator.setGlobalConfig(config)
.setDataSource(dataSourceConfig)
.setStrategy(strategyConfig)
.setPackageInfo(packageConfig);
//6、执行
autoGenerator.execute();
}
}
Employee.java
/**
* <p>
*
* </p>
*
* @author weiyunhui
* @since 2018-06-21
*/
@TableName("tbl_employee")
public class Employee extends Model<Employee> {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String lastName;
private String email;
private String gender;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
protected Serializable pkVal() {
return this.id;
}
@Override
public String toString() {
return "Employee{" +
", id=" + id +
", lastName=" + lastName +
", email=" + email +
", gender=" + gender +
", age=" + age +
"}";
}
}
EmployeeController
/**
* <p>
* 前端控制器
* </p>
*
* @author weiyunhui
* @since 2018-06-21
*/
@Controller
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
// public String login() {
//
// //employeeService.select
// }
}
EmployeeMapper.java
package com.atguigu.mp.mapper;
import com.atguigu.mp.beans.Employee;
import com.baomidou.mybatisplus.mapper.BaseMapper;
/**
* <p>
* Mapper 接口
* </p>
*
* @author weiyunhui
* @since 2018-06-21
*/
public interface EmployeeMapper extends BaseMapper<Employee> {
}
EmployeeMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.mp.mapper.EmployeeMapper">
<!-- 开启二级缓存 -->
<cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.atguigu.mp.beans.Employee">
<id column="id" property="id" />
<result column="last_name" property="lastName" />
<result column="email" property="email" />
<result column="gender" property="gender" />
<result column="age" property="age" />
</resultMap>
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
id, last_name, email, gender, age
</sql>
</mapper>
EmployeeService.java
package com.atguigu.mp.service;
import com.atguigu.mp.beans.Employee;
import com.baomidou.mybatisplus.service.IService;
/**
* <p>
* 服务类
* </p>
*
* @author weiyunhui
* @since 2018-06-21
*/
public interface EmployeeService extends IService<Employee> {
}
EmployeeServiceImpl.java
package com.atguigu.mp.service.impl;
import com.atguigu.mp.beans.Employee;
import com.atguigu.mp.mapper.EmployeeMapper;
import com.atguigu.mp.service.EmployeeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
/**
* <p>
* 服务实现类
* </p>
*
* @author weiyunhui
* @since 2018-06-21
*/
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
//不用再进行mapper的注入.
/**
* EmployeeServiceImpl 继承了ServiceImpl
* 1. 在ServiceImpl中已经完成Mapper对象的注入,直接在EmployeeServiceImpl中进行使用
* 2. 在ServiceImpl中也帮我们提供了常用的CRUD方法, 基本的一些CRUD方法在Service中不需要我们自己定义.
*
*
*/
}