mybatis****----自动生成实体类,dao,mapper.xml
前言:
mybatis是目前很为流行的持久层框架,其****更是大大缩减了我们的开发时间。所谓mybatis****,就是mybatis会根据我们设计好的数据表,自动生成pojo、mapper以及mapper.xml。
以下我将通过eclipse插件方式来解说mybatis的****
打开eclipse,点击工具栏的help>eclipseMarkespace 在里面输入mybatis,点击GO之后等待加载完毕,
安装Mybatis Generator 就是mybatis生成器
安装完成重启之后,在新建文件的窗体上,就会看到如图的选项出现,你可以点击他,创建一个xml文件
不过里面都是没有经过配置的
安装完插件,我们现在正式进入mybatis的逆向生成
建立一个web工程,在lib目录下,放入mybatis包与jdbc连接驱动
在webcontent目录下,新建上面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="MySqlContext" targetRuntime="Mybatis3Simple" >
<!-- 禁用生成注释信息 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- url,userId,password根据自己情况改成自己的 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mall"
userId="root"
password="123456">
</jdbcConnection>
<!-- 配置生成实体类的包名也就是pojo -->
<javaModelGenerator targetPackage="com.web.entry" targetProject="generator/src">
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 配置生成的Mapper.xml文件的位置 -->
<sqlMapGenerator targetPackage="com.web.mapper" targetProject="generator/src"/>
<!-- 配置生成Mapper接口的配置,类型设置成XMLMAPPER是将xml与接口完成分离 -->
<!-- targetProject有关的相对路径中需要增加当前的项目名称 -->
<javaClientGenerator targetPackage="com.web.dao" type="XMLMAPPER" targetProject="generator/src"/>
<!-- 使用通配符%匹配数据库中所有的表,所有表都有主键自增的id,针对当前的数据库配置MySql -->
<table tableName="%" ></table>
<!-- 如果不用数据库的表名作为实体的类名,则需要自己将每一个表进行如下配置-->
<!-- <table tableName="数据库表名" domainObjectName="生成实体类的名字"> </table> -->
</context>
</generatorConfiguration>
这里的targetProject要说一下,就是路径一定要是项目名/src的形式,否则会说项目不存在
在配置好这个文件后,在xml文件上右键run as找到Run Mybatis Generrator执行,控制套显示成功完成
我们看到,他真的自动生成出来,不同我们劳心劳累的去敲