spring-mybatis获取mapper的四种方法

文章目录

      • 1.用实现类获取这个用户
      • 2.SqlSessionDaoSupport获取
      • 3.MapperFactoryBean
      • 4.MapperScannerConfigurer

项目背景: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&useUnicode=true&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>

	
    <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&useUnicode=true&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&useUnicode=true&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);
        }
    }

你可能感兴趣的:(mybayis)