Code Bye

ssm整合报错 Mapped Statements collection does not contai

错误:java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.clx.ssm.mapper.UserMapper.save
spring-config.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:p=”http://www.springframework.org/schema/p”
xmlns:context=”http://www.springframework.org/schema/context”
xmlns:tx=”http://www.springframework.org/schema/tx”
xsi:schemaLocation=”
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd”>
<context:annotation-config />

<!– 1. 数据源 : DriverManagerDataSource –>
<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” />
<property name=”username” value=”root” />
<property name=”password” value=”clx” />
</bean>
<!–
2. mybatis的SqlSession的工厂: SqlSessionFactoryBean
dataSource / typeAliasesPackage
–>
<bean id=”sqlSessionFactory” class=”org.mybatis.spring.SqlSessionFactoryBean”>
<property name=”dataSource” ref=”datasource”/>
<property name=”typeAliasesPackage” value=”com.clx.ssm.bean”/>
</bean>
<!–
3. mybatis自动扫描加载Sql映射文件 : MapperScannerConfigurer
sqlSessionFactory / basePackage
–>
<bean id=”config” class=”org.mybatis.spring.mapper.MapperScannerConfigurer”>
<property name=”basePackage” value=”com.clx.ssm.mapper”/>
<property name=”sqlSessionFactory” ref=”sqlSessionFactory”/>
</bean>
<!– 4. 事务管理 : DataSourceTransactionManager –>
<bean id=”manager” class=”org.springframework.jdbc.datasource.DataSourceTransactionManager”>
<property name=”dataSource” ref=”datasource”/>
</bean>
<!– 5. 使用声明式事务 –>
<tx:annotation-driven transaction-manager=”manager” />
<bean id=”userService” class=”com.clx.ssm.service.UserServiceImpl”></bean>

<!– action方法的注入 –>
<bean id=”userAction” class=”com.clx.ssm.action.RegisterAction”>
<property name=”userService” ref=”userService”></property>
</bean>
</beans>
UserServiceImpl.java
package com.clx.ssm.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.clx.ssm.bean.User;
import com.clx.ssm.mapper.UserMapper;
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;

public void delete(int id) {
// TODO Auto-generated method stub
}
public List<User> findAll() {
// TODO Auto-generated method stub
return null;
}
public void save(User user) {
userMapper.save(user);
}

public void update(User user) {
// TODO Auto-generated method stub
}
}
userMapper.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE mapper PUBLIC “-//mybatis.org//DTD Mapper 3.0//EN”
“http://mybatis.org/dtd/mybatis-3-mapper.dtd”>
<!–namespace必须是接口的全类名  –>
<mapper namespace=”com.clx.ssm.mapper.UserMapper”>
<!– id必须是接口对应的方法–>
<insert id=”save”>
insert into user(username,password)
values(#{username},#{password})
</insert>

<update id=”update” parameterType=”User”>
update user
set username = #{username},
password = #{password}
where id = #{id}
</update>
<delete id=”delete” parameterType=”int”>
delete from user
where id = #{id}
</delete>
<select id=”findAll” resultMap=”User”>
select *
from user
</select>
</mapper>
UserMapper.java
package com.clx.ssm.mapper;
import java.util.List;
import com.clx.ssm.bean.User;
public interface UserMapper {
void save(User user);
void update(User user);
void delete(int id);
List<User> findAll();
}

解决方案

50

2. mybatis的SqlSession的工厂: SqlSessionFactoryBean
dataSource / typeAliasesPackage
–>
<bean id=”sqlSessionFactory” class=”org.mybatis.spring.SqlSessionFactoryBean”>
<property name=”dataSource” ref=”datasource”/>
<property name=”typeAliasesPackage” value=”com.clx.ssm.bean”/>
</bean>
<!–
3. mybatis自动扫描加载Sql映射文件 : MapperScannerConfigurer
sqlSessionFactory / basePackage
–>
<bean id=”config” class=”org.mybatis.spring.mapper.MapperScannerConfigurer”>
<property name=”basePackage” value=”com.clx.ssm.mapper”/>
<property name=”sqlSessionFactory” ref=”sqlSessionFactory”/>
</bean>
在 2 里设置映射文件的路径,3不会加载映射文件,只会自动创建 Mapper

50

http://mybatis.github.io/spring/zh/

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明ssm整合报错 Mapped Statements collection does not contai