随笔 - 395  文章 - 3  评论 - 49  阅读 - 85万

@Autowired(required = false)

标记在 方法上的时候,它会根据类型去spring容器中寻找 对于的形参并且注入。

 

复制代码
 1 @Repository(value="userDao")
 2 public class UserDaoImpl extends SqlSessionDaoSupport implements IUserDao{
 3     
 4 //    @Autowired
 5 //    @Qualifier(value="sqlsessionFactory11")
 6 //    private SqlSessionFactory asqlSessionFactory;
 7 //    
 8     public User getOne(String uid) {
 9         return this.getSqlSession().selectOne("cn.us.mybatis.getOne",uid);
10     }
11 
12     public List<User> getAllUsers() {
13         Object obj=this.getSqlSession();
14         return this.getSqlSession().selectList("cn.us.mybatis.getAllUsers");
15     }
16 
17 }
复制代码

 

子类也可以,如果配置多个就会报错。

 

SqlSessionDaoSupport 中有以下方法
1   @Autowired(required = false)
2   public final void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
3     if (!this.externalSqlSession) {
4       this.sqlSession = new SqlSessionTemplate(sqlSessionFactory);
5     }
6   }

 

 

只能一个bean,根据 type 类型去匹配的

1     <bean id="sqlsessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
2         <property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
3         <property name="dataSource" ref="dataSource"></property>
4     </bean>

 

 因为 required=false,如果spring找不到的话会赋空值进去

如果没有@Autowired(required = false)的话,那么UserDaoImpl 不能继承

SqlSessionDaoSupport 了,可以用组合的方式 

然后
@Autowired
private SqlSessionFactory sqlSessionFactory;

然后在spring配置文件中,<bean id=...>配置上 SqlSessionFactory 

posted on   wf110  阅读(11983)  评论(1编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
< 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

点击右上角即可分享
微信分享提示