SSM中零散小问题总结

这篇博客总结了SSM框架(Spring、SpringMVC、MyBatis)使用过程中遇到的一些具体问题,包括ModelAndView的重定向操作、启动报错、MyBatis的参数错误、多表操作策略、resultType与resultMap的使用误区以及MyBatis插入返回主键ID的方法。还探讨了MyBatis中集合默认值和SSM配置拦截器的设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本篇文章不会系统的讲解知识也不会深入讲,只会说明一个很具体的问题,属于随手记录型。

如何用 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

  1. useGeneratedKeys=“true”
<insert id="insertBook" useGeneratedKeys="true" keyProperty="id">
    insert into t_book (b_name,author) values (#{name},#{author});
</insert>
  1. 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里 集合默认值

  1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
  2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
  3. 如果传入的参数是多个的时候,我们就需要把它们封装成一个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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值