Mybatis 整合 SpringMVC

绪论

本篇继上章,节约篇幅,重复代码不再CV。
单纯使用Mybatis无法满足对Javaweb的开发,故本篇主要对 Mybatis 和 SpringMVC进行整合

目的:查找数据库中的person的信息以及所在的班级和所选的课程

SpringMVC工作流程:在这里插入图片描述

一、 准备

1.1 数据库

同上章

1.2 项目目录

新建Web项目 SpringMVC2
在这里插入图片描述
在这里插入图片描述

二、配置文件

web.xml(配置dispatcherServlet入口及判断路径)
springmvc.xml(配置注解驱动器等springmvc组件)
mybatis.xml(配置实体和数据库及sqlmapper.xml之间的映射关系)
sqlmapper.xml(配置接口中的方法跟sql的映射,及结果集的整理)
jdbc.properties(数据库中的url,username等参数)

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	<!-- 当前项目名 -->
	<display-name>springmvc2</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<!-- 配置springmvc的入口,就是前端控制器dispatcherServlet -->
	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<!-- 配置框架启动参数 参数路径是springmvc的配置文件的路径 -->
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring/springmvc.xml</param-value>
		</init-param>
	</servlet>
	<!-- 配置是否进入控制器处理 -->
	<servlet-mapping>
		<!-- "/"表示所有都进入控制器处理,其他参数:"*.do"(后缀为.do的文件进入控制器处理)、".action(同理)" -->
		<servlet-name>dispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

	<!-- 配置基于注解的注解驱动器,包含了基于注解的处理器映射器和处理器适配器 -->
	<mvc:annotation-driven></mvc:annotation-driven>

	<!-- 上下文组件扫描器,会扫描指定的包路径下的所有spring注解 -->
	<context:component-scan
		base-package="com.zk.springmvc.controller"></context:component-scan>

	<!-- 基于jsp的视图解析器 -->
<bean	class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 如果dispatcherServlet前端控制器返回的是个字符串,会为其拼接“前缀和后缀”,成为一个完整路径,响应给请求 -->
		<property name="prefix" value="/WEB-INF/pages/">  </property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>

mybatis.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
<properties resource="jdbc/jdbc.properties"></properties>
<typeAliases>
<package name="com.zk.springmvc.model"/>
</typeAliases>

<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.DriverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>

<mappers>
<package name="com.zk.springmvc.mapper"/>
</mappers>
</configuration>

PersonMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mapper namespace="com.zk.springmvc.mapper.PersonMapper">

<resultMap type="Person" id="baseResult">
<id column="p_id" property="id"/>
<result column="p_name" property="name"/>
<result column="p_age" property="age"/>
<result column="class_id" property="classId"/>
<association property="perClass" javaType="Class">
<id column="c_id" property="classId"/>
<result column="c_name" property="className"/>
</association>
<collection property="courseList" ofType="Course">
<id column="course_id" property="courseId"/>
<result column="course_name" property="courseName"/>
</collection>
</resultMap>

<select id="findPersonAndClassAndCourse" resultMap="baseResult">
SELECT p.*,cla.*,cour.*
FROM person p,class cla,course cour,per_cour_relation rea
WHERE p.class_id = cla.c_id AND p.p_id = rea.per_id
and cour.course_id = rea.cour_id
</select>

</mapper>

jdbc.properties

jdbc.DriverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/person?useUnicode=true&characterEncoding=utf8
jdbc.username=#
jdbc.password=#

三、controller

GoodController.java

import com.zk.springmvc.model.Person;
import com.zk.springmvc.service.PersonService;
import com.zk.springmvc.service.impl.PersonServiceImpl;
//添加Controller注解,会默认这是一个dispatcherServlet前端控制器
@Controller
//分包原则,下面所有路径都在 /person 路径下
@RequestMapping("/person")
public class GoodController {
	
	//底层是Servlet,同样相当于路径
	@RequestMapping("/findPersonAndClassAndCourse")
	public String findPersonAndClassAndCourse(HttpServletRequest request) {
		PersonService personService = new PersonServiceImpl();
		List<Person> list = personService.findPersonAndClassAndCourse();
		for (Person person : list) {
			System.out.println(person);
		}
		//将得到的结果集放到request的作用域中
		request.setAttribute("list", list);
		//返回字符串交给springmvc.xml中配置的视图解析器处理
		return "showPerson";
	}
}

四、展示

showPerson.jsp

采用JSTL标签库方式(加头标签和导包

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@  taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
Welcome to 东北boys!<p>
<table border="1">
	<tr>
		<td>编号</td>
		<td>姓名</td>
		<td>年龄</td>
		<td>班级</td>
		<td>课程</td>
	</tr>
	<c:forEach items="${list }" var="person">
	<tr>	
		<td>${person.id }</td>
		<td>${person.name }</td>
		<td>${person.age }</td>
		<td>${person.perClass.className }</td>
		<td>
			<c:forEach items="${person.courseList }" var="course">
				${course.courseName } - 
			</c:forEach>
		</td>
	</tr>
	</c:forEach>
</table>
</body>
</html>

浏览器访问

直接访问jsp是访问不到的,因为在jsp在WEB-INF下,所以只有通过内部访问,直接请求前端控制器,由其跳转到jsp。
访问路径应该是项目名+包名+控制器名
在这里插入图片描述
在这里插入图片描述

总结

通过对Mybatis和SpringMVC的整合,个人认为Mybatis主要负责底层的数据处理,SpringMVC作为“控制器”主要通过用其自身的组件,对前端的请求交给Mybatis处理,并发送响应给前端。小白入门学习,见解有误还请多多指教。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值