您的位置:首页 > 其它

mybatis入门问题汇总

2017-03-30 17:37 169 查看
1.问题一:使用mybatis,Bean类不能缺少默认构造函数

否则会出现

No constructor found 错误。如下

Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 

### Error querying database.  Cause: org.apache.ibatis.executor.ExecutorException: No constructor found in com.monetware.model.User matching [java.lang.Integer, java.lang.String, java.lang.Integer, java.lang.String]

### The error may exist in com/monetware/model/map/User.xml

### The error may involve com.monetware.dao.UserDao.selectUsers

解决:User实体类,如果用到有参数的构造方法,必须要写明无参构造方法。如果不用有参数的构造方法,可以不用写明无参构造方法

问题二:config配置文件<typeAliases> 别名的配置一定放在最前面 ,不然会报下面的错误

org.apache.ibatis.exceptions.PersistenceException: 

### Error building SqlSession.

### Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance.  Cause: org.xml.sax.SAXParseException; lineNumber: 24; columnNumber: 17; 元素类型为 "configuration" 的内容必须匹配 "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProvider?,mappers?)"。
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:52)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:36)

问题三:

Caused by: org.apache.ibatis.binding.BindingException: Parameter 'offset' not found. Available parameters are [0, 1, param1, param2]

解决://有多个参数的时候需要使用@Param注解

List<User> queryAllByLimit(@Param("offset") int offset,@Param("limit") int limit);

原因://java没有保存形参的记录:queryAllByLimit(int offset,int limit) 会变为-> queryAllByLimit(arg0,arg1)
//有多个参数的时候需要使用@Param注解
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: