mapper接口注入兩種方式詳解
這篇文章主要介紹了mapper接口注入兩種方式詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
1.使用模板方式:
<!--使用模板類實(shí)現(xiàn)mybatis -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
<constructor-arg name="executorType" value="BATCH"></constructor-arg>
<constructor-arg name="exceptionTranslator" ref="myBatisExceptionTranslator"></constructor-arg>
</bean>
后臺的調(diào)用:
public bookServiceImpl implements bookService{
@Autowired
public SqlSessionTemplate sqlSession;//注入sqlsessionTemplate
public List<book> getBookInformation(book param){
logger.info("getBookInformation()"+param.toString());
bookMapper mapper = sqlSession.getMapper(bookMapper.class);//調(diào)用mapper接口
List<book> bookList=mapper.queryBookInfo(param);
return bookList;
}
}
2.使用掃描接口方式:
<!--<!– 5.配置mybatisDao接口掃描MapperScannerConfigurer –>-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.alice.dao"></property>
</bean>
后臺調(diào)用:
public class UserServiceImpl implements IUserService {
@Autowired
private UserMapper mapper; //直接注入mapper dao接口的bean
public List<User> selectAll(String keyword1,String keyword2) {
return mapper.selectAll(keyword1,keyword2);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring JDK動態(tài)代理實(shí)現(xiàn)過程詳解
這篇文章主要介紹了Spring JDK動態(tài)代理實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02
MyBatis使用<foreach>標(biāo)簽報錯問題及解決
這篇文章主要介紹了MyBatis使用<foreach>標(biāo)簽報錯問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
基于spring如何實(shí)現(xiàn)事件驅(qū)動實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于基于spring如何實(shí)現(xiàn)事件驅(qū)動的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用spring具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Java輸入三個整數(shù)并把他們由小到大輸出(x,y,z)
這篇文章主要介紹了輸入三個整數(shù)x,y,z,請把這三個數(shù)由小到大輸出,需要的朋友可以參考下2017-02-02
使用RocketMQTemplate發(fā)送帶tags的消息
這篇文章主要介紹了使用RocketMQTemplate發(fā)送帶tags的消息,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
新建springboot項(xiàng)目時,entityManagerFactory報錯的解決
這篇文章主要介紹了新建springboot項(xiàng)目時,entityManagerFactory報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
SpringBoot+Redis使用AOP防止重復(fù)提交的實(shí)現(xiàn)
本文主要介紹了SpringBoot+Redis使用AOP防止重復(fù)提交的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07

