是什么&可以做什么
myabtis 官方提供了一个逆向工程,可以根据数据库表结构生成对应的bean,dao,mapper映射,极大程度提高开发效率,并且有很多优秀凯源工具在此基础上完成了对controller的自动生成
目录结构
因为跟的视频是比较久远的版本,依赖载入按照jar方式进行,如果之后有时间,补充一个maven版本。
-
生成器配置
```java <?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">
<a name="dqSpB"></a>
# 测试执行生成
```java
@Test
public void testMbg() throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
// 指定mbg的配置文件位置
File configFile = new File("src/main/resources/mbg.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
callback, warnings);
// 调用方法进行生成
myBatisGenerator.generate(null);
}
生成之后
- bean下会有实体类,以及实体类example
- beanExample 封装了一些查询的方法,下面会具体进行说明
测试
```java public SqlSessionFactory getSqlSessionFactory() throws IOException {
}String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); return new SqlSessionFactoryBuilder().build(inputStream);
@Test
public void testMyBatis3Simple() throws IOException{
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try{
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
// 1 zxxxxExample 就是封装查询条件
// List
// 3 or条件的封装
EmployeeExample.Criteria criteria1 = example.createCriteria();
criteria1.andEmailLike("%e%");
example.or(criteria1);
List<Employee> employees = mapper.selectByExample(example);
for (Employee employee :employees) {
System.out.println(employee.toString());
}
}finally{
openSession.close();
}
}