xml模板文件
<?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">
<generatorConfiguration>
<context id="DB2Tables" targetRuntime="Mybatis3">
<!-- 避免生成重复代码的插件 -->
<!-- <plugin type="com.vi.tmall.util.OverlsMergeablePlugin"/> -->
<!-- 是否不生成注释 -->
<commentGenerator>
<property name="suppressDate" value="true" />
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库链接地址账号密码 -->
<jdbcConnection driverClass="oracle.jdbc.OracleDriver"
connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="sys as sysdba"
password="123456" />
<!-- 这个元素的配置用来指定JDBC类型和Java类型如何转换。 -->
<javaTypeResolver>
<!-- 是否强制将DECIMAL和NUMERIC类型的字段转换为Java类型的java.math.BigDecimal 否则是integer-->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- targetPackage:生成实体类存放的包名, targetProject:指定目标项目路径,可以是绝对路径或相对路径(如 targetProject="src/main/java") -->
<javaModelGenerator
targetPackage="org.cjr.entity" targetProject=".\src">
<!--enableSubPackages 如果true,MBG会根据catalog和schema来生成子包。如果false就会直接用targetPackage属性。默认为false -->
<property name="enableSubPackages" value="false" />
<!-- trimStrings:是否对数据库查询结果进行trim操作,如果设置为true就会生成类似这样public void setUsername(String
username) -->
<property name="trimString" value="true" />
</javaModelGenerator>
<!--生成xml映射文件存放位置 -->
<sqlMapGenerator targetPackage="org.cjr.mapper"
targetProject=".\src">
</sqlMapGenerator>
<!--生成mapper类存放位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="org.cjr.mapper" targetProject=".\src">
</javaClientGenerator>
<!--生成对应表及类名,enableXXX:XXX代表多种SQL方法,该属性用来指定是否生成对应的XXX语句 -->
<table tableName="Student"></table>
<table tableName="StudentCard"></table>
<table tableName="StudentClass"></table>
</context>
</generatorConfiguration>
3.工具类
package org.cjr.test;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
public class Test {
public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
List<String> warning = new ArrayList<>();
File file = new File("src/generator.xml");
ConfigurationParser cp = new ConfigurationParser(warning);
Configuration config = cp.parseConfiguration(file);
DefaultShellCallback callback = new DefaultShellCallback(true);
MyBatisGenerator generator = new MyBatisGenerator(config,callback,warning);
generator.generate(null);
}
}
执行Test 就可以生成结构了
最后,我们写一个 查询 来测试功能;
public class test1111 {
public static void main(String[] args) throws IOException {
Reader reader = Resources.getResourceAsReader("config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sqlSessionFactory.openSession();
/*
* 1.实现接口 2.然后接口文件的名称 与 xml中的namespace形成映射 自动绑定配置
*/
StudentMapper studentmapper = session.getMapper(StudentMapper.class);
// example 中Criteria 的查询条件
StudentExample example = new StudentExample();
Criteria criteria = example.createCriteria();
List<Student> student = studentmapper.selectByExample(example);
System.out.println(student);
}
}
运行结果