Mybatis generatorConfig.xml生成文件內(nèi)容解析

使用mybatis generator生成XX.java,XXExample.java,XXmapper.java,XXmapper.xml四個(gè)數(shù)據(jù)庫(kù)表映射文件

一、Example實(shí)例解析

mybatis的逆向工程中會(huì)生成實(shí)例及實(shí)例對(duì)應(yīng)的XXExample.java,XXExample.java包含了對(duì)象的常用查詢方法

XXExample有三個(gè)內(nèi)部類,GeneratedCriteria,Criteria,Criterion

創(chuàng)建內(nèi)部類Criteria用于查詢,代碼示例如下看表達(dá):

XXExample example =newXXExample();//新建XX對(duì)象的example對(duì)象進(jìn)行查詢,XXExample繼承XX

XXExample.Criteria c = example.Criteria();//new一個(gè)當(dāng)前example的內(nèi)部類

Criteriac.andXXIsNull();//設(shè)置查詢條件,某值為空

c.andXXIsNotNull();//設(shè)置查詢條件,某值不空

//使用mapper進(jìn)行查詢

List data =newXXMapper.selectByExample(example);


二、mapper接口中的方法解析

mapper.java的函數(shù)及方法,按命名的字面意思理解用途。

countByExample,帶example的一般都是使用example查詢條目數(shù)。

selectByPrimaryKey使用id查詢結(jié)果集? mapper中的方法和xml中的sql語(yǔ)句是對(duì)應(yīng)的,

如果在mapper中傳入example對(duì)象,xml中會(huì)解析example的值,取值拼寫(xiě)sql語(yǔ)句進(jìn)行查詢

傳入String類型的key,id等,xml直接取值拼寫(xiě)sql進(jìn)行查詢?


三、XML文件解析

舉例一個(gè)xml文件中的一條語(yǔ)句

<select id="countByExample" parameterType="com.ctvit.cportal.core.config.entity.XXExample" resultType="java.lang.Integer">

?????select count(*) from t_app_second_config_item

????<if test="_parameter != null">

????????<include refid="Example_Where_Clause"/>

? ? </if>

</select>

語(yǔ)句的id與mapper.java中的方法名對(duì)應(yīng),

parameterType是傳入?yún)?shù)的類型,resultType是返回結(jié)果的類型,傳參和返回有很多種情況,另開(kāi)文章講

這樣對(duì)照起來(lái)的話,我們可以自主在mapper.java和mapper.xml中增加或刪除相應(yīng)的查詢語(yǔ)句,以供調(diào)用

比如增加一條新增版本批量復(fù)制語(yǔ)句“copyBatchByVersion”

mapper.java中增加方法

int copyBatchByVersion(String old_version,String new_version) thorws SQLException;

/*根據(jù)舊版本號(hào)old_version查找記錄

復(fù)制記錄

同時(shí)將其中的old_version替換為new_version

返回值為復(fù)制成功的條數(shù)

*/

mapper.xml中增加語(yǔ)句

<insert id="copyBatchByVersion">

?????insert into t_xx

? ? (XX_id,xx_value,xx_version)

? ? (select XX_id,xx_value,#{1})

? ? from t_xx where xx_version = #{0})

? </insert>

其中#{0},#{1}代表輸入的第1,2個(gè)參數(shù)

四、應(yīng)用舉例

1.查詢

① selectByPrimaryKey()

User user = XxxMapper.selectByPrimaryKey(100);//相當(dāng)于select * from user where id = 100

② selectByExample() 和 selectByExampleWithBLOGs()

UserExample example =new UserExample();

Criteria criteria = example.createCriteria();

criteria.andUsernameEqualTo("wyw");

criteria.andUsernameIsNull();

example.setOrderByClause("username asc,email desc");

Listlist = XxxMapper.selectByExample(example);

//相當(dāng)于:select * from user where username = 'wyw' and? username is null order by username asc,email desc

注:在iBator逆向工程生成的文件XxxExample.java中包含一個(gè)static的內(nèi)部類Criteria,Criteria中的方法是定義SQL 語(yǔ)句where后的查詢條件。

2.插入數(shù)據(jù)

①insert()

User user =new User();

user.setId("dsfgsdfgdsfgds");

user.setUsername("admin");

user.setPassword("admin")

user.setEmail("wyw@163.com");

XxxMapper.insert(user);

//相當(dāng)于:insert into user(ID,username,password,email) values ('dsfgsdfgdsfgds','admin','admin','wyw@126.com');

3.更新數(shù)據(jù)

①updateByPrimaryKey()

User user =new User();

user.setId("dsfgsdfgdsfgds");

user.setUsername("wyw");

user.setPassword("wyw");

user.setEmail("wyw@163.com");

XxxMapper.updateByPrimaryKey(user);

//相當(dāng)于:update user set username='wyw', password='wyw', email='wyw@163.com' where id='dsfgsdfgdsfgds'

②updateByPrimaryKeySelective()

User user =new User();

user.setId("dsfgsdfgdsfgds");

user.setPassword("wyw");

XxxMapper.updateByPrimaryKey(user);

//相當(dāng)于:update user set password='wyw' where id='dsfgsdfgdsfgds'

③ updateByExample() 和 updateByExampleSelective()

UserExample example =new UserExample();

Criteria criteria = example.createCriteria();

criteria.andUsernameEqualTo("admin");

User user =new User();

user.setPassword("wyw");

XxxMapper.updateByPrimaryKeySelective(user,example);

//相當(dāng)于:update user set password='wyw' where username='admin'

updateByExample()更新所有的字段,包括字段為null的也更新,建議使用 updateByExampleSelective()更新想更新的字段

4.刪除數(shù)據(jù)

①deleteByPrimaryKey()

XxxMapper.deleteByPrimaryKey(1);//相當(dāng)于:delete from user where id=1

②deleteByExample()

UserExample example =new UserExample();

Criteria criteria = example.createCriteria();

criteria.andUsernameEqualTo("admin");

XxxMapper.deleteByExample(example);

//相當(dāng)于:delete from user where username='admin'

5.查詢數(shù)據(jù)數(shù)量

①countByExample()

UserExample example =new UserExample();

Criteria criteria = example.createCriteria();

criteria.andUsernameEqualTo("wyw");intcount = XxxMapper.countByExample(example);

//相當(dāng)于:select count(*) from user where username='wyw'

原文鏈接:https://www.cnblogs.com/difs/p/9234900.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容