版权声明:最终解释权归属Hern、HernSong(hernsong)、苍鹭、www.hernsong.com所有! https://blog.csdn.net/qq_36761831/article/details/89403871
MyBatis Generator简介
简称MBG,是一个专门为MyBatis框架使用者定 制的代码生成器,可以快速的根据表生成对应的 映射文件,接口,以及bean类。支持基本的增删 改查,以及QBC风格的条件查询。但是表连接、 存储过程等这些复杂sql的定义需要我们手工编写
官方文档地址 http://www.mybatis.org/generator/
官方工程地址 https://github.com/mybatis/generator/releases
MBG使用
generator,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>
<!--
targetRuntime="MyBatis3Simple":生成简单版的CRUD
MyBatis3:豪华版
-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<!--jdbcConnection指定如何连接数据库-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&useSSL=false"
userId="root"
password="10043632">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- javaModelGenerator:指定javaBean的生成策略
targetPackage="com.javabean":目标包名
targetProject="./src":生成在哪(直接生成在目标工程下)
-->
<javaModelGenerator targetPackage="com.javaBean" targetProject="./src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- sqlMapGenerator:sql映射生成策略,配置SQL 映射文件存放的位置-->
<sqlMapGenerator targetPackage="com.sqlMapper" targetProject="./src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- javaClientGenerator:指定mapper接口所在的位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.mapperInterface" targetProject="./src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 指定要逆向分析哪些表:根据表要创建javaBean,tableName指定表名 -->
<table tableName="userinfo" domainObjectName="UserInfo"></table>
</context>
</generatorConfiguration>
生成器代码
public class Main {
public SqlSessionFactory getSqlSessionFactory() throws IOException {
String path = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(path);
return new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void test() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
String path = "mbg.xml";
File configFile = new File(path);
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);
}
@Test
public void testselect() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
UserInfoMapper userInfoMapper = sqlSession.getMapper(UserInfoMapper.class);
UserInfo userInfo = userInfoMapper.selectByPrimaryKey(1);
System.out.println(userInfo.getUserid());
}finally {
sqlSession.close();
}
}
}