【串线篇】Mybatis拓展之MBG
2024-09-02 14:46:07
MBG-逆向工程
一、介绍
MBG:MyBatis Generator:代码生成器;
MyBatis官方提供的代码生成器;帮我们逆向生成;
正向:
table----javaBean---BookDao---dao.xml---xxx
逆向工程:
根据数据表table,逆向分析数据表,自动生成javaBean---BookDao---dao.xml---xxx
二、配置
1、导包:mbg的核心包
….. mybatis-generator-core-1.3.2.jar
2、编写mbg.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> <!--
MyBatis3Simple:基础班CRUD
MyBatis3:复杂版CRUD
-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- jdbcConnection:指导连接到哪个数据库 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis_0325" userId="root" password="123456">
</jdbcConnection> <javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver> <!-- javaModelGenerator:生成pojo targetPackage:生成的pojo放在哪个包
targetProject:放在哪个工程下
-->
<javaModelGenerator targetPackage="com.atguigu.bean"
targetProject=".\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator> <!--sqlMapGenerator:sql映射文件生成器;指定xml生成的地方 -->
<sqlMapGenerator targetPackage="com.atguigu.dao"
targetProject=".\conf">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator> <!-- javaClientGenerator:dao接口生成的地方 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.atguigu.dao" targetProject=".\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator> <!-- table:指定要逆向生成哪个数据表
tableName="t_cat":表名
domainObjectName="":这个表对应的对象名
-->
<table tableName="t_cat" domainObjectName="Cat"></table>
<table tableName="t_employee" domainObjectName="Employee"></table>
<table tableName="t_teacher" domainObjectName="Teacher"></table> </context>
</generatorConfiguration>
三、测试
public class MBGTest { public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("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);
System.out.println("生成ok了!");
} }
最新文章
- Guidelines for accessing OneDrive from an app
- as3 Loader 加载资源后内存泄露无法释放的问题。
- leetcode:Delete Node in a Linked List
- Codeforces Round #312 (Div. 2) A. Lala Land and Apple Trees 暴力
- python字符串格式化 %操作符 {}操作符---总结
- javascript原型链__proto__属性的理解
- CODING 敏捷实践完全指南
- 关于Django报错django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configure
- zoj 3605
- Linux shell编程 -test
- ionic2启动出现try again later
- No X11 DISPLAY variable was set
- RESTful规范1
- 通过JS页面唤醒app(安卓+ios)
- 一款超级炫酷的编辑代码的插件 Power Mode
- 【spring基础】spring声明式事务详解
- sql server 2008 数据库可疑的解决步骤
- Asp.Net MVC :路由器
- linux—文件目录简单介绍
- Thread中的join使用