導包以後咱們先寫一個方法測試一下,好比: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方法中了,就表明你能訪問到方法中了