mybatis基础知识

Xml中遍历List

<if test="list != null and list.size() > 0">
    and status in
    <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
        #{item,jdbcType=VARCHAR}
    </foreach>
</if>

resultType和resultMap的区别

相同:

MyBatis中在查询进行select映射的时候,返回类型为resultType、resultMap

不同:

  • resultType是直接表示返回类型的(对应着我们的model对象中的实体)
  • resultMap则是对外部ResultMap的引用(提前定义了db和model之间的隐射key–>value关系),但是resultType跟resultMap不能同时存在。

当我们在项目中添加了mapper.xml文件,就需要把这些mapper扫描进去
在创建SqlSessionFactory的时候,加了以下配置:

PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
bean.setMapperLocations(resolver.getResources("classpath*:mapper/*Mapper.xml"));
bean.setTypeAliasesPackage("com.test.maper");

而且注意setTypeAliasesPackage参数不支持通配符*,如果有多个包可以通过","等分割

在实际项目中使用:

@Autowired
private DataSource dataSource;

@Bean("sqlSessionFactory")
public SqlSessionFactory buildSqlSessionFactory() throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    bean.setMapperLocations(resolver.getResources("classpath*:mapper/*Mapper.xml"));
    bean.setPlugins(parameterHandlerInterceptor());
    return bean.getObject();
}

如果需要加载依赖传递过来的jar包中的mapper目录下的xml,classpath:mapper/.xml 修改为classpath:mapper/*.xml

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

信仰_273993243

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值