您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“spring mybatis獲取mapper的方式有哪些”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
項(xiàng)目背景:
pojo下面有一個(gè)user實(shí)體類
Dao包下面寫了usermapper.xml 和usermapper.interface,其中只有一個(gè)方法查詢數(shù)據(jù)庫(kù)中所有的用戶。
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf8"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> <!--注冊(cè)實(shí)現(xiàn)類usermapperimpl--> <bean id="userMapper" class="com.kuang.mapper.UserMapperImpl"> <property name="sqlSession" ref="sqlSession"/> </bean>
實(shí)現(xiàn)類usermapperImpl:
public class UserMapperImpl implements UserMapper { private SqlSessionTemplate sqlSession; public List<User> selectAllUser() { return sqlSession.getMapper(UserMapper.class).selectAllUser(); } public void setSqlSession(SqlSessionTemplate sqlSession) { this.sqlSession = sqlSession; } }
test測(cè)試:
@Test public void test2(){ ApplicationContext app = new ClassPathXmlApplicationContext("spring-dao.xml"); UserMapperImpl userMapper = app.getBean(UserMapperImpl.class); List<User> users = userMapper.selectAllUser(); for (User user : users) { System.out.println(user); } }
public class UserMapperImpl1 extends SqlSessionDaoSupport implements UserMapper { public List<User> selectAllUser() { return getSqlSession().getMapper(UserMapper.class).selectAllUser(); } }
bean的注冊(cè):
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <bean id="userimpl1" class="com.kuang.mapper.UserMapperImpl1"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf8"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <bean id="userimpl" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.kuang.mapper.UserMapper"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean>
測(cè)試:
@Test public void test3(){ ApplicationContext app = new ClassPathXmlApplicationContext("spring-dao.xml"); UserMapper userMapper = app.getBean(UserMapper.class); // UserMapper userMapper = app.getBean("userimpl"); List<User> users = userMapper.selectAllUser(); for (User user : users) { System.out.println(user); } }
在使用這個(gè)MapperFactoryBean方式的時(shí)候,通過(guò)app有兩種方式獲取bean,一種是id然后強(qiáng)轉(zhuǎn),另一種是接口的類型class。
xml配置
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf8"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.kuang.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean>
test:
@Test public void test4(){ ApplicationContext app = new ClassPathXmlApplicationContext("spring-dao.xml"); UserMapper bean = app.getBean(UserMapper.class); for (User user : bean.selectAllUser()) { System.out.println(user); } }
從mybatis3.4.0開始加入了@Mapper注解,目的就是為了不再寫mapper映射文件(那個(gè)xml寫的是真的無(wú)語(yǔ)。。。)。很惡心的一個(gè)事實(shí)是源碼中并沒(méi)有對(duì)于這個(gè)注解的詳細(xì)解釋
在 Spring 程序中,Mybatis 需要找到對(duì)應(yīng)的 mapper,在編譯的時(shí)候動(dòng)態(tài)生成代理類,實(shí)現(xiàn)數(shù)據(jù)庫(kù)查詢功能,所以我們需要在接口上添加 @Mapper 注解。
@Mapper public interface UserDao { ... }
但是,僅僅使用@Mapper注解,我們會(huì)發(fā)現(xiàn),在其他變量中依賴注入,IDEA 會(huì)提示錯(cuò)誤,但是不影響運(yùn)行(親測(cè)~)。
因?yàn)槲覀儧](méi)有顯式標(biāo)注這是一個(gè) Bean,IDEA 認(rèn)為運(yùn)行的時(shí)候會(huì)找不到實(shí)例注入,所以提示我們錯(cuò)誤。
如下圖,會(huì)有紅色波浪線。
盡管這個(gè)錯(cuò)誤提示并不影響運(yùn)行,但是看起來(lái)很不舒服,所以我們可以在對(duì)應(yīng)的接口上添加 bean 的聲明,如下:
@Repository // 也可以使用@Component,效果都是一樣的,只是為了聲明為bean @Mapper public interface UserDao { @Insert("insert into user(account, password, user_name) " + "values(#{user.account}, #{user.password}, #{user.name})") int insertUser(@Param("user") User user) throws RuntimeException; }
基于注解的開發(fā)也有其他手段幫助 Mybatis 找到 mapper,那就是 @MapperScan 注解,可以在啟動(dòng)類上添加該注解,自動(dòng)掃描包路徑下的所有接口。
@SpringBootApplication @MapperScan("com.scut.thunderlearn.dao") public class UserEurekaClientApplication { public static void main(String[] args) { SpringApplication.run(UserEurekaClientApplication.class, args); } }
“spring mybatis獲取mapper的方式有哪些”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。