dao层不写实现类容易出现的错误及解决方法

来源:互联网 发布:匡恩网络 招聘 编辑:程序博客网 时间:2024/06/16 21:59

        这次碰到这个问题费了一番功夫,就想记下来,为以后提个醒,也希望大家能够看后少走弯路。

        我配置的是spring+springMVC+mybatis框架,开始也是借鉴别人的代码,看到人家dao层不用写实现类就能直接映射mapper里的方法,我也想试试,可是一直报错,org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.dao.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)},一直找不到原因。

        接下来先把部分代码和配置文件粘出来,

        首先是spring-mybatis.xml文件:

<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">    <property name="url" value="${mysql.url}"></property>    <property name="username" value="${mysql.username}"></property>    <property name="password" value="${mysql.password}"></property>    <property name="driverClassName" value="${mysql.driverClassName}"></property></bean><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">    <property name="dataSource" ref="ds"></property>    <property name="mapperLocations" value="classpath:mybatis/userMapper.xml"></property>    <property name="typeAliasesPackage" value="com.entity"></property></bean><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    <property name="dataSource" ref="ds"></property></bean><tx:annotation-driven transaction-manager="txManager"/>
 接下来是dao层接口:

public interface UserMapper {    /**     * 判断用户登录     * @param userName 登录名     * @return usr 查询到的用户     */    User selectUserByName(String userName);}
 再下来就是userMapper.xml文件了:

<mapper namespace="com.dao.UserMapper">       <resultMap type="User" id="userResultMap">        <id property="userId" column="id"/>        <result property="userName" column="name"/>        <result property="password" column="password"/>        <result property="mail" column="mail"/>    </resultMap>    <select id="selectUserByName" parameterType="java.lang.String" resultType="User">     SELECT id AS userId,name AS userName,password,mail     FROM user     WHERE name = #{userName}    </select>    </mapper>
  最后是UserServiceImpl文件:

@Service("userService")public class UserServiceImpl implements IUserService {    private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);    @Autowired    private UserMapper userMapper;    /**     * 验证用户登录     *     * @param userName 登录名     * @param password 登录密码     * @return code为1,message为"账户或密码不正确,请重新输入"     * code为2,message为"输入正确,请等待跳转"     * code为3,message为"密码不正确,请重新输入"     */    public ResultMessage checkUserLogin(String userName, String password) {       ...}
    其实看到这好多大神应该就能找到问题所在了,是的,就是spring-mybatis.xml中出现的问题,我也是后来才知道的,如果mybtis中不写dao层的实现类,spring如何为service层注入dao的实例呢,找到两种方法可以解决。

    一种是在spring-mybatis.xml中加入

<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">    <property name="mapperInterface" value="com.dao.UserMapper" />    <property name="sqlSessionFactory" ref="sqlSessionFactory" /></bean>
     还有一种是在spring-mybatis.xml中加入

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="com.dao"></property></bean>
     两种方法其实差不多,第一种是只能在单个接口的时候使用,如果映射接口很多的话,就需要使用第二种了,这个大家试一下就知道了。

     第一次写博客,有什么问题请大家见谅!

原创粉丝点击