java應(yīng)用開發(fā)之Mybatis通過Mapper代理自定義接口的實(shí)現(xiàn)
如何實(shí)現(xiàn)?主要分為以下兩步驟
- 1.通過 Mapper 代理實(shí)現(xiàn)⾃定義接口
- 2.編寫與方法相對應(yīng)的 Mapper.xml
1.自定義接口AccountRepository
package repository;
import entity.Account;
import java.util.List;
public interface AccountRepository {
public int save(Account account);
public int update(Account account);
public int deleteById(long id);
public List<Account> findAll();
public Account findById(long id);
}
2.創(chuàng)建接⼝對應(yīng)的 Mapper.xml,定義接口方法對應(yīng)的 SQL 語句。
statement 標(biāo)簽可根據(jù) SQL 執(zhí)⾏的業(yè)務(wù)選擇 insert、delete、update、select。 MyBatis 框架會根據(jù)規(guī)則⾃動創(chuàng)建接⼝實(shí)現(xiàn)類的代理對象
規(guī)則:
- Mapper.xml 中 namespace 為接⼝的全類名。
- Mapper.xml 中 statement 的 id 為接⼝中對應(yīng)的⽅法名。
- Mapper.xml 中 statement 的 parameterType 和接⼝中對應(yīng)⽅法的參數(shù)類型⼀致。
- Mapper.xml 中 statement 的 resultType 和接⼝中對應(yīng)⽅法的返回值類型⼀致。
<?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="repository.AccountRepository">
<insert id="save" parameterType="entity.Account">
insert into t_account(username,password,age) values (#{username},#{password},#{age});
</insert>
<update id="update" parameterType="entity.Account">
update t_account set username=#{username},password=#{password},age=#{age} where id=#{id};
</update>
<delete id="deleteById" parameterType="long">
delete from t_account where id=#{id};
</delete>
<select id="findAll" resultType="entity.Account">
select * from t_account;
</select>
<select id="findById" parameterType="long" resultType="entity.Account">
select * from t_account where id =#{id};
</select>
</mapper>
3.在 config.xml 中注冊 AccountRepository.xml
<mappers>
<mapper resource="mapper/AccountMapper.xml"></mapper>
<mapper resource="repository/AccountRepository.xml"></mapper>
</mappers>
4.調(diào)用接⼝的代理對象完成相關(guān)的業(yè)務(wù)操作
package Test;
import entity.Account;
import org.apache.ibatis.io.ResolverUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import repository.AccountRepository;
import java.io.InputStream;
import java.util.List;
public class Test1 {
public static void main(String[] args) {
InputStream inputStream= ResolverUtil.Test.class.getClassLoader().getResourceAsStream("config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
AccountRepository accountRepository=sqlSession.getMapper(AccountRepository.class);
// List<Account> list=accountRepository.findAll();
// for(Account account:list){
// System.out.println(account);
// }
// sqlSession.close();
//添加
// Account account=new Account(3L,"wangwu","555555",122);
// accountRepository.save(account);
// sqlSession.commit();
//通過id查找對象
// Account account=accountRepository.findById(3L);
// System.out.println(account);
// sqlSession.close();
//通過id改對象
// Account account=accountRepository.findById(2L);
// account.setUsername("alibaba");
// account.setPassword("12345678");
// account.setAge(11);
// int result=accountRepository.update(account);
// sqlSession.commit();
// System.out.println(result);
// sqlSession.close();
//刪除
int result=accountRepository.deleteById(3L);
sqlSession.commit();
System.out.println(result);
sqlSession.close();
}
}
以上就是java應(yīng)用開發(fā)之Mybatis通過Mapper代理自定義接口的實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于Mybatis通過Mapper代理自定義接口的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
關(guān)于JSON.toJSONString()和Gson.toJson()方法的比較
本文介紹了兩種將Java對象轉(zhuǎn)換為JSON字符串的方法:阿里的`JSON.toJSONString()`和谷歌的`Gson.toJson()`,通過一個示例,展示了當(dāng)使用繼承關(guān)系且子類覆蓋父類字段時,`Gson`會報錯,而`JSON`可以正常運(yùn)行,作者建議在處理JSON相關(guān)操作時使用阿里的`JSON`類2024-11-11
基于Java實(shí)現(xiàn)圖片相似度對比的示例代碼
很多時候我們需要將兩個圖片進(jìn)行對比,確定兩個圖片的相似度。本文將利用Java和OpenCV庫實(shí)現(xiàn)圖片相似度對比,感興趣的可以動手嘗試一下2022-07-07
java實(shí)現(xiàn)兩個線程交替打印的實(shí)例代碼
在本篇文章里小編給大家整理的是一篇關(guān)于java實(shí)現(xiàn)兩個線程交替打印的相關(guān)知識點(diǎn)內(nèi)容,有需要的朋友們參考下。2019-12-12
使用Java實(shí)現(xiàn)查找并移除字符串中的Emoji
Emoji 實(shí)際上是 UTF-8 (Unicode) 字符集上的特殊字符,這篇文章主要介紹了如何使用Java實(shí)現(xiàn)查找并移除字符串中的Emoji,感興趣的可以了解下2024-03-03
JavaEE Filter敏感詞過濾的方法實(shí)例詳解
我們無論是在聊天還是在留言時,都有一些信息不希望別人看到。那么如果過濾這些關(guān)鍵詞呢?下面小編給大家分享JavaEE Filter敏感詞過濾的方法實(shí)例詳解,感興趣的朋友一起學(xué)習(xí)吧2016-05-05
劍指Offer之Java算法習(xí)題精講鏈表專項訓(xùn)練
跟著思路走,之后從簡單題入手,反復(fù)去看,做過之后可能會忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會發(fā)現(xiàn)質(zhì)的變化2022-03-03

