Archetype Created Web Application
contextConfigLocation
/WEB-INF/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
dispatcher
org.springframework.web.servlet.DispatcherServlet
1
dispatcher
/
web.xml文件中的配置,主要是servlet部分的配置
dispatcher-servlet.xml文件中的配置
userMapper.xml文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
学生信息查询
id
name
birt
sex
addr
lead
学号
姓名
生日
性别
地址
辅导员
前端部分代码
package com.hundsun.zdr;
import com.google.gson.Gson;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* Created by ZDR on 2016/8/8.
*/
@Controller//标识为控制类
//@RequestMapping("/mvc")
public class mvcController {
@RequestMapping(value = "/hello",produces = "text/html;charset=UTF-8")//value指定请求的实际地址,produces指定返回的内容类型
@ResponseBody
public String hello(@RequestParam(value="id")String id,@RequestParam(value="name")String name,@RequestParam(value="birthday")String birthday,
@RequestParam(value="sex")String sex,@RequestParam(value="address")String address,@RequestParam(value="teacher")String teacher){
System.out.println("======controller start=========="+sex);
//mybatis的配置文件
String resource = "conf.xml";
//使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
InputStream is = Stu.class.getClassLoader().getResourceAsStream(resource);
//构建sqlSession的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
//创建能执行映射文件中sql的sqlSession
SqlSession session = sessionFactory.openSession();
/**
* 映射sql的标识字符串,
* me.gacl.mapping.userMapper是userMapper.xml文件中mapper标签的namespace属性的值,
* getUser是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL
*/
String statement = "me.gacl.mapping.userMapper.getUser";//映射sql的标识字符串
//执行查询
Stu checkStu = new Stu();
checkStu.setId(id);
checkStu.setName(name);
checkStu.setSex(sex);
checkStu.setBirthday(birthday);
checkStu.setAddress(address);
checkStu.setTeacher(teacher);
List resultList = new ArrayList();
resultList = session.selectList(statement,checkStu);
System.out.println("===result==="+resultList.toString());
return (new Gson()).toJson(resultList);//数据返回给页面
}
}
控制类的代码,因为处理的逻辑很简单,逻辑部分和控制类写在的一起,以后做较大的项目时,应该把逻辑部分建立一个新的类,层次清晰。
package com.hundsun.zdr;
/**
* Created by ZDR on 2016/8/8.
*/
public class Stu {
public String name;
public String id;
public String birthday;
public String sex;
public String address;
public String teacher;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTeacher() {
return teacher;
}
public void setTeacher(String teacher) {
this.teacher = teacher;
}
public Stu(){
}
public Stu(String name, String id, String birthday, String sex, String address, String teacher) {
this.name = name;
this.id = id;
this.birthday = birthday;
this.sex = sex;
this.address = address;
this.teacher = teacher;
}
@Override
public String toString() {
return "Stu{" +
"name='" + name + '\'' +
", id='" + id + '\'' +
", birthday='" + birthday + '\'' +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
", teacher='" + teacher + '\'' +
'}';
}
}
这一部分就不多解释了,实体类。值得一提的是实体类的属性名称要和数据库中字段名一致,这样在userMapper.xml文件中才能直接使用参数名称!即使名称不一致也应该有方法解决,但是名称一致是麻烦最少的方法了。
这次算是之前项目的升级吧,添加了Spring mvc框架、使用了myBatis。前端部分还是基本延续了之前的项目
以上部分就是我个人的学习感受,对于WEB这一块我还是个小白,有不妥的地方还请各位大牛指出,谢谢大家