JavaWeb轻量级开发框架-Mybatis如何自动生成代码

SSM三大框架是javaWeb轻量级开发中比较常用的一套开发框架,分别是Spring,SpringMVC,Mybatis。

在MyBatis的使用过程中,如果不会使用工具自动生成代码,那将会是一个比较麻烦的过程:首先,需要写Dao,接着写Mapping(映射xml文件),最后,还需要写Model(Bean)...

那么如果会使用工具来生成代码,将会是一个比较轻松的过程,大大减少了手写代码的频率,接下来,我就根据网上看到的结合自己使用的过程来讲述如何使用工具来生成代码。

1.首先,需要下载自动生成代码的工具,可以使用积分通过我分享的CSDN资源下载,也可以去我的网站上免费下载使用(文末注有),下载后解压将会得到如下文件

2.修改以上文件中的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>
	<!-- 数据库驱动包位置 -->
	<classPathEntry location="D:\generator\mysql-connector-java-5.1.34.jar" /> 
	<!-- <classPathEntry location="C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" />-->
	<context id="DB2Tables" targetRuntime="MyBatis3">
		<commentGenerator>
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<!-- 数据库链接URL、用户名、密码 -->
		 <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhots:3306/music?characterEncoding=utf8" userId="root" password="ZCLZY"> 
		<!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa">-->
		</jdbcConnection>
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>
		<!-- 生成模型的包名和位置 -->
		<javaModelGenerator targetPackage="andy.model" targetProject="D:\generator\src">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
		<!-- 生成的映射文件包名和位置 -->
		<sqlMapGenerator targetPackage="andy.mapping" targetProject="D:\generator\src">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>
		<!-- 生成DAO的包名和位置 -->
		<javaClientGenerator type="XMLMAPPER" targetPackage="andy.dao" targetProject="D:\generator\src">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		<!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
		<table tableName="kb_city" domainObjectName="KbCity" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
		<!-- <table tableName="course_info" domainObjectName="CourseInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
		<table tableName="course_user_info" domainObjectName="CourseUserInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> -->
	</context>
</generatorConfiguration>

如上文件中需要修改的地方有:数据库链接URL、用户名、密码,包名,以及要生成的那些表

3.在cmd命令台中切换到generator文件夹下

输入java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite后即可成功生成需要的代码

注:以上工具使用的前提是你的电脑上已经安装了JDK以及mysql或其他数据库,并且已经在数据库中建立了数据库及其表,这样工具就可以为这个表创建你需要的代码文件了!

你可以到我的个人网站中下载工具553影院LongBro博客的干货街

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值