MyBatis詳解如何實(shí)現(xiàn)Dao層接口
傳統(tǒng)開發(fā)方式
編寫UserDao接口
public interface UserMapper { public List<User> findAll() throws IOException; }
編寫UserDaompl實(shí)現(xiàn)
public class UserMapperImp implements UserMapper { @Override public List<User> findAll() throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = build.openSession(); List<User> users=sqlSession.selectList("User.findAll"); sqlSession.close(); return users; } }
傳統(tǒng)測(cè)試方法
public class ServiceCode { public static void main(String[] args) throws IOException { UserMapper userMapper = new UserMapperImp(); List<User> all = userMapper.findAll(); System.out.println(all); } }
我們發(fā)現(xiàn)使用傳統(tǒng)的開發(fā)方式,每次都要實(shí)現(xiàn)接口的代碼編寫,這樣也有很多的代碼冗余,也是相當(dāng)?shù)姆爆?,下面,MyBatis為我們提供了代理開發(fā)的方法,我們只需要提供接口,MyBatis框架就可以根據(jù)接口定義為我們實(shí)現(xiàn)。
代理開發(fā)方法
代理開發(fā)方式介紹
采用MyBatis的代理開發(fā)方式實(shí)現(xiàn)Dao層的開發(fā),這種方式是我們后面進(jìn)入企業(yè)的主流。
Mapper接口開發(fā)方法只需要程序員編寫Mapper接口(相當(dāng)與Dao接口),由MyBatis框架根據(jù)接口定義創(chuàng)建接口的動(dòng)態(tài)代理對(duì)象,代理對(duì)象方法體同上邊Dao接口實(shí)現(xiàn)類方法。
Mapper接口開發(fā)需要遵循一下規(guī)范:
- 1、Mapper.xml文件中的
namespace
與mapper
接口的全限定名相同 - 2、Mapper接口方法名和Mapper.xml中定義的每個(gè)
Statement
的id相同 - 3、Mapper接口方法的輸入?yún)?shù)類型和Mapper.xml中定義的每個(gè)sql的
parameterType
的類型相同 - 4、Mapper接口方法的輸出參數(shù)類型和Mapper.xml中定義的每個(gè)sql的
resultType
的類型相同
編寫UserMapper接口
測(cè)試代理方法
接口:
public interface UserMapper { public List<User> findAll() ; }
測(cè)試代碼:
public class Test { public static void main(String[] args) throws Exception { InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); //獲得MyBatis框架生產(chǎn)的UserMapper接口的實(shí)現(xiàn)類 UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<user> all = mapper.findAll(); for (user user : all) { System.out.println(user); } }
根據(jù)id查詢:
接口:
public interface UserMapper { //根據(jù)id查詢 public User findById(int id); }
測(cè)試:
UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user=mapper.findById(2); System.out.println(user);
到此這篇關(guān)于MyBatis詳解如何實(shí)現(xiàn)Dao層接口的文章就介紹到這了,更多相關(guān)MyBatis Dao層接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot默認(rèn)包掃描機(jī)制與默認(rèn)配置文件詳解
這篇文章主要給大家詳細(xì)介紹了SpringBoot默認(rèn)包掃描機(jī)制的原理和示例,以及SpringBoot默認(rèn)配置文件介紹,文章通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08Java超詳細(xì)講解接口的實(shí)現(xiàn)與用法
Java接口是一系列方法的聲明,是一些方法特征的集合,一個(gè)接口只有方法的特征沒有方法的實(shí)現(xiàn),因此這些方法可以在不同的地方被不同的類實(shí)現(xiàn),而這些實(shí)現(xiàn)可以具有不同的行為2022-04-04SpringBoot3和ShardingSphere5框架實(shí)現(xiàn)數(shù)據(jù)分庫分表
這篇文章主要介紹了SpringBoot3和ShardingSphere5框架實(shí)現(xiàn)數(shù)據(jù)分庫分表的相關(guān)資料,需要的朋友可以參考下2023-08-08Java基于Lock的生產(chǎn)者消費(fèi)者模型示例
這篇文章主要介紹了Java基于Lock的生產(chǎn)者消費(fèi)者模型,結(jié)合實(shí)例形式分析了java基于鎖機(jī)制的生產(chǎn)者消費(fèi)者模型相關(guān)實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2018-08-08Java中增強(qiáng)for循環(huán)的實(shí)現(xiàn)原理和坑詳解
增強(qiáng)的for循環(huán)是在傳統(tǒng)的for循環(huán)中增加的強(qiáng)大的迭代功能的循環(huán),是在jdk1.5之后提出來的。下面這篇文章主要給大家介紹了關(guān)于Java中增強(qiáng)for循環(huán)的實(shí)現(xiàn)原理和坑的相關(guān)資料,需要的朋友可以參考下2018-04-04java項(xiàng)目中使用 Lombok遇到的問題小結(jié)
這篇文章主要介紹了java項(xiàng)目中使用 Lombok遇到的問題小結(jié),需要的朋友可以參考下2018-07-07