1.问题一:使用mybatis,Bean类不能缺少默认构造函数
否则会出现
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注解