spring整合mybatis
spring整合mybatis
用逆向工程生成mybatis的一些配置文件,mapper.xml出现了一些错误。

将该文件下所有路劲修改正确即可。
持久层继承SqlSessionDaoSupport对象
继承SqlSessionDaoSupport,在配置文件中注入给SqlSessionDaoSupport中的sqlSessionFactory方法注入sqlSessionFactoryBean
dao层
点击查看代码
public class UserDaoIpml2 extends SqlSessionDaoSupport implements UserDao {
@Override
public void addUsers(Users users) {
}
@Override
public List<Users> selectUsersAll() {
//获取代理对象
UsersMapper usersMapper = this.getSqlSessionTemplate().getMapper(UsersMapper.class);
List<Users> users = usersMapper.selectByExample(new UsersExample());
return users;
}
}
配置文件
点击查看代码
<bean id="usersService2" class="com.bjsxt.service.impl.UsersServiceImpl2"> <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/> </bean>
MapperScannerConfigurer 对象的使用
用于以自动扫描形式来配置 MyBatis 中映射器对象,可以通过配置包路径来自动扫描包接口生成映射器对象。
dao层
点击查看代码
public class UserDaoImpl3 implements UserDao {
private UsersMapper usersMapper;
public UsersMapper getUsersMapper() {
return usersMapper;
}
public void setUsersMapper(UsersMapper usersMapper) {
this.usersMapper = usersMapper;
}
@Override
public void addUsers(Users users) {
}
@Override
public List<Users> selectUsersAll() {
//MapperScannerConfigurer 对象在配置文件中映射给userMapper,
return this.usersMapper.selectByExample(new UsersExample());
}
}
创建配置文件
点击查看代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
>
<!-- 配置解析properties工具类-->
<context:property-placeholder location="dp.properties"/>
<!--配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
</bean>
<!--配置 SqlSessionFactoryBean 整合mybatis 加载数据源 pojo对象映射 mapper映射文件-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.bjsxt.pojo"/>
<!-- 如果接口与映射配置文件在同一个包中,那么 mapperLocations 属性则不 需要配置。-->
<!-- <property name="mapperLocations" value="com/bjsxt/mapper/*.xml"/>-->
</bean>
<!--配置 MapperScannerConfigurer -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描后会自动生成一个mapper的Bean对象在容器中 -->
<property name="basePackage" value="com.bjsxt.mapper"/>
</bean>
<bean id="userdao3" class="com.bjsxt.dao.impl.UserDaoImpl3">
<!-- 注入自动生成的bean对象-->
<property name="usersMapper" ref="usersMapper"/>
</bean>
<bean id="userService3" class="com.bjsxt.service.impl.UsersServiceImpl">
<property name="userDao" ref="userdao3"/>
</bean>
</beans>
查看配置文件spring容器管理的bean对象
点击查看代码
/**
* 查看applicationContext中的bean对象
*/
public class LookSpringBean {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext2.xml");
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
for (String str : beanDefinitionNames) {
System.out.println(str);
}
}
}

浙公网安备 33010602011771号