Spring框架整合mybais框架-注入映射器實現

通過上面一個案例,我們能夠看到,每次在執行具體的某個方法的時候,我們都會創建一個映射器,這是非常麻煩的,這就是我們所看到的UserMapperImpl.java,那麽我們能不能將他省略掉了,將創建映射器的方法交給Spring的ioc容器進行管理,答案是肯定的

aplicationContext.xml

复制代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:aop="http://www.springframework.org/schema/aop" 
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xmlns:context="http://www.springframework.org/schema/context"
 8     xsi:schemaLocation="http://www.springframework.org/schema/beans
 9     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
10     http://www.springframework.org/schema/aop
11     http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
12     http://www.springframework.org/schema/tx
13     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
14     http://www.springframework.org/schema/context
15     http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
16 
17     <!--配置数据源 -->
18     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
19         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
20         <property name="url"
21             value="jdbc:mysql://localhost:3306/smbms?useUnicode=true&amp;characterEncoding=utf-8"></property>
22         <property name="username" value="root"></property>
23         <property name="password" value="root"></property>
24 
25     </bean>
26     <!--配置SqlSessionFactoryBean -->
27     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
28         <!--获取到你的数据源 -->
29         <property name="dataSource" ref="dataSource"></property>
30         <!--获取到mybatis的配置文件  注意这里使用的是value属性 -->
31         <property name="configLocation" value="classpath:mybatis-config.xml"></property>
32         <!--使用下面这种方式获取sql文件  -->
33         <property name="mapperLocations">
34             <list>
35                 <value>classpath:cn/smbms/dao/**/*.xml</value>
36             </list>
37         </property> 
38     </bean>
39     
40     <!--配置 SqlSessionTemplate  用它来执行数据库的各种操作    使用继承SqlSessionDaoSupport方式的话,就不用获取SqlSessionTemplate类了-->
41 <!--     <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
42        操作数据库的时候,引用数据库的连接  通过这个SqlSessionTemplate类的构造方法
43        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
44     </bean> -->
45     
46     <!--將映射器的實現交給spring的ioc容器進行管理,這時候,可以將UserMapperImpl類刪除掉也是可以的 -->
47     <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
48     <property name="mapperInterface" value="cn.smbms.dao.user.UserMapper"></property>
<!-- 为什么还需要配置会话工厂,主要的原因是获取sqlSessionTemplate操作数据库 -->
49 <property name="SqlSessionFactory" ref="sqlSessionFactory"></property> 50 </bean> 51 52 </beans>
复制代码

這時候,我們可以將UserMapperImpl.java類進行刪除,刪除之後的項目結構;

 

 最總的運行結果:

 

 

  

posted on   ~码铃薯~  阅读(136)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示