spring-mybatis获取mapper的四种方法


项目背景:pojo下面有一个user实体类

​ Dao包下面写了usermapper.xml 和usermapper.interface,其中只有一个方法查询数据库中所有的 用户。

1.用实现类获取这个用户

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
	
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

	<!--注册实现类usermapperimpl-->
    <bean id="userMapper" class="com.kuang.mapper.UserMapperImpl">
        <property name="sqlSession" ref="sqlSession"/>
    </bean>

实现类usermapperImpl:

public class UserMapperImpl implements UserMapper {
    private SqlSessionTemplate sqlSession;

    public List<User> selectAllUser() {
        return sqlSession.getMapper(UserMapper.class).selectAllUser();
    }

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }
}

test测试:

@Test
    public void test2(){
        ApplicationContext app = new ClassPathXmlApplicationContext("spring-dao.xml");
        UserMapperImpl userMapper = app.getBean(UserMapperImpl.class);
        List<User> users = userMapper.selectAllUser();
        for (User user : users) {
            System.out.println(user);
        }
    }

2.SqlSessionDaoSupport获取

public class UserMapperImpl1 extends SqlSessionDaoSupport implements UserMapper {
    public List<User> selectAllUser() {
        return getSqlSession().getMapper(UserMapper.class).selectAllUser();
    }
}

bean的注册:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

<bean id="userimpl1" class="com.kuang.mapper.UserMapperImpl1">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

3.MapperFactoryBean

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
    
<bean id="userimpl" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.kuang.mapper.UserMapper"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

测试:

 @Test
    public void test3(){
        ApplicationContext app = new ClassPathXmlApplicationContext("spring-dao.xml");
        UserMapper userMapper = app.getBean(UserMapper.class);
 		// UserMapper userMapper = app.getBean("userimpl");
        List<User> users = userMapper.selectAllUser();
        for (User user : users) {
            System.out.println(user);
        }
    }

在使用这个MapperFactoryBean方式的时候,通过app有两种方式获取bean,一种是id然后强转,另一种是接口的类型class。

4.MapperScannerConfigurer

xml配置

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.kuang.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

test:

@Test
    public void test4(){
        ApplicationContext app = new ClassPathXmlApplicationContext("spring-dao.xml");
        UserMapper bean = app.getBean(UserMapper.class);
        for (User user : bean.selectAllUser()) {
            System.out.println(user);
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值