MyBatis学习---使用MyBatis_Generator生成Dto、Dao、Mapping
由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,所以查资料发现有现成的工具可以自动生成底层模型类、Dao接口类甚至Mapping映射文件。
一、建立表结构
CREATE TABLE 'user' (
'id' varchar(50) NOT NULL,
'username' varchar(18) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
'password' varchar(18) DEFAULT NULL,
'email' varchar(50) DEFAULT NULL,
'name' varchar(18) DEFAULT NULL,
'sex' varchar(2) DEFAULT NULL,
'birthday' varchar(50) DEFAULT NULL,
'address' varchar(500) DEFAULT NULL,
'tel' varchar(18) DEFAULT NULL,
'qq' varchar(18) DEFAULT NULL,
'image' varchar(50) DEFAULT NULL,
'sfjh' varchar(1) DEFAULT NULL,
'sfzx' varchar(1) DEFAULT NULL,
'sfhf' varchar(1) DEFAULT NULL,
'sfpl' varchar(1) DEFAULT NULL,
'sffx' varchar(1) DEFAULT NULL,
PRIMARY KEY ('id')
) ENGINE=InnoDB DEFAULT CHARSET=utf-8;
二、下载mybatis-generator-core
进入:http://code.google.com/p/mybatis/
选择Downloads,再选择MyBatis Generator Tool下载即可。
三、生成配置文件
新建一个空的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.0.6-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/test" userId="test" password="test">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="test.model" targetProject="src">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="test.mapping" targetProject="src">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="test.dao" targetProject="src">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成哪些表-->
<table tableName="about" domainObjectName="AboutDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="user" domainObjectName="UserDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="syslogs" domainObjectName="SyslogsDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
1、其中需要注意的有数据库驱动、数据库URL、用户名、密码、生成模型的包名和位置、生成映射文件的包名和位置、生成DAO的包名和位置以及最后需要生成的表名和对应的类名。
四、运行
需要通过CMD命令行方式来运行,首先可以先准备一个运行的脚本,这里使用的脚本是:java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
需要注意的是:mybatis-generator-core-1.3.2.jar为下载的对应版本的jar,generatorConfig.xml 为配置文件名,如果不为这个可以在这里进行修改。
启动cmd进入到“F:\soft\mybatis-generator-core-1.3.2\lib”这个目录下,如图:
生成成功后进到src目录下,可以看到已经生成了对应的model、dao、mapping,如图:
下面可以看看生成后的UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="test.dao.UserDtoMapper" >
<resultMap id="BaseResultMap" type="test.model.UserDto" >
<id column="id" property="id" jdbcType="VARCHAR" />
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="email" property="email" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="sex" property="sex" jdbcType="VARCHAR" />
<result column="birthday" property="birthday" jdbcType="VARCHAR" />
<result column="address" property="address" jdbcType="VARCHAR" />
<result column="tel" property="tel" jdbcType="VARCHAR" />
<result column="qq" property="qq" jdbcType="VARCHAR" />
<result column="image" property="image" jdbcType="VARCHAR" />
<result column="sfjh" property="sfjh" jdbcType="VARCHAR" />
<result column="sfzx" property="sfzx" jdbcType="VARCHAR" />
<result column="sfhf" property="sfhf" jdbcType="VARCHAR" />
<result column="sfpl" property="sfpl" jdbcType="VARCHAR" />
<result column="sffx" property="sffx" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
id, username, password, email, name, sex, birthday, address, tel, qq, image, sfjh,
sfzx, sfhf, sfpl, sffx
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
<include refid="Base_Column_List" />
from user
where id = #{id,jdbcType=VARCHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
delete from user
where id = #{id,jdbcType=VARCHAR}
</delete>
<insert id="insert" parameterType="test.model.UserDto" >
insert into user (id, username, password,
email, name, sex, birthday,
address, tel, qq, image,
sfjh, sfzx, sfhf, sfpl,
sffx)
values (#{id,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR}, #{birthday,jdbcType=VARCHAR},
#{address,jdbcType=VARCHAR}, #{tel,jdbcType=VARCHAR}, #{qq,jdbcType=VARCHAR}, #{image,jdbcType=VARCHAR},
#{sfjh,jdbcType=VARCHAR}, #{sfzx,jdbcType=VARCHAR}, #{sfhf,jdbcType=VARCHAR}, #{sfpl,jdbcType=VARCHAR},
#{sffx,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="test.model.UserDto" >
insert into user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="username != null" >
username,
</if>
<if test="password != null" >
password,
</if>
<if test="email != null" >
email,
</if>
<if test="name != null" >
name,
</if>
<if test="sex != null" >
sex,
</if>
<if test="birthday != null" >
birthday,
</if>
<if test="address != null" >
address,
</if>
<if test="tel != null" >
tel,
</if>
<if test="qq != null" >
qq,
</if>
<if test="image != null" >
image,
</if>
<if test="sfjh != null" >
sfjh,
</if>
<if test="sfzx != null" >
sfzx,
</if>
<if test="sfhf != null" >
sfhf,
</if>
<if test="sfpl != null" >
sfpl,
</if>
<if test="sffx != null" >
sffx,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=VARCHAR},
</if>
<if test="username != null" >
#{username,jdbcType=VARCHAR},
</if>
<if test="password != null" >
#{password,jdbcType=VARCHAR},
</if>
<if test="email != null" >
#{email,jdbcType=VARCHAR},
</if>
<if test="name != null" >
#{name,jdbcType=VARCHAR},
</if>
<if test="sex != null" >
#{sex,jdbcType=VARCHAR},
</if>
<if test="birthday != null" >
#{birthday,jdbcType=VARCHAR},
</if>
<if test="address != null" >
#{address,jdbcType=VARCHAR},
</if>
<if test="tel != null" >
#{tel,jdbcType=VARCHAR},
</if>
<if test="qq != null" >
#{qq,jdbcType=VARCHAR},
</if>
<if test="image != null" >
#{image,jdbcType=VARCHAR},
</if>
<if test="sfjh != null" >
#{sfjh,jdbcType=VARCHAR},
</if>
<if test="sfzx != null" >
#{sfzx,jdbcType=VARCHAR},
</if>
<if test="sfhf != null" >
#{sfhf,jdbcType=VARCHAR},
</if>
<if test="sfpl != null" >
#{sfpl,jdbcType=VARCHAR},
</if>
<if test="sffx != null" >
#{sffx,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="test.model.UserDto" >
update user
<set >
<if test="username != null" >
username = #{username,jdbcType=VARCHAR},
</if>
<if test="password != null" >
password = #{password,jdbcType=VARCHAR},
</if>
<if test="email != null" >
email = #{email,jdbcType=VARCHAR},
</if>
<if test="name != null" >
name = #{name,jdbcType=VARCHAR},
</if>
<if test="sex != null" >
sex = #{sex,jdbcType=VARCHAR},
</if>
<if test="birthday != null" >
birthday = #{birthday,jdbcType=VARCHAR},
</if>
<if test="address != null" >
address = #{address,jdbcType=VARCHAR},
</if>
<if test="tel != null" >
tel = #{tel,jdbcType=VARCHAR},
</if>
<if test="qq != null" >
qq = #{qq,jdbcType=VARCHAR},
</if>
<if test="image != null" >
image = #{image,jdbcType=VARCHAR},
</if>
<if test="sfjh != null" >
sfjh = #{sfjh,jdbcType=VARCHAR},
</if>
<if test="sfzx != null" >
sfzx = #{sfzx,jdbcType=VARCHAR},
</if>
<if test="sfhf != null" >
sfhf = #{sfhf,jdbcType=VARCHAR},
</if>
<if test="sfpl != null" >
sfpl = #{sfpl,jdbcType=VARCHAR},
</if>
<if test="sffx != null" >
sffx = #{sffx,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="test.model.UserDto" >
update user
set username = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
email = #{email,jdbcType=VARCHAR},
name = #{name,jdbcType=VARCHAR},
sex = #{sex,jdbcType=VARCHAR},
birthday = #{birthday,jdbcType=VARCHAR},
address = #{address,jdbcType=VARCHAR},
tel = #{tel,jdbcType=VARCHAR},
qq = #{qq,jdbcType=VARCHAR},
image = #{image,jdbcType=VARCHAR},
sfjh = #{sfjh,jdbcType=VARCHAR},
sfzx = #{sfzx,jdbcType=VARCHAR},
sfhf = #{sfhf,jdbcType=VARCHAR},
sfpl = #{sfpl,jdbcType=VARCHAR},
sffx = #{sffx,jdbcType=VARCHAR}
where id = #{id,jdbcType=VARCHAR}
</update>
</mapper>
接下来就可以将这三个目录拷贝到对应项目的目录中,如果需要新增自己的方法可以修改dao类。
本文转自:http://blog.csdn.net/wyc_cs/article/details/9023117
MyBatis学习---使用MyBatis_Generator生成Dto、Dao、Mapping的更多相关文章
- mybatis generator.xml 配置 自动生成model,dao,mapping
generator.xml文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE gener ...
- MyBatis学习4---使用MyBatis_Generator生成Dto、Dao、Mapping
由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,所以查资料发现有现成的工具可以自动生成底层模型类.Dao接口类甚至Mappi ...
- 使用MyBatis_Generator生成Dto、Dao、Mapping
由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,所以查资料发现有现成的工具可以自动生成底层模型类.Dao接口类甚至Mappi ...
- Mybatis 自动从数据库生成entity,mapping,dao接口
1.下载需要的jar包 mybatis-generator-core-1.3.2.jar,mysql-connector-java-5.1.39.jar 2.把上面的jar包放到某个目录,并在该目录下 ...
- MyBatis 使用Generator自动生成Model , Dao, mapper
最近 我新建了一 个maven 项目,使用的是spring + springmvc + mybatis框架. 听说Mybatis可以自动生成model和mapper以及dao层,我就从网上查了查资 ...
- Mybatis学习(2)原始dao开发和使用mapper接口代理开发
基础知识: 1).SqlSessionFactoryBuilder: 通过SqlSessionFactoryBuilder创建会话工厂SqlSessionFactory.将SqlSessionFact ...
- Mybatis学习笔记汇总(包括源码和jar包)
博客整理 Mybatis学习笔记(一)--对原生jdbc中问题的总结 Mybatis学习笔记(二)--Mybatis框架 Mybatis学习笔记(三)--入门程序 MyBatis学习笔记(四)--入门 ...
- mybatis 学习笔记(三):mapper 代理开发 dao 层
mybatis 学习笔记(三):mapper 代理开发 dao 层 优势 通过使用mapper 代理,我们可以不需要去编写具体的实现类(使用 getMapper() 方法自动生成),只需编写接口即可, ...
- MyBatis学习--mybatis开发dao的方法
简介 使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper接口开发方法. 主要概念介绍: MyBatis中进行Dao开发时候有几个重要的类,它们是SqlSessionFac ...
随机推荐
- 安装tar.bz2文件
(1) 解包 – tar jxvf softname-10.0.1.tar.gz -C /usr/src/(-C指的是把文件解压到后面的路径下,此处可以不选) – cd /usr/src/softna ...
- 侃侃前端MVC设计模式
前言 前端的MVC,近几年一直很火,大家也都纷纷讨论着,于是乎,抽空总结一下这个知识点.看了些文章,结合实践略作总结并发表一下自己的看法. 最初接触MVC是后端Java的MVC架构,用一张图来表示之— ...
- Android实现圆形圆角图片
本文主要使用两种方法实现图形圆角图片 自定View加上使用Xfermode实现 Shader实现 自定View加上使用Xfermode实现 /** * 根据原图和变长绘制圆形图片 * * @param ...
- 二、JavaScript语言--JS基础--JavaScript进阶篇--数组
1.什么事数组 我们知道变量用来存储数据,一个变量只能存储一个内容.假设你想存储10个人的姓名或者存储20个人的数学成绩,就需要10个或20个变量来存储,如果需要存储更多数据,那就会变的更麻烦.我们用 ...
- oracle 10g 学习之单行函数(5)
目标 通过本章学习,您将可以: l SQL中不同类型的函数. l 在 SELECT 语句中使用字符,数字和日期函数. l 描述转换型函数的用途. 字符函数 字符函数分为大小写控制函数和字符控制函 ...
- jquery easy ui 1.3.4 窗口,对话框,提示框(5)
5.1.window(窗口) 窗口我们在程序中会大量的使用,比如我们的添加.编辑都可以使用窗口实现,与winform的程序非常的类似.下面的代码是创建一个基本的窗口 $(function () { $ ...
- jQuery Mobile 基础(第二章)
1.可折叠块: <div data-role="collapsible"> <h1>点击我 - 我可以折叠!</h1> <p>我是可 ...
- Java Hour 51 CheckStyle
Plan List: 1 mysql 相关 2 java code style 安装体验 1 直接从eclipse install new software 安装,莫名奇妙失败,提示为缺少依赖项. 这 ...
- Java Hour 39 Maven ( 1 )
有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. Hour 39 Maven 1 Perhaps you are running ...
- 登录Cloudera Manager时报错org.hibernate.exception.GenericJDBCException: Could not open connection
去Cloudera Server上边看了一下日志: cat /opt/cloudera-manager/log/cloudera-scm-server/cloudera-scm-server.log ...