MyBatis學習4---使用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這個文件的具體內容:
- <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
- <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類。
MyBatis學習4---使用MyBatis_Generator生成Dto、Dao、Mapping