目录
1.1.2 web.xml中添加核心类配置,DispatcherServlet,指向框架Springmvc.xml的配置文件
Spring Mvc
Spring Mvc流程
工作流程:
①DispatcherServlet:接受到浏览器端发送的所有请求
②HandlerMapper处理器映射器:找到@requestMapping对应值
③HandlerAdapter处理器适配器:适配对应的方法调用执行
④modelAndView模型视图对象:方法的返回值
⑤viewResolver解析模型视图对象,得到view,返回给浏览器用户展示;
1.1.1 导入pom依赖
<!-- https://mvnrepository.com/artifact/com.mangofactory/swagger-springmvc -->
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>1.0.2</version>
</dependency>
1.1.2 web.xml中添加核心类配置,DispatcherServlet,指向框架Springmvc.xml的配置文件
<!-- DispatcherServlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
1.1.3 配置Springmvc.xml
配置驱动包扫描<context:component-scan base-package="com.javaxl.ssm"/>
重点:配置本地资源视图解析器
<!-- ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- viewClass需要在pom中引入两个包:standard.jar and jstl.jar -->
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
添加静态文件映射
<mvc:resources location="/images/" mapping="/images/**"/>
1.1.4 controller层,正常开发即可
通过@controller标记当前类被spring所管理
写方法:@requestMapping
方法会有返回值:"index" > /index.jsp
1.1.5 Springmvc怎么返回数据
转发:"forword:/list"
重定向:"redirect:/list"
1.1.6 常用的注解
@controller @requestMapping
@getMapping @putMapping
@deleteMapping @postMapping 若依
@responseBody-返回json数据 @requestBody-接受json数据
@pathvariable @requestParam
Mybatis是什么?
1 Mybatis是一个优秀的持久层,它对JDBC操作数据库的过程进行封装,使开发者只需要关注sql本身
2 MyBatis属于半自动的ORM(object reference Mapping 对象关系映射)框架
2.1 关联关系的配置
一对多:association javatype
多对多:Collection oftype
<resultMap id="order_list_map" type="order">
<!-- id用于映射主键 -->
<id property="id" column="id"/>
<!-- 普通属性映射 -->
<result property="userId" column="user_id"/>
<result property="number" column="number"/>
<!-- 配置一对多的关系 :引入一的一方属性 -->
<association property="user" javaType="com.jjxy.pojo.User">
<result property="id" column="id"/>
<result property="username" column="username"/>
</association>
</resultMap>
2.2 常用标签
2.2.1 if
<if test="null!=bookType and ''!=bookType">
book_type 代表数据库字段
bookType代表实体属性不需要类名点
# 代表取值
and book_type=#{bookType}
</if>
2.2.2 trim
prefix 前缀 suffix 后缀
suffixOverrides 去除后缀指定字符
prefixOverrides 去除前缀指定字符
<insert id="insertSelective" parameterType="com.zking.provider.model.Book">
insert into t_book
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="bid != null"> bid, </if>
<if test="bname != null"> bname, </if>
<if test="bprice != null"> bprice, </if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="bid != null"> #{bid,jdbcType=INTEGER}, </if>
<if test="bname != null"> #{bname,jdbcType=VARCHAR}, </if>
<if test="bprice != null"> #{bprice,jdbcType=REAL}, </if>
</trim>
2.3 #{...}与${...}传参的区别
#{...} 传递参数会自带引号
${...} 不会自带引号,所有存在sql注入的风险
例如:
DELETE FROM t_user WHERE id = 1101
#{id} > id = '1101' or id is not null
DELETE FROM t_user WHERE id = ''1101' or id is not null '
注:运行就会报错,这样便不会删除
${id} > id = '1101' or id is not null
DELETE FROM t_user WHERE id = '1101' or id is not null