springMVC学习笔记
一、springMVC异常处理:
1、第一种方法:
(1)在handler中,添加一个处理异常的方法该方法必须实现@ExceptionHandler注解,此注解有一个value属性,是一个class数组,可以使一个方法同时处理多个不同类型的异常。例子:
@ExceptionHandler({ArithmeticException.class, ArrayIndexOutOfBoundsException.class})
public String isException(Exception ex) {
System.err.println("出错了: " + ex);
return "error";
}
在此handler中的方法如果出现ArithmeticException或ArrayIndexOutOfBoundsException异常则会交给此方法处理,在控制台打印异常信息,并进入error页面。练习截图:
(2)若希望将异常信息打印到页面上,则需要使用ModelAndView对象作为返回值:
error页面中的写法为:
(3)@ExceptionHandler注解标注的方法有优先级:
一个是RuntimeException,一个是ArithmeticException,如果出现除数为0的情况则会执行第二个方法,因为ArithmeticException异常与除数为0精确匹配。即精确匹配的优先级高。
2、第二种方法,专门写一个异常处理的类,并且为此类添加@ControllerAdvice注解即可用来专门处理异常,handler中不需添加其他注解和方法。
详细解释:如果在当前handler中找不到 @ExceptionHandler 标记的方法,则会去 @ControllerAdvice 标记的类中查找 @ExceptionHandler 标记的方法来处理异常。
练习截图:
3、添加 @ResponseStatus 注解,异常信息会在页面打印:
(1)在类名上添加 @ResponseStatus 注解
假设i的值为13时抛出该异常:
则会在页面中打印如下异常信息:
(2)在方法名上添加 @ResponseStatus 注解则无论i是否等于13都会抛出异常。即添加了该注解的方法就会抛出下图异常:
4、在springMVC的配置文件中处理异常,例如出现数组下标越界异常则跳转到error页面。练习截图:
如果要在异常页面打印异常信息,则exceptionAttribute的value值必须与页面中相同:
在error页面打印异常信息:
二、在web.xml中配置spring的快速方法:
Alt+/ 输入contextloaderlistener
三、使用spring整合springMVC时,将controller交给springMVC的IOC容器管理,其他的由spring的IOC容器管理,反之会报错。
Spring的IOC容器扫描的内容为(除了controller的所有内容):
springMVC的IOC容器扫描的内容为controller:
四、springMVC与Struts2的对比
(1)springMVC的入口是Servlet,而Struts2的入口是Filter;
(2)springMVC比Struts2稍微快些,因为springMVC是基于方法设计的而Struts2是基于类,每发一次请求都会实例一个action;
(3)springMVC更加简洁,开发效率比Struts2高,支持JSR303,处理ajax的请求更方便;
(4)Struts2的OGNL表达式使页面的开发效率相比springMVC更高些。
myBatis学习笔记
一.一对一关联(需要使用association 标签):
有两张表,一张teacher表,一张class表(teacher表中的id字段是class表的外键),要求查询class表中的一条记录,同时查询出teacher表对应的一条记录(class表和teacher表是一对一关系):
Teacher表:
Class表:
有两种方法(teacher和class都是实体类的别名):
1.使用一条SQL语句:
<resultMap type="class" id="getClass">
<id column="c_id" property="id"/>
<result column="c_name" property="name"/>
<association property="teacher" javaType="teacher">
<id column="t_id" property="id"/>
<result column="t_name" property="name"/>
</association>
</resultMap>
<!-- 一条SQL语句 -->
<select id="selectOne" parameterType="int" resultMap="getClass">
SELECT * FROM class, teacher WHERE c_id=#{id} AND teacher_id=t_id
</select>
2.使用两条SQL语句:
<!-- 2条SQL语句 -->
<select id="selectClass" parameterType="int" resultMap="getClass2Map">
select * from class where c_id=#{id}
</select>
<select id="selectTeacher" parameterType="int" resultType="teacher">
select t_id id, t_name name from teacher where t_id=#{id}
</select>
<resultMap type="class" id="getClass2Map">
<id column="c_id" property="id"/>
<result column="c_name" property="name"/>
<!-- 将上一条SQL语句中查询出来的teacher_id作为参数传递给selectTeacher对应的SQL语句 -->
<association property="teacher" column="teacher_id" select="selectTeacher"></association>
</resultMap>
二.一对多关联(需要使用collection 标签):
在数据库中添加一张student表:
查询class表中的一条记录,同时查出对应的teacher表和student表中的记录(class表中的记录和student表中的记录为一对多关系)
<!-- ***一对多关联*** -->
<select id="selectSome" parameterType="int" resultMap="getClass3Map">
SELECT * FROM class, teacher, student WHERE c_id=#{id} AND teacher_id=t_id AND c_id=class_id
</select>
<resultMap type="class" id="getClass3Map">
<id column="c_id" property="id"/>
<result column="c_name" property="name"/>
<association property="teacher" javaType="teacher">
<id column="t_id" property="id"/>
<result column="t_name" property="name"/>
</association>
<collection property="list" ofType="student"><!-- ofType:指定list中存放的数据类型 -->
<id column="s_id" property="id"/>
<result column="s_name" property="name"/>
</collection>
</resultMap>
如果为三条SQL语句的话collection标签可以写为:
<collection property="teacher" column="c_id" select="selectStudent"></collection>
存储过程中<![CDATA[ ]]> 标记的作用:避免Sql中与xml规范相冲突的字符对xml映射文件的合法性造成影响
三.MyBatis一级缓存:
Session级别,清除缓存的方法:session.clearCache();
练习例子:当没调用clearCache()方法时,使用缓存只调用一次SQL语句:
public void testGetUser() {
SqlSession session = MybatisUtils.getSessionFactory().openSession(true);//true表示会自动提交
User user = session.selectOne("cn.com.gjw.test2.userMapper.getUser", 1);
System.out.println(user);
user = session.selectOne("cn.com.gjw.test2.userMapper.getUser", 1);
System.out.println(user);
}
当调用clearCache()方法时,缓存被清掉,调用两次SQL语句:
或在两次查询之间调用增/删/改的操作即可清除一级缓存,SQL语句会调用两次;
当session被关掉的时候一级缓存也会被清掉。
四.Mybatis二级缓存:
SessionFactory级别,
二级缓存的范围是整个mapper文件,以namespace为单元,默认没有开启,开启方式只需在mapper文件中添加cache标签即可,并且实体bean必须实现序列化接口(Serializable):
练习例子:
虽然是两个不同的session,但也只调用了一次SQL:
在cache标签中eviction属性指定回收策略,flushInterval指定自动刷新时间,单位为ms:
<cache eviction="FIFO" flushInterval="60000"/>//60秒
另一种方式(https://my.oschina.net/dxqr/blog/123163):