Spring整合Mybatis-第2种方法

Spring整合Mybatis-第2种方法

概述

本文主要讲述利用继承SqlSessionDaoSupport抽象类的方法实现简化整合Mybatis的流程,本文仅介绍增量部分,全量请结合上篇Spring整合Mybatis

实践

UserMapperImpl2.java

package com.kuang.dao;

import com.kuang.pojo.User;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import java.util.List;

public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper  {
    @Override
    public List<User> getUsers() {
        return getSqlSession().getMapper(UserMapper.class).getUsers();
    }
}

application-source.xml

    <bean id="userMapper2" class="com.kuang.dao.UserMapperImpl2">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>

MyTest.java

import com.kuang.dao.UserMapper;
import com.kuang.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.util.List;

public class MyTest {
    @Test
    public void test(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application-source.xml");
        UserMapper userMapper2 = context.getBean("userMapper2", UserMapper.class);
        List<User> users = userMapper2.getUsers();
        for (User user : users) {
            System.out.println(user);
        }
    }
}

posted @ 2022-07-30 21:05  Oh,mydream!  阅读(24)  评论(0编辑  收藏  举报