ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

MyBatis + SpringMVC 总结

2022-03-28 22:32:21  阅读:209  来源: 互联网

标签:总结 SpringMVC Controller classes MyBatis DispatcherServlet id


一. MyBatis 环境的搭建

  1. 创建 MyBatis 的主配置文件(mybatis-config.xml):环境,事务管理,数据源

  2. 给类取别名

    <typeAliases>
    	<package name="com.ztkj.entity"/>
    </typeAliases>	
    
  3. 配置支持懒加载

    <settings>
    	<!-- 开启或关闭延迟加载 -->
    	<setting name="lazyLoadingEnabled" value="true"/>
    	<!-- 支持延迟加载,在需要时加载外表,为 true 加载本表也会加载外表 -->
    	<setting name="aggressiveLazyLoading" value="false"/>
    </settings>
    
  4. 创建接口以及接口的映射文件(UserMapper,UserMapper.xml)

  5. 接口的全路径和映射文件中 mapper 标签的 namespace 属性保持一致

  6. 接口的方法名和映射文件中具体的标签名(insert,select,update,delete) ID 值保持一致

  7. 读取new SqlSessionFactoryBuilder().build(is)配置文件创建 SqlSessionFactory,从而创建SqlSession.openSession()方法

  8. 通过 SqlSession 来调用具体的方法

  9. sqlSession.getMapper(UserMapper.class).addUser();

  10. 提交事务,关闭 sqlSession

二. MyBatis 关系的配置

  • 多对一:association
  • 一对多:collection
  • 一对一:association
  • 多对多:collection

三. MyBatis 懒加载的配置

  1. 正常关联查询:

    <resultMap type="Student" id="studentMap">
    	<id property="studentId" column="student_id"/>
    	<result property="studentName" column="student_name"/>
    	<!-- 采用延迟加载 -->
    	<association property="classes" column="classes_id" resultMap="classesMap"></association>
    </resultMap>
    
    <!-- classes 结果集映射 -->
    <resultMap type="Classes" id="classesMap">
    	<id property="classesId" column="classes_id"/>
    	<result property="classesName" column="classes_name"/>
    </resultMap>
    
  2. 懒加载

    <resultMap type="Student" id="studentMap">
    	<id property="studentId" column="student_id"/>
    	<result property="studentName" column="student_name"/>
    	<!-- 采用延迟加载 -->
    	<association property="classes" column="classes_id" select="searchClassesById"></association>
    </resultMap>
    
  3. 只有一条 SQL 查询语句

    <select id="searchStudentById" parameterType="int" resultMap="studentMap">
    	select * from t_student t1, t_classes t2 where t1.classes_id = t2.classes_id and student_id = #{id}
    </select>
    
  4. 查询学生时,有两条 SQL 语句

    <select id="searchStudentById" parameterType="int" resultMap="studentMap">
    	select * from t_student where student_id = #{id}
    </select>
    
    <select id="searchClassesById" parameterType="int" resultMap="classesMap">
    	select * from t_classes where classes_id = #{classes.classesId}
    </select>
    
  5. 动态 SQL
    if、where、set、forEach、trim

四. SpringMVC 环境的搭建

  1. web.xml中配置 SpringMVC 的中央控制器 dispatcherServlet 拦截所有匹配的请求路径,配置读取 SpringMVC 核心配置文件的路径

  2. 配置 SpringMVC 核心配置文件

    <!-- 支持Resource autowired注解 -->
    <context:annotation-config/>
      
    <!--配置扫描器-->
    <context:component-scan base-package="com.ztkj"/>
      
    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	
        <!--配置前缀-->
    	<property name="prefix">
    		<value>/</value>
    	</property>
    	
        <!--配置后缀-->
    	<property name="suffix">
    		<value>.jsp</value>
    	</property>
    	
        <!--支持 JSTL 标签-->
    	<property name="viewClass">
       		<value>org.springframework.web.servlet.view.JstlView</value>
    	</property>
    </bean>
    
    <!-- 配置 SpringMVC 支持 JSON 数据格式的注解 -->
    <mvc:annotation-driven/>
    
    <!-- 配置 SpringMVC 的拦截器 -->
    <mvc:interceptors>
    	<mvc:interceptor>
    		<!-- 代表哪些路径需要被拦截 -->
    		<mvc:mapping path="/index/*"/>
    		<mvc:mapping path="/login/*"/>
    		<!-- 引用的拦截器的类 -->
    		<bean class="com.ztkj.interceptor.MyInterceptor"></bean>
    	</mvc:interceptor>
    </mvc:interceptors>
    
    <!-- 上传下载 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
    	<!-- 上传文件大小上限,单位为字节(10MB) -->
       	<property name="maxUploadSize">  
    	   	<value>10485760</value>  
       	</property>  
       	
        <!-- 请求的编码格式,必须和 jSP 的 pageEncoding 属性一致,以便正确读取表单的内容,默认为ISO-8859-1 -->
       	<property name="defaultEncoding">
    	   	<value>UTF-8</value>
        </property>
    </bean>
    
  3. 创建 Controller,UserController(创建方法method,addUser)

    @Controller、@RequestMapping、@RequestParam、@ResponseBody

五. SpringMVC 组件

  • DispatcherServlet:中央控制器
  • HandleMapping:映射处理器
  • Controller:控制器
  • ViewResolver:视图解析器
  • ModelAndView:模型和视图对象

六. SpringMVC 工作原理

  1. Tomcat 启动时会读取web.xml,加载 DispatcherServlet,并实例化中央控制器
  2. 客户端发送一个请求到服务器,Tomcat 会接收这个请求
  3. 判断请求是否符合 DispatcherServlet 的请求路径
  4. 若符合,则 DispatcherServlet 会调用 HandleMapping 把请求交给对应的 Controller 处理
  5. Controller 处理完请求之后,会返回一个 ModelAndView 对象给 DispatcherServlet
  6. DispatcherServlet 调用 ViewResolver 把 ModelAndView 解析成视图页面

标签:总结,SpringMVC,Controller,classes,MyBatis,DispatcherServlet,id
来源: https://www.cnblogs.com/xiqingbo/p/java-26.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有