SSM框架(六)SpringMVC-Spring集成【Mybatis搭建详述】附源码

一、Spring集成Mybatis配置

mybatis配置SQL的三种方式:
1.DAO接口+xml配置(在XML中配置SQL)(本文采用这种方式)
2.DAO接口上用注解
@Insert
@Update
@Delete
@Select
@Results+@Result/@ResultMap
3.SQLProvider模式
自定义类实现生成sql的方法
@SelectProvider注解将自定义类注入进来用以生成select sql

(一)配置

  • web.xml中:
    在这里插入图片描述
    web.xml关键代码:

        <!-- 声明Spring的主配置文件 -->
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<!--param-value>classpath:config/*.xml</param-value-->
    		<param-value>classpath:config/application-*.xml</param-value>
    	</context-param>
    
  • 详细resources中配置:

    • 主要配置目录
      在这里插入图片描述

    mybatis运行原理
    在这里插入图片描述
    Configuration:解析xml配置文件
    SqlSessionFactoryBuilder:读取配置文件,产生SqlSessionFactory
    SqlSessionFactory:生成SqlSession
    SqlSession:具体执行sql
    MapppedStatement:读取映射文件
    Transation:处理事务

    • StudentInfoDao.xml : Mybatis封装对数据库增删改查操作
      名字路径都要一致
      在这里插入图片描述
      在这里插入图片描述
      在这里插入图片描述
    <?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.cungudafa.spingmvc01.dao.StudentInfoDao"><!-- 表示该xml和哪个DAO接口对应 -->
    
    	<!-- 配置使自定义的DAO接口生效的方法 -->
    	<!-- 配置addStudentInfo方法 -->
    	<insert id="addStudentInfo" parameterType="StudentInfo">
    		<!-- insert方法,标签id和接口方法名相同,方法的形参的类型 -->
    		insert into student_info(student_name,student_number)
    		values(#{studentName},#{studentNumber})
    		<!-- 表示接口的addStudentInfo方法要执行的sql语句,#{}表示从方法的形参里取值 -->
    	</insert>
    
    	<!-- 配置deleteStudentInfo方法 -->
    	<delete id="deleteStudentInfo" parameterType="StudentInfo">
    		delete from
    		student_info where student_id = #{studentId}
    	</delete>
    
    	<!-- 配置updateStudentInfo方法 -->
    	<update id="updateStudentInfo" parameterType="StudentInfo">
    		update student_info
    		set student_name = #{studentName},
    		student_number = #{studentNumber}
    		where student_id = #{studentId}
    	</update>
    
    	<!-- 针对查询的结果集,配置查询结果和实体类的映射关系 -->
    	<resultMap type="StudentInfo" id="stumap">
    		<!-- 配置主键映射,student_info 表中的student_id字段映射到StudentInfo类中的studentId成员变量 -->
    		<id column="student_id" property="studentId" />
    		<!-- 配置非主键映射 student_info 表中的student_name字段映射到StudentInfo类中的studentName成员变量 -->
    		<result column="student_name" property="studentName" />
    		<result column="student_number" property="studentNumber" />
    	</resultMap>
    
    	<select id="getStudentInfoById" parameterType="StudentInfo"
    		resultMap="stumap">
    		select * from student_info where student_id=#{studentId}
    	</select>
    
    	<!-- <select id="getStudentInfos" resultType="List" resultMap="stumap"> 
    		select * from student_info; </select> -->
    
    	<select id="getStudentInfos" parameterType="StudentInfo"
    		resultType="List" resultMap="stumap">
    		select * from student_info
    		<where>
    			<if test="studentName != null and studentName !=''">
    				and student_name like #{studentName}
    			</if>
    			<if test="studentNumber != null and studentNumber != ''">
    				and student_number like #{studentNumber}
    			</if>
    		</where>
    	</select>
    </mapper>
    

    在这里插入图片描述

    • 2.0 application-annotation:配置Spring扫描哪些包里的注解(除开Controller)
      <?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:context="http://www.springframework.org/schema/context"
      	xmlns:tx="http://www.springframework.org/schema/tx"
      	xmlns:aop="http://www.springframework.org/schema/aop"
      	xsi:schemaLocation="  
      http://www.springframework.org/schema/beans   
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
      http://www.springframework.org/schema/context   
      http://www.springframework.org/schema/context/spring-context-3.0.xsd  
      http://www.springframework.org/schema/tx   
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
      http://www.springframework.org/schema/aop   
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
        
      ">
      	<!-- Spring扫描 -->
      	<context:component-scan
      		base-package="com.cungudafa.spingmvc01"><!-- 最大工程目录 -->
      		<context:exclude-filter type="annotation"
      			expression="org.springframework.stereotype.Controller" />
      	</context:component-scan>
      
      	<context:annotation-config />
      </beans>  
      
    • 2.1 application-datasource:配置本工程需要连接的数据源(数据库)
      在这里插入图片描述
      <?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:context="http://www.springframework.org/schema/context"
      	xmlns:tx="http://www.springframework.org/schema/tx"
      	xmlns:aop="http://www.springframework.org/schema/aop"
      	xsi:schemaLocation="  
      http://www.springframework.org/schema/beans   
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
      http://www.springframework.org/schema/context   
      http://www.springframework.org/schema/context/spring-context-3.0.xsd  
      http://www.springframework.org/schema/tx   
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
      http://www.springframework.org/schema/aop   
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  ">
      	<!-- 配置C3P0:第三方的数据库连接池 -->
      	<bean id="dataSource"
      		class="com.mchange.v2.c3p0.ComboPooledDataSource">
      		<property name="driverClass" value="${driverClass}"></property>
      		<property name="jdbcUrl" value="${jdbcUrl}"></property>
      		<property name="user" value="${user}"></property>
      		<property name="password" value="${password}"></property>
      	</bean>
      
      	<!-- 读取资源文件properties/jdbc.properties -->
      	<bean
      		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      		<property name="locations">
      			<list>
      				<value>classpath:properties/jdbc.properties</value>
      			</list>
      		</property>
      	</bean>
      
      </beans>  
      

    jdbc.properties:(mysql8.0版本

    driverClass=com.mysql.cj.jdbc.Driver
    jdbcUrl=jdbc:mysql://localhost:3306/ssm_mysql?useSSL=false&serverTimezone=UTC
    user=root
    password=wy123456
    
    • pom.xml中需要加入对C3P0的依赖
      在这里插入图片描述

      		<!-- c3p0版本号 -->
      		<c3p0-version>0.9.1.2</c3p0-version>
      		---------------------------------------
      		<!-- connect pool with c3p0 -->
      		<dependency>
      			<groupId>c3p0</groupId>
      			<artifactId>c3p0</artifactId>
      			<version>${c3p0-version}</version>
      		</dependency>
      
    • 2.2 application-mybatis:配置mybatis相关的参数
      在这里插入图片描述
      <?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:context="http://www.springframework.org/schema/context"
      	xmlns:tx="http://www.springframework.org/schema/tx"
      	xmlns:aop="http://www.springframework.org/schema/aop"
      	xsi:schemaLocation="  
      http://www.springframework.org/schema/beans   
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
      http://www.springframework.org/schema/context   
      http://www.springframework.org/schema/context/spring-context-3.0.xsd  
      http://www.springframework.org/schema/tx   
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
      http://www.springframework.org/schema/aop   
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
      ">
      
      	<!-- Mybatis SqlSessionFactory配置 -->
      	<bean id="sqlSessionFactory"
      		class="org.mybatis.spring.SqlSessionFactoryBean">
      		<property name="dataSource" ref="dataSource"></property>
      		<property name="mapperLocations"
      			value="classpath:com/cungudafa/spingmvc01/dao/*.xml"></property>
      		<property name="typeAliasesPackage"
      			value="com.cungudafa.spingmvc01.bean"></property>
      	</bean>
      
      	<!-- 扫描包 -->
      	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      		<property name="basePackage"
      			value="com.cungudafa.spingmvc01.dao"></property>
      		<property name="sqlSessionFactoryBeanName"
      			value="sqlSessionFactory"></property>
      	</bean>
      </beans>  
      
    • 2.3 application-transaction:配置事务
      <?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:context="http://www.springframework.org/schema/context"
      	xmlns:tx="http://www.springframework.org/schema/tx"
      	xmlns:aop="http://www.springframework.org/schema/aop"
      	xsi:schemaLocation="  
      http://www.springframework.org/schema/beans   
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
      http://www.springframework.org/schema/context   
      http://www.springframework.org/schema/context/spring-context-3.0.xsd  
      http://www.springframework.org/schema/tx   
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
      http://www.springframework.org/schema/aop   
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
        
      ">
      	<!-- Spring事务配置 -->
      	<bean id="transactionManager"
      		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      		<property name="dataSource" ref="dataSource"></property>
      	</bean>
      
      	<!-- 事务注解 -->
      	<tx:annotation-driven
      		transaction-manager="transactionManager" />
      </beans>  
      

(二)测试

  • 项目目录
    在这里插入图片描述

  • 实体StudentInfo

    package com.cungudafa.spingmvc01.bean;
    
    public class StudentInfo {
    	private Integer studentId;
    	private String studentName;
    	private String studentNumber;
    	public Integer getStudentId() {
    		return studentId;
    	}
    	public void setStudentId(Integer studentId) {
    		this.studentId = studentId;
    	}
    	public String getStudentName() {
    		return studentName;
    	}
    	public void setStudentName(String studentName) {
    		this.studentName = studentName;
    	}
    	public String getStudentNumber() {
    		return studentNumber;
    	}
    	public void setStudentNumber(String studentNumber) {
    		this.studentNumber = studentNumber;
    	}
    }
    
  • StudentInfoController控制器
    在这里插入图片描述

    package com.cungudafa.spingmvc01.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import com.cungudafa.spingmvc01.bean.StudentInfo;
    import com.cungudafa.spingmvc01.service.StudentInfoService;
    
    /**
     * 学生操作的核心控制器
     * @author Administrator
     *
     */
    @Controller
    @RequestMapping("/student")
    public class StudentInfoController {
    	
    	@Autowired//自动装配,将StudentInfoService接口实现的类自动注入进来
    	private StudentInfoService studentInfoService ;
    	/**
    	 * 处理学生注册的请求
    	 * @param studentInfo
    	 * @return
    	 */
    	@RequestMapping("/doreg")
    	public String doReg(StudentInfo studentInfo){
    		System.out.println("执行StudentInfoController.doReg...");
    		studentInfoService.regStudentInfo(studentInfo);
    		return "index";
    	}
    }
    
  • DAO层:

    • 接口StudentInfoDao

      package com.cungudafa.spingmvc01.dao;
      
      import com.cungudafa.spingmvc01.bean.StudentInfo;
      
      /**
       * 对于StudentInfo实体的CRUD操作接口
       * @author Administrator
       *
       */
      public interface StudentInfoDao {
      	/**
      	 * 添加一条学生信息
      	 * @param studentInfo
      	 */
      	public void addStudentInfo(StudentInfo studentInfo);
      }
      
      
    • 接口实现类StudentInfoDaoImpl

      package com.cungudafa.spingmvc01.dao.impl;
      
      import com.cungudafa.spingmvc01.bean.StudentInfo;
      import com.cungudafa.spingmvc01.dao.StudentInfoDao;
      
      
      /**
       * 学生数据访问接口的实现类
       * @author Administrator
       *
       */
      public class StudentInfoDaoImpl implements StudentInfoDao{
      
      	@Override
      	public void addStudentInfo(StudentInfo studentInfo) {
      		System.out.println("执行了StudentInfoDaoImpl.addStudentInfo...");
      	}
      }
      
  • Service业务层:

    • 学生业务功能接口StudentInfoService

      package com.cungudafa.spingmvc01.service;
      
      import com.cungudafa.spingmvc01.bean.StudentInfo;
      
      /**
       * 对于学生的业务功能接口
       * @author Administrator
       *
       */
      public interface StudentInfoService {
      	/**
      	 * 学生注册
      	 * @param studentInfo
      	 */
      	public void regStudentInfo(StudentInfo studentInfo);
      }
      
    • 学生业务接口的实现类StudentInfoServiceImpl
      在这里插入图片描述

      package com.cungudafa.spingmvc01.service.impl;
      
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Component;
      import org.springframework.stereotype.Service;
      
      import com.cungudafa.spingmvc01.bean.StudentInfo;
      import com.cungudafa.spingmvc01.dao.StudentInfoDao;
      import com.cungudafa.spingmvc01.dao.impl.StudentInfoDaoImpl;
      import com.cungudafa.spingmvc01.service.StudentInfoService;
      
      
      /**
       * 学生业务接口的实现类
       * @author Administrator
       *
       */
      @Service//告诉Sping这是某个接口的实现类,可以用来注入
      public class StudentInfoServiceImpl implements StudentInfoService{
      	
      	private StudentInfoDao studentInfoDao = new StudentInfoDaoImpl();
      
      	@Override
      	public void regStudentInfo(StudentInfo studentInfo) {
      		System.out.println("执行了StudentInfoServiceImpl.regStudentInfo...");
      		studentInfoDao.addStudentInfo(studentInfo);
      	}
      }
      
    • 业务层其他审核操作:StudentInfoServiceImplCheckBeforeUpdate

      package com.cungudafa.spingmvc01.service.impl;
      
      import com.cungudafa.spingmvc01.bean.StudentInfo;
      import com.cungudafa.spingmvc01.dao.StudentInfoDao;
      import com.cungudafa.spingmvc01.dao.impl.StudentInfoDaoImpl;
      import com.cungudafa.spingmvc01.service.StudentInfoService;
      /**
       * 
       * @author Administrator
       *
       */
      public class StudentInfoServiceImplCheckBeforeUpdate implements StudentInfoService{
      	
      	private StudentInfoDao studentInfoDao = new StudentInfoDaoImpl();
      	
      	@Override
      	public void regStudentInfo(StudentInfo studentInfo) {
      		System.out.println("安全检查……");
      		System.out.println("执行了StudentInfoServiceImpl.regStudentInfo...");
      		studentInfoDao.addStudentInfo(studentInfo);
      	}
      }
      
  • Spring的控制反转/依赖注入 Invertion Of Controll/Dependency Injection
    在这里插入图片描述

(三)运行结果

浏览器输入:
http://localhost:8080/SpingMVC01/student/doreg.d

查看到结果:
在这里插入图片描述
控制台打印出:
在这里插入图片描述
框架搭建成功!

二、结合数据库应用

(一)数据库->学生表

数据库名:ssm_mysql
表名:student_info
列名:student_id(int11,主键,自增)、student_name(varchar255)、student_number(varchar255)
在这里插入图片描述
假设现有一学生记录:(id=1,name=1,number=1)
在这里插入图片描述

(二)具体实现

  • 目录
    在这里插入图片描述在这里插入图片描述

mybatis运行流程:
在这里插入图片描述

  • StudentInfoDao.java和StudentInfoDao.xml相对比
    StudentInfoDao.java和StudentInfoDao.xml之间的关系图:
    名字路径都要一致)
    在这里插入图片描述
    StudentInfoDao.xml使用说明图:
    在这里插入图片描述在这里插入图片描述
    StudentInfoDao.java函数说明图:
    在这里插入图片描述

  • StudentInfoDao.java:(dao接口)

    package com.cungudafa.spingmvc01.dao;
    
    import java.util.List;
    
    import com.cungudafa.spingmvc01.bean.StudentInfo;
    
    /**
     * 对于StudentInfo实体的CRUD操作接口
     * @author Administrator
     *
     */
    public interface StudentInfoDao {
    	/**
    	 * 添加一条学生信息
    	 * @param studentInfo {studentName,studentNumber}
    	 */
    	public void addStudentInfo(StudentInfo studentInfo);
    	/**
    	 * 删除一条学生信息
    	 * @param studentInfo {studentId}
    	 */
    	public void deleteStudentInfo(StudentInfo studentInfo);
    	/**
    	 * 更新学生信息
    	 * @param studentInfo {studentId,studentName,studentNumber}
    	 */
    	public void updateStudentInfo(StudentInfo studentInfo);
    	
    	/**
    	 * 根据id查询学生信息
    	 * @param studentInfo {studentId}
    	 * @return StudentInfo类型的对象
    	 */
    	public StudentInfo getStudentInfoById(StudentInfo studentInfo);
    	
    //	/**
    //	 * 查询所有学生信息
    //	 * @return
    //	 */
    //	public List<StudentInfo> getStudentInfos();
    	
    	/**
    	 * 根据条件查询学生记录
    	 * @param studentInfo {studentName 可能为空,可能不为空 studentNumber可能为空,可能不为空}
    	 * 1.studentName studentNumber都为空 ——> select * from student_info
    	 * 2.studentName != null --> select * from student_info where student_name like ?
    	 * 3.studentNumber != null --> select * from student_infow where student_number like ?
    	 * 4.studentName && studentNumber != null
    	 * 	-->select * from student_info where studnet_name = ? and student_number = ?
    	 * @return
    	 */
    	public List<StudentInfo> getStudentInfos(StudentInfo studentInfo);
    }
    
  • StudentInfoService.java(service接口)
    StudentInfoService和StudentInfoServiceImpl运行流程图:
    在这里插入图片描述
    StudentInfoService.java:

    package com.cungudafa.spingmvc01.service;
    
    import java.util.List;
    
    import com.cungudafa.spingmvc01.bean.StudentInfo;
    
    
    /**
     * 对于学生的业务功能接口
     * @author Administrator
     *
     */
    public interface StudentInfoService {
    	/**
    	 * 学生注册
    	 * @param studentInfo
    	 */
    	public void regStudentInfo(StudentInfo studentInfo);
    	
    	/**
    	 * 学生注销
    	 * @param studentInfo
    	 */
    	public void delStudentInfo(StudentInfo studentInfo);
    	
    	/**
    	 * 学生信息更新
    	 * @param studentInfo
    	 */
    	public void updateStudentInfo(StudentInfo studentInfo);
    	
    	/**
    	 * 根据条件查询学生信息
    	 * @param studentInfo
    	 * @return
    	 */
    	public List<StudentInfo> getStudentInfos(StudentInfo studentInfo);
    	
    }
    
    • StudentInfoServiceImpl.java
    package com.cungudafa.spingmvc01.service.impl;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Service;
    
    import com.cungudafa.spingmvc01.bean.StudentInfo;
    import com.cungudafa.spingmvc01.dao.StudentInfoDao;
    //import com.cungudafa.spingmvc01.dao.impl.StudentInfoDaoImpl;
    import com.cungudafa.spingmvc01.service.StudentInfoService;
    
    
    /**
     * 学生业务接口的实现类
     * @author Administrator
     *
     */
    @Service
    public class StudentInfoServiceImpl implements StudentInfoService{
    	@Autowired
    	private StudentInfoDao studentInfoDao ;//= new StudentInfoDaoImpl();
    	
    	/**
    	 * 学生注册
    	 */
    	@Override
    	public void regStudentInfo(StudentInfo studentInfo) {
    		System.out.println("执行了StudentInfoServiceImpl.regStudentInfo...");
    		studentInfoDao.addStudentInfo(studentInfo);
    	}
    	
    	@Override
    	public void delStudentInfo(StudentInfo studentInfo){
    		studentInfoDao.deleteStudentInfo(studentInfo);
    	}
    
    	@Override
    	public void updateStudentInfo(StudentInfo studentInfo) {
    		studentInfoDao.updateStudentInfo(studentInfo);
    	}
    	
    	/**
    	 * 张	--》   %张%
    	 * 10	--》   %10%
    	 */
    	@Override
    	public List<StudentInfo> getStudentInfos(StudentInfo studentInfo) {
    		if(studentInfo.getStudentName()!= null &&
    				!studentInfo.getStudentName().equals("")){
    			studentInfo.setStudentName("%" + studentInfo.getStudentName() + "%");
    		}
    		if(studentInfo.getStudentNumber() != null &&
    				!studentInfo.getStudentNumber().equals("")){
    			studentInfo.setStudentNumber("%" + studentInfo.getStudentNumber() + "%");
    		}
    		return studentInfoDao.getStudentInfos(studentInfo);
    	}
    }
    
  • studentController.java
    controller与service之间的关系图:
    在这里插入图片描述

package com.cungudafa.spingmvc01.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cungudafa.spingmvc01.bean.StudentInfo;
import com.cungudafa.spingmvc01.service.StudentInfoService;

/**
 * 学生操作的核心控制器
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/student")
public class StudentInfoController {
	
	@Autowired//自动装配,将StudentInfoService接口实现的类自动注入进来
	private StudentInfoService studentInfoService ;
	/**
	 * 处理学生注册的请求
	 * @param studentInfo
	 * @return
	 */
	@RequestMapping("/doreg")
	public String doReg(StudentInfo studentInfo){
		System.out.println("执行StudentInfoController.doReg...");
		studentInfoService.regStudentInfo(studentInfo);
		return "index";
	}
	/**
	 * 处理学生注销的请求
	 * @param studentInfo
	 * @return
	 */
	@RequestMapping("/dodelete")
	public String doDelete(StudentInfo studentInfo){
		studentInfoService.delStudentInfo(studentInfo);
		return "index";
	}
	
	/**
	 * 处理学生信息更新的请求
	 * @param studentInfo
	 * @return
	 */
	@RequestMapping("/doupdate")
	public String doUpdate(StudentInfo studentInfo){
		studentInfoService.updateStudentInfo(studentInfo);
		return "index";
	}
	/**
	 * 根据条件查询学生信息
	 * @param studentInfo {studentName 可能为空,可能不为空 studentNumber可能为空,可能不为空}
	 * @return
	 */
	@RequestMapping("/list")
	public String list(StudentInfo studentInfo,HttpSession session){
		List<StudentInfo> list = studentInfoService.getStudentInfos(studentInfo);
		session.setAttribute("students", list);
		return "index";
	}
}
  • jsp界面
    在这里插入图片描述
    main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
	session.setAttribute("basePath", basePath);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>Helloworld</h1>
	<form action="${basePath }dologin.d" method="post">
		<input type="text" name="userName" /><br />
		<input type="text" name="userPassword" /><br /> 
		<input type="submit" value="添加" />
	</form>
	欢迎您,${user.userName },id=${id }<br>
	<a href="${basePath }file/upload.d">文件上传</a><br>
	<a href="${basePath }file/files.d">查看所有文件</a><br>
	<h1>学生登录</h1>
	<form action="${basePath }student/list.d" method="post">
	<input type="text" name="studentName" placeholder="请输入姓名">
	<input type="text" name="studentNumber" placeholder="请输入学号">
	<button type="submit">查询</button>
</form>
</body>
</html>

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
	session.setAttribute("basePath", basePath);
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	欢迎您,${user.userName },id=${id }
	<br>
	<a href="${basePath }file/upload.do">文件上传</a>
	<br>
	<a href="${basePath }file/files.do">查看所有文件</a>
	<br>
	<h3>学生信息</h3>
	<table>
		<tr>
			<th>学号</th>
			<th>姓名</th>
		</tr>
		<c:forEach items="${students }" var="s">
			<tr>
				<td>${s.studentNumber }</td>
				<td>${s.studentName }</td>
			</tr>
		</c:forEach>
	</table>
</body>
</html>
  • 运行结果:http://localhost:8080/SpingMVC01/main.jsp
    查询结果:
    在这里插入图片描述在这里插入图片描述

三、Junit测试

(一)引入jar

pom.xml中引入:

		<!-- Spring做测试 -->
		<dependency>
    		<groupId>org.springframework</groupId>
    		<artifactId>org.springframework.test</artifactId>
    		<version>3.0.2.RELEASE</version>
    		<scope>test</scope>
		</dependency>

遇见问题:
不过我怎么引入都不成功,最后在网上自己下载了org.springframework.test-3.0.2.RELEASE.jar包:
下载链接:org.springframework.test-3.0.2.RELEASE.jar
导入方法:项目右键buildpath-》Add External JARs-》jar包位置
在这里插入图片描述

(二)测试:

  • 目录
    在这里插入图片描述
  • StudentInfoDaoTest.java:
    在这里插入图片描述
package com.cungudafa.spingmvc01.dao;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.cungudafa.spingmvc01.bean.StudentInfo;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:config/application-*.xml")
public class StudentInfoDaoTest {
	@Autowired
	private StudentInfoDao studentInfoDao;
	@Test
	public void test1(){
		StudentInfo studentInfo = new StudentInfo();
		studentInfo.setStudentId(1);//查询是否有id为1的记录
		
		studentInfo = studentInfoDao.getStudentInfoById(studentInfo);
		System.out.println(studentInfo.getStudentName());
		System.out.println(studentInfo.getStudentNumber());
	}
	
	@Test
	public void test2(){
		StudentInfo studentInfo = new StudentInfo();
		studentInfo.setStudentName("王五");
		studentInfo.setStudentNumber("100003");		//添加
		studentInfoDao.addStudentInfo(studentInfo);
	}
	
//	@Test
//	public void test3(){
//		List<StudentInfo> list = studentInfoDao.getStudentInfos();
//		for(StudentInfo s : list){
//			System.out.println("s.name = " + s.getStudentName());
//			System.out.println("s.number = " + s.getStudentNumber());
//		}
//	}
	
	@Test
	public void test4(){
		StudentInfo studentInfo = new StudentInfo();
		studentInfo.setStudentName("王%");//模糊查询
		studentInfo.setStudentNumber("10%");
		List<StudentInfo> list = studentInfoDao.getStudentInfos(studentInfo);
		for(StudentInfo s : list){
			System.out.println("s.name = " + s.getStudentName());
			System.out.println("s.number = " + s.getStudentNumber());
		}
	}
}
  • 结果:
    在这里插入图片描述
    查询:
    在这里插入图片描述
    添加:
    在这里插入图片描述
    模糊查询:
    在这里插入图片描述
    在这里插入图片描述

  • Service测试:

package com.cungudafa.spingmvc01.service;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.cungudafa.spingmvc01.bean.StudentInfo;
import com.cungudafa.spingmvc01.service.StudentInfoService;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:config/application-*.xml")
public class StudentInfoServiceTest {
	@Autowired
	private StudentInfoService studentInfoService;
	
	@Test
	public void test1(){
		StudentInfo studentInfo = new StudentInfo();
		studentInfo.setStudentName("王");
		studentInfo.setStudentNumber("10");
		List<StudentInfo> list = studentInfoService.getStudentInfos(studentInfo);
		for(StudentInfo s : list){
			System.out.println("s.name = " + s.getStudentName());
			System.out.println("s.number = " + s.getStudentNumber());
		}
	}
}

在这里插入图片描述


附:源码下载链接
https://download.csdn.net/download/cungudafa/11221177

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值