Dao包下面写了usermapper.xml 和usermapper.interface,其中只有一个方法查询数据库中所有的 用户。
<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);
}
}
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>
<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。
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);
}
}