导包以后咱们先写一个方法测试一下,好比:html
我要在页面上显示以下内容java
这里要注意咱们数据库tb_student表中的学校给的只是一个ID,必须使用多表查询。mysql
SQL语句以下:web
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
先在数据库测试一下,获得以下数据:spring
发现查到的数据已经和咱们的页面对应上了,接下来让咱们写一个方法从后台查询一下数据。sql
后台从数据库取值咱们用的是Mybatis整合Spring的取值方式数据库
实体类studentInfo.class代码以下:apache
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; } }
里面先写一个方法的接口,咱们测试一下,代码以下:json
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); }
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>
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); } }
代码以下:
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
代码以下:
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是与的做用。
这里咱们直接在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); }
测试结果以下:
出现了这一句证实已经拿到值了,返回的是一个内存地址
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; } }
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>
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>
把项目添加到服务器的Tomcat中,而后启动服务器
浏览器中输入
<!-- localhost:8080/项目名/方法地址(即controller层里面的@RequestMapping(value="getStudentInfo",method=RequestMethod.GET)中的value的值 --> 即: localhost:8080/stuManage/getStudentInfo
查看一下eclipse的控制台,以下:
发现打印出来了咱们编写的测试输出语句“进入到getStudentInfo方法中了,就表明你能访问到方法中了