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>两种方法其实差不多,第一种是只能在单个接口的时候使用,如果映射接口很多的话,就需要使用第二种了,这个大家试一下就知道了。
第一次写博客,有什么问题请大家见谅!
- dao层不写实现类容易出现的错误及解决方法
- git命令容易出现的错误以及解决方法
- 正则非贪婪匹配 及 容易出现的错误
- 移植boa出现的错误及解决方法
- Redis安装出现的错误及解决方法
- oracle中出现的错误及解决方法
- 出现错误的解决方法:
- 出现错误的解决方法:
- VB中容易出现的溢出错误
- 运行.js脚本容易出现的错误!
- SSH整合容易出现的错误
- 黑马程序员 JavaScript 容易出现的错误
- javascript编程容易出现的错误
- 玩quartus时容易出现的错误
- IOS APP上传容易出现的错误
- Core Data容易出现的错误
- spring新手容易出现的错误
- ACMer刷题时容易出现的错误
- ES6-字符串扩展-标签模板
- 在Windows下为PHP5.6安装redis扩展
- microsoft edge地址栏无法复制死机解决方法
- 文件上传
- BeautifulSoup库的使用
- dao层不写实现类容易出现的错误及解决方法
- JavaWeb从入门到放弃(1)-配置环境
- MyEclipse中jsp默认编码格式改变
- 一步一步带你安装史上最难安装的 vim 插件 —— YouCompleteMe
- c语言下的约瑟夫环
- android+项目中常见的错误1
- 【BZOJ5020】【THUWC2017】在美妙的数学王国中畅游 LCT 泰勒展开
- 【bzoj1217】[HNOI2003]消防局的设立 贪心
- 分布式锁的解决好办法,此文属转载,请支持作者。