实体类?Dao接口?Mapper映射文件?都别写了!!!用这种方法就可以
大家好,我是雄雄,前兩天一直在解決使用idea整合SSM的報(bào)錯(cuò)問(wèn)題,今天,給大家?guī)?lái)的是如何使用插件快速生成實(shí)體類、接口以及映射文件,相信你看過(guò)本文之后對(duì)你有很大的幫助。
前言
每次我們?cè)趯?xiě)代碼的時(shí)候,都會(huì)對(duì)應(yīng)數(shù)據(jù)表來(lái)寫(xiě)實(shí)體類,以及接口中常用的增刪改查方法,包括Mapper映射文件,一個(gè)兩個(gè)表還可以,如果數(shù)據(jù)表多的情況下,可見(jiàn)工作量也會(huì)很大,對(duì)此有沒(méi)有一種很好的解決辦法呢?答案當(dāng)然是有的,下面我們就來(lái)看看如何讓程序自動(dòng)生成這些代碼呢?
01
下載所需的工具
需要下載“mybatis反向生成工具”(可在本公眾號(hào)【雄雄的小課堂】后臺(tái)回復(fù):mybatis反向生成工具,即可免費(fèi)下載)。
02
修改配置文件generatorConfig.xml
打開(kāi)下載的工具,進(jìn)入lib文件夾,右擊用記事本(其他編輯器也可以)打開(kāi)generatorConfig.xml文件,如下所示:
數(shù)據(jù)庫(kù)表設(shè)計(jì)如下:
所在數(shù)據(jù)庫(kù)為:schooldb。
1.加載所對(duì)應(yīng)數(shù)據(jù)庫(kù)的jar,我在這里用的是mysql數(shù)據(jù)庫(kù),所以就得需要mysql的驅(qū)動(dòng)jar文件(jar文件已經(jīng)放至lib目錄中,oracle的也有),將名字寫(xiě)在下面的地方。
<classPathEntry??location="mysql-connector-java-5.1.25-bin.jar"/>??<context?id="DB2Tables"??targetRuntime="MyBatis3">mysql-connector-java-5.1.25-bin.jar就是數(shù)據(jù)庫(kù)的jar包。
2.連接配置(此步驟的作用就是連接數(shù)據(jù)庫(kù)),將對(duì)應(yīng)的driverClass,connectionURL,userId和password寫(xiě)上,如下是我的,你就把用戶名和密碼改成你的就行。
<jdbcConnection driverClass="com.mysql.jdbc.Driver"?connectionURL="jdbc:mysql://localhost:3306/schooldb"?userId="root"?password="root"> </jdbcConnection>3.配置生成實(shí)體類、Dao接口以及映射文件的路徑,我在這放在了org.dao里面了,最后在放在src下面,如下代碼:
<javaModelGenerator?targetPackage="org.entity"?targetProject="src"><!-- 是否在當(dāng)前路徑下新加一層schema,eg:fase路徑com.shkj.pojo, true:com.shkj.pojo.[schemaName] --><property?name="enableSubPackages"?value="true"/><!-- 設(shè)置是否在getter方法中,對(duì)String類型字段調(diào)用trim()方法 --><!-- <property name="trimStrings" value="false"/> --></javaModelGenerator>??<!-- 生成xml文件的路徑,這個(gè)路徑可以自動(dòng)生成,但是必須有src這個(gè)路徑--><sqlMapGenerator?targetPackage="org.dao"?targetProject="src">??<property?name="enableSubPackages"?value="true"/>??</sqlMapGenerator>??<!-- 生成Dao類的路徑,這個(gè)路徑可以自動(dòng)生成,但是必須有src這個(gè)路徑--><javaClientGenerator?type="XMLMAPPER"?targetPackage="org.dao"?targetProject="src">??<property?name="enableSubPackages"?value="true"/>??</javaClientGenerator>4.根據(jù)表生成對(duì)應(yīng)的類名:
<table tableName="BookManage"?domainObjectName="BookManage"?enableCountByExample="false"?enableUpdateByExample="false"?enableDeleteByExample="false"?enableSelectByExample="false"?selectByExampleQueryId="false"></table>注意:這是一個(gè)表,如果有多個(gè)表的話,需要寫(xiě)多個(gè)<table>標(biāo)簽。
03
開(kāi)始生成
打開(kāi)文件中的cmd.bat批處理,雙擊進(jìn)入之后自己會(huì)定位當(dāng)前所在的目錄,然后輸入:java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite ?回車(chē)即可。
如下所示:
最后打開(kāi)文件夾中的src目錄,就會(huì)發(fā)現(xiàn)想要的都在里面。
實(shí)體類:
package?org.entity;import?java.util.Date;public?class?BookManage?{private?Integer bid;private?String bname;private?String bauthor;private?Date btime;private?Integer btype;public?Integer getBid()?{return?bid;}public?void?setBid(Integer bid)?{this.bid = bid;}public?String getBname()?{return?bname;}public?void?setBname(String bname)?{this.bname = bname;}public?String getBauthor()?{return?bauthor;}public?void?setBauthor(String bauthor)?{this.bauthor = bauthor;}public?Date getBtime()?{return?btime;}public?void?setBtime(Date btime)?{this.btime = btime;}public?Integer getBtype()?{return?btype;}public?void?setBtype(Integer btype)?{this.btype = btype;} }Dao接口:
package?org.dao;import?org.entity.BookManage;public?interface?BookManageMapper?{int?deleteByPrimaryKey(Integer bid);int?insert(BookManage record);int?insertSelective(BookManage record);BookManage selectByPrimaryKey(Integer bid);int?updateByPrimaryKeySelective(BookManage record);int?updateByPrimaryKey(BookManage record); }Dao接口所對(duì)應(yīng)的Mapper文件:
<?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="org.dao.BookManageMapper"?><resultMap id="BaseResultMap"?type="org.entity.BookManage"?><id column="bid"?property="bid"?jdbcType="INTEGER"?/><result column="bname"?property="bname"?jdbcType="VARCHAR"?/><result column="bauthor"?property="bauthor"?jdbcType="VARCHAR"?/><result column="btime"?property="btime"?jdbcType="TIMESTAMP"?/><result column="btype"?property="btype"?jdbcType="INTEGER"?/></resultMap><sql id="Base_Column_List"?>bid, bname, bauthor, btime, btype</sql><select id="selectByPrimaryKey"?resultMap="BaseResultMap"?parameterType="java.lang.Integer"?>select <include refid="Base_Column_List"?/>from bookmanagewhere?bid = #{bid,jdbcType=INTEGER}</select><delete id="deleteByPrimaryKey"?parameterType="java.lang.Integer"?>delete from bookmanagewhere?bid = #{bid,jdbcType=INTEGER}</delete><insert id="insert"?parameterType="org.entity.BookManage"?>insert into bookmanage (bid, bname, bauthor, btime, btype)values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{bauthor,jdbcType=VARCHAR}, #{btime,jdbcType=TIMESTAMP}, #{btype,jdbcType=INTEGER})</insert><insert id="insertSelective"?parameterType="org.entity.BookManage"?>insert into bookmanage<trim prefix="("?suffix=")"?suffixOverrides=","?><if?test="bid != null"?>bid,</if><if?test="bname != null"?>bname,</if><if?test="bauthor != null"?>bauthor,</if><if?test="btime != null"?>btime,</if><if?test="btype != null"?>btype,</if></trim><trim prefix="values ("?suffix=")"?suffixOverrides=","?><if?test="bid != null"?>#{bid,jdbcType=INTEGER},</if><if?test="bname != null"?>#{bname,jdbcType=VARCHAR},</if><if?test="bauthor != null"?>#{bauthor,jdbcType=VARCHAR},</if><if?test="btime != null"?>#{btime,jdbcType=TIMESTAMP},</if><if?test="btype != null"?>#{btype,jdbcType=INTEGER},</if></trim></insert><update id="updateByPrimaryKeySelective"?parameterType="org.entity.BookManage"?>update bookmanage<set?><if?test="bname != null"?>bname = #{bname,jdbcType=VARCHAR},</if><if?test="bauthor != null"?>bauthor = #{bauthor,jdbcType=VARCHAR},</if><if?test="btime != null"?>btime = #{btime,jdbcType=TIMESTAMP},</if><if?test="btype != null"?>btype = #{btype,jdbcType=INTEGER},</if></set>where?bid = #{bid,jdbcType=INTEGER}</update><update id="updateByPrimaryKey"?parameterType="org.entity.BookManage"?>update bookmanageset?bname = #{bname,jdbcType=VARCHAR},bauthor = #{bauthor,jdbcType=VARCHAR},btime = #{btime,jdbcType=TIMESTAMP},btype = #{btype,jdbcType=INTEGER}where?bid = #{bid,jdbcType=INTEGER}</update> </mapper>往期精彩
springmvc中引入靜態(tài)太資源js文件的方法
2021-01-20
idea中報(bào)錯(cuò)……的解決方式!
2021-01-19
如何在idea中使用jstl標(biāo)簽庫(kù)
2021-01-18
我去,終于解決了!
2021-01-17
3班的第一次模擬面試
2021-01-16
不能初始化泛型參數(shù)和數(shù)組
2021-01-15
點(diǎn)分享
點(diǎn)點(diǎn)贊
點(diǎn)在看
總結(jié)
以上是生活随笔為你收集整理的实体类?Dao接口?Mapper映射文件?都别写了!!!用这种方法就可以的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 9个最佳电气标识以及如何免费获得[202
- 下一篇: 记得完成寒假作业~