MyBatis Generator
简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的根据表生成对应的映射文件,接口,以及bean类。支持基本的增删改查,以及QBC风格的条件查询。但是表连接、存储过程等这些复杂sql的定义需要我们手工编写。
官方文档地址
http://www.mybatis.org/generator/
官方工程地址
https://github.com/mybatis/generator/releases
MBG使用实操:
创建工程
- 1、添加依赖
org.mybatis.generator mybatis-generator-core 1.3.6 org.mybatis.generator mybatis-generator-maven-plugin 1.4.0 log4j log4j 1.2.17 org.mybatis mybatis 3.4.1 org.mybatis.generator mybatis-generator-core 1.3.2 mysql mysql-connector-java 8.0.23 com.oracle.database.jdbc ojdbc6 11.2.0.4 org.slf4j slf4j-api 1.7.32 org.slf4j slf4j-log4j12 1.7.32 test src/main/java ***.xml false
- 2、配置mybatis文件
- 3、编写MBG的配置文件(重要几处配置)
- jdbcConnection配置数据库连接信息
- javaModelGenerator配置javaBean的生成策略
- sqlMapGenerator 配置sql映射文件生成策略
- javaClientGenerator配置Mapper接口的生成策略
- table 配置要逆向解析的数据表
tableName:表名
domainObjectName:对应的javaBean名
mbg.xml
注意:
Context标签
targetRuntime=“MyBatis3“可以生成带条件的增删改查
targetRuntime=“MyBatis3Simple“可以生成基本的增删改查
如果再次生成,建议将之前生成的数据删除,避免xml向后追加内容出现的问题。
- 4、运行代码生成器生成代码
public class MBGTest {
public static void main(String[] args) {
try {
List warnings = new ArrayList();
File configFile = new File("mbg.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(true);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,callback, warnings);
myBatisGenerator.generate(null);
} catch (IOException e) {
e.printStackTrace();
} catch (XMLParserException e) {
e.printStackTrace();
} catch (InvalidConfigurationException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
运行结果:
DEBUG 10-18 16:25:46,593 Retrieving column information for table "tbl_dept" (Log4jImpl.java:46) DEBUG 10-18 16:25:46,744 Found column "id", data type 4, in table "test..tbl_dept" (Log4jImpl.java:46) DEBUG 10-18 16:25:46,745 Found column "dept_name", data type 12, in table "test..tbl_dept" (Log4jImpl.java:46) DEBUG 10-18 16:25:46,794 Retrieving column information for table "tbl_employee" (Log4jImpl.java:46) DEBUG 10-18 16:25:46,828 Found column "id", data type 4, in table "test..tbl_employee" (Log4jImpl.java:46) DEBUG 10-18 16:25:46,828 Found column "last_name", data type 12, in table "test..tbl_employee" (Log4jImpl.java:46) DEBUG 10-18 16:25:46,828 Found column "gender", data type 1, in table "test..tbl_employee" (Log4jImpl.java:46) DEBUG 10-18 16:25:46,829 Found column "email", data type 12, in table "test..tbl_employee" (Log4jImpl.java:46) DEBUG 10-18 16:25:46,829 Found column "d_id", data type 4, in table "test..tbl_employee" (Log4jImpl.java:46) Process finished with exit code 0
- 5、测试生成的代码
为了测试效果,给生成的Employee示例添加toString方法
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", lastName='" + lastName + ''' +
", gender='" + gender + ''' +
", email='" + email + ''' +
", dId=" + dId +
'}';
}
测试代码1:
public class Test {
public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
SqlSession sqlSession = getSqlSessionFactory().openSession();
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
//查询所有
List employees = mapper.selectByExample(null);
employees.forEach(System.out::println);
}
public static SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
}
运行结果:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Employee{id=1, lastName='张三', gender='1', email='zhangsan@qq.com', dId=1001}
Employee{id=2, lastName='tom', gender='1', email='tom002@qq.com', dId=1002}
Employee{id=3, lastName='jerry', gender='0', email='jerry@qq.com', dId=1003}
Employee{id=4, lastName='jeck', gender='1', email='jeck004@qq.com', dId=1004}
Employee{id=5, lastName='tom', gender='1', email='tom@qq.com', dId=1005}
Employee{id=7, lastName='lucy', gender='0', email='lucy@qq.com', dId=1001}
Employee{id=10, lastName='李四', gender='1', email='lisi@qq.com', dId=1002}
Employee{id=11, lastName='王五', gender='1', email='wangwu@qq.com', dId=1003}
Employee{id=12, lastName='妍丽', gender='0', email='yanli@qq.com', dId=1004}
Employee{id=13, lastName='静娈', gender='0', email='jingluan@qq.com', dId=1003}
Employee{id=14, lastName='上官思伯', gender='1', email='shangguansibo@qq.com', dId=1004}
Employee{id=15, lastName='式微', gender='0', email='shiwei@qq.com', dId=1003}
Employee{id=16, lastName='琼琚', gender='0', email='qiongju@qq.com', dId=1004}
Employee{id=17, lastName='维曦', gender='1', email='weixi@qq.com', dId=1004}
Employee{id=18, lastName='维曦', gender='1', email='weixi@qq.com', dId=1004}
Process finished with exit code 0
测试代码2:
public class Test {
public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
SqlSession sqlSession = getSqlSessionFactory().openSession();
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
//xxxExample就是封装查询条件的
EmployeeExample employeeExample = new EmployeeExample();
//创建一个Criteria,这个Criteria就是拼装查询条件
EmployeeExample.Criteria criteria = employeeExample.createCriteria();
criteria.andLastNameLike("%j%");
List employees = mapper.selectByExample(employeeExample);
employees.forEach(System.out::println);
}
public static SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
}
运行结果:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Employee{id=3, lastName='jerry', gender='0', email='jerry@qq.com', dId=1003}
Employee{id=4, lastName='jeck', gender='1', email='jeck004@qq.com', dId=1004}
Process finished with exit code 0



