学生管理系统学生基本信息查询(1)

导包之后我们先写一个方法测试一下,比如:

我要在页面上显示如下内容

这里要注意我们数据库tb_student表中的学校给的只是一个ID,必须使用多表查询。

一、SQL语句测试

SQL语句如下:

 

SELECT studentName,idCard,sex,studentId,education,schoolName,professional,acceptanceDate,birthday,STATUS,tel,secondTel,qq,email,address FROM tb_student AS stu INNER JOIN tb_school AS sch ON stu.schoolId=sch.schoolId AND stu.`studentId`=910513201419

先在数据库测试一下,得到如下数据:

发现查到的数据已经和我们的页面对应上了,接下来让我们写一个方法从后台查询一下数据。

二、后台方法测试

后台从数据库取值我们用的是Mybatis整合Spring的取值方式

1、建立一个实体类存储从数据库的取得的数据,注意字段名要和列名对应(不对应的情况你就得多写一个映射关系或注解了)

实体类studentInfo.class代码如下:

package entity;

import java.math.BigInteger;
import java.util.Date;

public class StudentInfo {

    private String studentName;        //姓名
    private String idCard;            //身份证号码
    private String sex;                //性别
    private BigInteger studentId;        //学号
    private String education;        //学历
    private String schoolName;        //毕业学校
    private String professional;    //专业
    private Date acceptanceDate;    //入学时间
    private Date birthday;            //出生日期
    private String status;            //状态
    private String tel;                //手机号码
    private String secondTel;        //第二联系号码
    private String qq;                //QQ
    private String email;            //邮箱
    public String getStudentName() {
        return studentName;
    }
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
    public String getIdCard() {
        return idCard;
    }
    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public BigInteger getStudentId() {
        return studentId;
    }
    public void setStudentId(BigInteger studentId) {
        this.studentId = studentId;
    }
    public String getEducation() {
        return education;
    }
    public void setEducation(String education) {
        this.education = education;
    }
    public String getSchoolName() {
        return schoolName;
    }
    public void setSchoolName(String schoolName) {
        this.schoolName = schoolName;
    }
    public String getProfessional() {
        return professional;
    }
    public void setProfessional(String professional) {
        this.professional = professional;
    }
    public Date getAcceptanceDate() {
        return acceptanceDate;
    }
    public void setAcceptanceDate(Date acceptanceDate) {
        this.acceptanceDate = acceptanceDate;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public String getTel() {
        return tel;
    }
    public void setTel(String tel) {
        this.tel = tel;
    }
    public String getSecondTel() {
        return secondTel;
    }
    public void setSecondTel(String secondTel) {
        this.secondTel = secondTel;
    }
    public String getQq() {
        return qq;
    }
    public void setQq(String qq) {
        this.qq = qq;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
}

 

 2、新建一个方法的接口:StudentInfoDao

 里面先写一个方法的接口,我们测试一下,代码如下:

StudentInfoDao.java

package dao;


import org.springframework.stereotype.Repository;

import entity.StudentInfo;

@Repository        //标注数据访问组件,即Dao层
public interface StudentInfoDao {
    
    /**
     * 获取学生
     * @param studentId 学生学号
     * @return    学生的基本信息
     */
    public StudentInfo get_StudentInfo(String studentId);
}

 

3、配置SQL映射文件

StudentInfo_mapper.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="dao.StudentInfoDao">        <!-- 命名空间里面的类名要和与这个mapper文件相对应的dao里面的接口名相同 -->
      <!-- 获得学生基本信息的方法 -->
    <select id="get_StudentInfo" parameterType="String" resultType="entity.StudentInfo">
        SELECT studentName,idCard,sex,studentId,education,schoolName,professional,acceptanceDate,birthday,STATUS,tel,secondTel,qq,email,address FROM tb_student AS stu INNER JOIN tb_school AS sch ON stu.schoolId=sch.schoolId AND stu.`studentId`=#{studentId}
    </select>
  </mapper>

4、配置service的接口和其实现service.impl

StudentInfo_Service.java

package service;

import org.apache.ibatis.annotations.Param;

import entity.StudentInfo;

public interface StudentInfo_Service {
    
    /**
     * 获取学生
     * @param studentId 学生学号
     * @return    学生的基本信息
     */
    public StudentInfo get_StudentInfo(@Param("studentId")String studentId);
}

StudentInfo_ServiceImpl.java

package service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import service.StudentInfo_Service;
import dao.StudentInfoDao;
import entity.StudentInfo;

@Service()  //标注数据处理层组件,即service层
public class StudentInfo_ServiceImpl implements StudentInfo_Service{

    @Autowired  //自动装配,交给spring帮我们装配StudentInfoDao的bean
    private StudentInfoDao stuInfoDao;
    
    public StudentInfo get_StudentInfo(String studentId) {
        
        return stuInfoDao.get_StudentInfo(studentId);
    }
}

 

5、新建数据库连接池:db.properties

代码如下:

bd.properties

jdbc.url=jdbc:mysql://localhost:3306/stusys
jdbc.username=root
jdbc.password=123
jdbc.driver=com.mysql.jdbc.Driver
jdbc.maxActive=100
jdbc.maxWait=10000

 

6、配置spring全局配置文件applicationContext.xml文件

代码如下:

applicationContext.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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <!-- 扫描数据库连接池文件 -->
    <context:property-placeholder location="classpath:db.properties" />

    <!-- 扫描service.impl层的注解 -->
    <context:component-scan base-package="service.impl"></context:component-scan>
    
    <!-- 配置数据库连接 -->
    <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="maxActive" value="${jdbc.maxActive}"></property>
        <property name="maxWait" value="${jdbc.maxWait}"></property>
    </bean>

    <!-- 配置数据 库session连接工厂 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!-- 扫描SQL映射文件 -->
        <property name="mapperLocations" value="classpath:*mapper.xml"></property>
    </bean>

    <!-- 采用自动扫描方式创建mapper bean(单个更新模式) -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="dao"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
</beans>

 

 

MapperScannerConfigurer中包含三个重要属性

basePackage:扫描器开始扫描的基础包名,支持嵌套扫描;

sqlSessionTemplateBeanName:前文提到的模板bean的名称;

markerInterface:基于接口的过滤器,实现了该接口的dao才会被扫描器扫描,与basePackage是与的作用。

7、编写测试方法后台拿值

这里我们直接在service.impl里面的get_StudentInfo后书写一个测试方法

测试方法:

public static void main(String[] args) {
        ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentInfo_Service stuInfoDao = (StudentInfo_Service) act.getBean("studentInfo_ServiceImpl");
        StudentInfo stuInfo = stuInfoDao.get_StudentInfo("910513201419");
        System.out.println(stuInfo);
    } 

测试结果如下:

出现了这一句证明已经拿到值了,返回的是一个内存地址

三、前台访问后台方法拿到后台查询出来的数据

1、先配置一个controller层的方法测试一下

StudentInfo_Controller.java

 

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import entity.StudentInfo;

@Controller
public class StudentInfo_Controller {
    
    @RequestMapping(value="getStudentInfo",method=RequestMethod.GET)    //配置地址映射,当访问这个地址时会调用这个方法
    @ResponseBody
    public StudentInfo getStudentInfo(String studentId){
        System.out.println("进入到getStudentInfo方法中了");
        return null;
    }
}

 

2、配置springmvc配置文件springmvc-servlet.xml

springmvc是Spring 框架提供的构建 Web 应用程序的全功能 MVC 模块,当我们需要和页面访问的时候配置的就是这个文件

springmvc-servlet.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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <!-- 定义controller层的注解扫描控制器 -->
    <context:component-scan base-package="controller"></context:component-scan>
    
    <!-- 启动注解,动静分离使用jsonp时需要 -->
    <mvc:annotation-driven></mvc:annotation-driven>
    
    <!-- 配置静态资源不被拦截,不设置时返回静态界面也会被web.xml拦截,web.xml的拦截分发在接下来的web.xml文件中会配置 -->
    <mvc:resources location="/js" mapping="/js/**"></mvc:resources>
    <mvc:resources location="/" mapping="/**"></mvc:resources>
    
    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".html"></property>
    </bean>
</beans>

 

3、配置web.xml文件

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>

    <!-- 关联上下文(引进其他的配置文件)设置监听器 ,多个配置文件用“,"隔开 -->
    <!-- needed for ContextLoaderListener -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>        <!-- 这里关联上下文,添加其他配置文件 -->
    </context-param>

    <!-- Bootstraps the root web application context before servlet initialization -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 关联上下文(引进其他的配置文件)设置监听器 ,多个配置文件用“,"隔开 -->
    
    
    <!-- 配置核心过滤器(分发器),拦截地址请求,然后springmvc自动根据地址去寻找对应的方法 -->
    <!-- The front controller of this Spring Web application, responsible for 
        handling all application requests -->
    <servlet>
        <servlet-name>springDispatcherServlet</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>        <!-- 这里添加springmvc的配置文件 -->
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>        <!-- 这里配置的是拦截的地址,设成/意味着拦截所有地址带/的请求,静态资源也会被拦截 -->
    </servlet-mapping>
</web-app>

 

4、测试一下能不能访问到服务器的方法

把项目添加到服务器的Tomcat中,然后启动服务器

浏览器中输入

<!-- localhost:8080/项目名/方法地址(即controller层里面的@RequestMapping(value="getStudentInfo",method=RequestMethod.GET)中的value的值 -->

即:
localhost:8080/stuManage/getStudentInfo

 

查看一下eclipse的控制台,如下:

发现打印出来了我们编写的测试输出语句“进入到getStudentInfo方法中了,就代表你能访问到方法中了

 

posted @ 2017-12-27 20:27  365天小人物  阅读(2685)  评论(0编辑  收藏  举报