本篇文章不会系统的讲解知识也不会深入讲,只会说明一个很具体的问题,属于随手记录型。
如何用 ModelAndView 进行重定向
//返回一个ModelAndView
public ModelAndView updateInfo(Info info) {
ModelAndView modelAndView = new ModelAndView();
//更新
infoService.updateSelect(info);
// 设置视图,到更新成功页面
modelAndView.setViewName("redirect:/xxxx/Update.jsp");
return modelAndView;
}
//或者 参数里来一个
public ModelAndView updateInfo(ModelAndView modelAndView) {
// 设置视图,到更新成功页面
modelAndView.setViewName("redirect:/xxxx/Update.jsp");
}
只是一种(其他还有很多)
SSM工程启动报错:foreach的集合,不是var而是item
Ignoring bean creation exception on FactoryBean type check:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'certificateArticleMapper' defined in file [xxx...\dao\CertificateArticleMapper.class]:
Unsatisfied dependency expressed through bean property 'sqlSessionFactory': :
Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring-datasource.xml]:
Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [xxx...m\oracle\entity\InfoMapper.xml]';
nested exception is org.apache.ibatis.builder.BuilderException: Error creating document instance.
Cause: org.xml.sax.SAXParseException; lineNumber: 616; columnNumber: 74; 必须为元素类型 "foreach" 声明属性 "var"。; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring-datasource.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [D:\Program Files (x86)\apache-tomcat-8.5.38\wtpwebapps\o2o\WEB-INF\classes\com\oracle\entity\InfoMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 616; columnNumber: 74; 必须为元素类型 "foreach" 声明属性 "var"。
InfoMapper.xml 里面mybatis的 foreach 写错 了,,不是var ,是 item。
mybatis报错:Available parameters are [classesIds, pager, param1, param2]
Type Exception Report
Message Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'array' not found. Available parameters are [classesIds, pager, param1, param2]
检查参数,只允许 classesIds, pager, param1, param2,,,这些
注意Mapper里方法的参数,建议直接用@Parma
注解显示声明 这个集合到底叫什么
这里的代码显示声明为了 classesIds,在Mapper.xml里写错了
数据更新 涉及到多个表,采用哪种方案?(1对1 ,1对多 ,多对多 )
只涉及单表增删改
然后,在service层里 ,,混合调用 吗?(分别调用各自的 dao)
(会有两次查询数据库)
还是
在Mapper里,直接连接查询,,?
一次性得到,所有数据
答:
一般情况下:
插入,删除,修改。 ----表各自操作,分多次 改变
查询 时 尽量一次搞定(利用连接)
在多对多的时候 ,要分两次查询(如:用户-订单 ,一组用户的订单)
mybatis注意resultType和resultMap
<select id="countBySearchPager" resultType="java.lang.Integer">
select count(1)
xxx...
</select>
返回int
注意第一行 resultType="java.lang.Integer"
莫要错写成了 resultMap="xxx"
resultType 返回一个值(列)的类型
resultMap多列对应的类型
本该写resultMap,却写成了resultType会如何?
[DEBUG]2019-12-10 14:35:39[org.springframework.beans.factory.support.DefaultListableBeanFactory:1426] Ignoring bean creation exception on FactoryBean type check: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userGeneRecordMapper' defined in file [D:\Program Files (x86)\apache-tomcat-8.5.38\wtpwebapps\o2o\WEB-INF\classes\com\oracle\model\dao\UserGeneRecordMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory': : Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring-datasource.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [D:\Program Files (x86)\apache-tomcat-8.5.38\wtpwebapps\o2o\WEB-INF\classes\com\oracle\entity\ClassesMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'BaseResultMap'. Cause: java.lang.ClassNotFoundException: Cannot find class: BaseResultMap; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring-datasource.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [D:\Program Files (x86)\apache-tomcat-8.5.38\wtpwebapps\o2o\WEB-INF\classes\com\oracle\entity\ClassesMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'BaseResultMap'. Cause: java.lang.ClassNotFoundException: Cannot find class: BaseResultMap
注意看:
org.apache.ibatis.type.TypeException: Could not resolve type alias 'BaseResultMap'. Cause: java.lang.ClassNotFoundException: Cannot find class: BaseResultMap;
无法解析BaseResultMap,找不到BaseResultMap
mybatis插入时返回主键id
- useGeneratedKeys=“true”
<insert id="insertBook" useGeneratedKeys="true" keyProperty="id">
insert into t_book (b_name,author) values (#{name},#{author});
</insert>
- SELECT LAST_INSERT_ID()
<insert id="insertBook">
insert into t_book (b_name,author) values (#{name},#{author});
<selectKey keyProperty="id" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
</insert>
SELECT LAST_INSERT_ID() 可以放到插入语句不同位置(前、后),对应上一次插入主键,本次插入主键
一般用第一种
mybatis里 集合默认值
- 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
- 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
- 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可
123
举个小栗子
int 数组
int countClassesAllChildByPager(int[] classesIds);
对应
<select id="countClassesAllChildByPager" resultType="java.lang.Integer">
select count(1)
from info
where info.class_id in
<foreach item="id" collection="array" open="(" close=")" separator=",">
#{id}
</foreach>
</select>
SSM配置拦截器
<!-- 拦截器配置 -->
<mvc:interceptors>
<!-- 自动获取IP地址 -->
<mvc:interceptor>
<mvc:mapping path="/user/**"/> <!-- 拦截路径 -->
<bean class="com.oracle.interceptor.IPInterceptor"></bean>
</mvc:interceptor>
<!-- 用户登录拦截器 -->
<mvc:interceptor>
<mvc:mapping path="/business/**"/>
<mvc:mapping path="/user/personalCenter/*"/>
<mvc:mapping path="/user/infoPublic/selectClasses"/>
<mvc:mapping path="/user/infoPublic/toInfoEdit"/>
<mvc:mapping path="/user/qiuzhizhaopin/ChoiceResume"/>
<bean class="com.oracle.interceptor.UserInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>