Spring + Mybatis 结合报错
本帖最后由 clear_shadow 于 2012-12-27 17:54:10 编辑 rt。我在搭建一个struts2+spring+mybatis+mysql的web环境。在搭建好之后,可以使用mybatis访问数据库,检索出数据。但当我使用spring与mybatis结合后,就报错了。
错误发生在我打开session的时候
------------------------------------------
public boolean checkUser(String userName, String password) {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
UserExample example = new UserExample();
example.clear();
example.or().andUsernameEqualTo(userName).andPasswordEqualTo(password);
List<User> lst = mapper.selectByExample(example);
return !lst.isEmpty();
} finally {
sqlSession.close();
}
}
------------------------------------------
错误信息如下。
------------------------------------------
org.apache.ibatis.exceptions.PersistenceException:
### Error opening session. Cause: java.lang.NullPointerException
### Cause: java.lang.NullPointerException
------------------------------------------
spring的配置文件(applicationContext.xml)
------------------------------------------
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 配置要扫描的包 -->
<context:component-scan base-package="com.test" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
<!-- 定义数据源连接 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test_for_ssi"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="maxActive" value="100"/>
<property name="maxIdle" value="30"/>
<property name="maxWait" value="500"/>
<property name="defaultAutoCommit" value="true"/>
</bean>
<!-- 定义全局的事务控制 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启注解方式声明事务 -->
<tx:annotation-driven/>
<!-- 定义SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath*:com/test/xml/*.xml"/>
<property name="typeAliasesPackage" value="com.mybatis.model"/>
</bean>
<!-- 配置SqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- 自动扫描mapper,允许自动注入(根据类型匹配),不需要逐个配置mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.test.dao"/>
</bean>
</beans>
------------------------------------------
mybatis的配置文件(mybatis-config.xml)由于使用spring自动注入了,就不需要再配置mybatis了
------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- <environments default="development"> -->
<!-- <environment id="development"> -->
<!-- <transactionManager type="JDBC"/> -->
<!-- <dataSource type="POOLED"> -->
<!-- <property name="driver" value="com.mysql.jdbc.Driver"/> -->
<!-- <property name="url" value="jdbc:mysql://127.0.0.1:3306/test_for_ssi"/> -->
<!-- <property name="username" value="root"/> -->
<!-- <property name="password" value="root"/> -->
<!-- </dataSource> -->
<!-- </environment> -->
<!-- </environments> -->
<!-- <mappers> -->
<!-- <mapper resource="com/test/xml/UserMapper.xml"/> -->
<!-- </mappers> -->
</configuration>
------------------------------------------
[解决办法]
@Service
public class MongoServiceImpl implements MongoService
{
private static Logger logger = LoggerFactory.getLogger(MongoServiceImpl.class);
private ClientInfoMapper clientMapper;
@Autowired
public MongoServiceImpl(ClientInfoMapper clientMapper)
{
this.clientMapper = clientMapper;
}