Spring和Mybatis整合

https://blog.csdn.net/qq_42371269/article/details/83505269

方法一: 通过 XXXMapper.xml 映射文件整合

使用 SqlSession 进行操作

public class SpringMybatisTest {

/*
* 1. 创建spring容器
       根据xml文件应用程序Context容器(上下文)
       classpath指配置文件的位置, 起点有java, resources. 写路径时相对这个起点去写
* */
static ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring.xml");

@Test
public void test1(){

    /* 得到 SqlSession 对象*/
    SqlSession sqlSession = (SqlSession) context.getBean("sqlSession");

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("start",0);
    map.put("size",5);

    // 调用 Mapper映射文件里的方法
    List<Product> list = sqlSession.selectList("com.chen.mapper.ProductMapper.selectPage", map);

    for (Product product : list) {
        System.out.println(product);
    }
}

}

方法二:通过 Mapper接口的方式整合

1.搜索有哪些 mapper 实现类,把mapper接口自动配置成 spring 中的 –>
<bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">

    <!-- name="basePackage":(起始)包名, 从这个包开始扫描-->
    <property name="basePackage" value="com.chen.mapper"/>

</bean>
2.编写 mapper接口

3. 使用 XXXMapper.xml 配合 Mapper接口 进行 动态 SQL 的拼接

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值