使用MyBatis_Generator生成Dto、Dao、Mapping

Home / Article 百晓生 2019-2-27 2584

什么是 MyBatis ?

MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。

百度百科:MyBatis 

MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。2013年11月迁移到Github。

iBATIS一词来源于“internet”和“abatis”的组合,是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAOs)

由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,所以查资料发现有现成的工具可以自动生成底层模型类、Dao接口类甚至Mapping映射文件。

生成配置文件

新建一个空的XML配置文件,名称可以随便取,这里以generatorConfig.xml为名。最好将这个文件放在下载后的lib目录中,如图:

其中mysql的驱动可以随便放在非中文路径的地方,这里为了方便就放在lib目录下。

自动生成最重要的就是配置文件的书写,现在就开始介绍generatorConfig.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>
<!-- 数据库驱动-->
	<classPathEntry	location="mysql-connector-java-5.1.25-bin.jar"/>
	<context id="DB2Tables"	targetRuntime="MyBatis3">
		<commentGenerator>
			<property name="suppressDate" value="true"/>
			<!-- 是否去除自动生成的注释 true:是 : false:否 -->
			<property name="suppressAllComments" value="true"/>
		</commentGenerator>
		<!--数据库链接URL,用户名、密码 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/bbs" userId="root" password="">
		</jdbcConnection>
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false"/>
		</javaTypeResolver>
		<!-- 生成模型的包名和位置-->
		<javaModelGenerator targetPackage="bbs.model" targetProject="src">
			<property name="enableSubPackages" value="true"/>
			<property name="trimStrings" value="true"/>
		</javaModelGenerator>
		<!-- 生成映射文件的包名和位置-->
		<sqlMapGenerator targetPackage="bbs.mapping" targetProject="src">
			<property name="enableSubPackages" value="true"/>
		</sqlMapGenerator>
		<!-- 生成DAO的包名和位置-->
		<javaClientGenerator type="XMLMAPPER" targetPackage="bbs.dao" targetProject="src">
			<property name="enableSubPackages" value="true"/>
		</javaClientGenerator>
		<!-- 要生成哪些表-->
		<table tableName="bbs_user" domainObjectName="UserDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
		<table tableName="bbs_thread" domainObjectName="ThreadDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
		<table tableName="bbs_post" domainObjectName="PostDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
	</context>
</generatorConfiguration>

需要通过CMD命令行方式来运行,首先可以先准备一个运行的脚本,这里使用的脚本是:java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

需要注意的是:mybatis-generator-core-1.3.2.jar为下载的对应版本的jar,generatorConfig.xml 为配置文件名,如果不为这个可以在这里进行修改。

生成成功后进到src目录下,可以看到已经生成了对应的model、dao、mapping

本文链接:https://www.it72.com/12495.htm

推荐阅读
最新回复 (0)
返回