mybatis映射-04-04
2019-04-05 本文已影响0人
封_绝
mybatis中的mapping和dao放在一起,这样才能完成方法和查询语句的映射❌
只要配置了SqlSessionFactoryBean的映射xml路径和扫描器的路径即可。
只有dao和mapping放在一起才能完成查询结果与类属性的映射?
如果dao目录下还有impl文件夹则会自动加载impl文件夹中的实现类,如果该接口没有实现,但是有impl文件夹也是不会用系统自带的实现方法,导致无法调用对应的xml文件。
还是有无法获取,可能是配置文件出了问题
<!-- myBatis文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
<property name="mapperLocations" value="classpath:com/bota/bean/**/*.xml" />
</bean>
突然发现有这么一个东西,问题的关键所在:明明已经删掉了,但是还能使用,说明是out目录下存在,只是源文件被删了而已,所以需要rebuilt一下。
![](https://img.haomeiwen.com/i8494809/bb69a2b13ad2777e.png)
rebuild会发现找不到class,如果没有堆栈信息,tomcat开不了,报错为:SEVERE: One or more listeners failed to start. Full details will be found in the appropriate container log file之类的。
解决办法:在WEB-INF下面新建一个logging.properties文件,里面写上:
org.apache.catalina.core.ContainerBase.[Catalina].level=INFO
org.apache.catalina.core.ContainerBase.[Catalina].handlers=java.util.logging.ConsoleHandler
就可以输出报错信息了。
一般都是说找不到类,只要删掉整个out目录,然后rebuild一下就可以restart服务器了。
接着报错:
05-Apr-2019 00:45:20.125 严重 [http-nio-8080-exec-6] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.bota.dao.UserDao.selectUserByUserNumber] with root cause
java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.bota.dao.UserDao.selectUserByUserNumber
要么xml中的id没有和方法名一样
要么dao的名字没有和xml的名字一样
要么就是指向不对,value指向mapping地址
<!-- myBatis文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
<property name="mapperLocations" value="classpath:com/bota/bean/mapping/*.xml" />
</bean>
根据日期查活动报错
05-Apr-2019 04:25:30.970 严重 [http-nio-8080-exec-4] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 3] with root cause
org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 3
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:338)
at com.sun.proxy.$Proxy13.selectOne(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:154)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:75)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:38)
at com.sun.proxy.$Proxy22.selectCourseByCreateTime(Unknown Source)
at com.bota.service.impl.CourseServiceImpl.selectCourseByCreateTime(CourseServiceImpl.java:225)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
//CourseMapper.xml
<select id="selectCourseByCreateTime" resultMap="BaseResultMap" parameterType="java.lang.String">
select *
from course
WHERE createtime = #{createTime,jdbcType=TIMESTAMP};
</select>
//dao
public Course selectCourseByCreateTime(String createTime);
TooManyResultsException结果太多,查询出来不止一条记录,所以出问题了,查询出一条语句没有问题。
只要改一下接口就可以了
//dao
public List<Course> selectCourseByCreateTime(String createTime);