Student.java
package cn.weiwei.domain;
import java.math.BigDecimal;
import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
public class Student {
//id
private Long id;
//姓名
private String name;
//年龄
private Integer age;
//性别
private Boolean sex;
//工资
private BigDecimal wages;
//生日
//转换前台传过来的时间格式
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date date;
//当前日期
private Date currentdate =new Date();
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(Long id, String name, Integer age, Boolean sex, BigDecimal wages, Date date, Date currentdate) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.wages = wages;
this.date = date;
this.currentdate = currentdate;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getSex() {
return sex;
}
public void setSex(Boolean sex) {
this.sex = sex;
}
public BigDecimal getWages() {
return wages;
}
public void setWages(BigDecimal wages) {
this.wages = wages;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Date getCurrentdate() {
return currentdate;
}
public void setCurrentdate(Date currentdate) {
this.currentdate = currentdate;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", wages=" + wages + ", date="
+ date + ", currentdate=" + currentdate + "]";
}
}
StudentMapper.java
package cn.weiwei.mapper;
import java.util.List;
import cn.weiwei.domain.Student;
import cn.weiwei.quesy.QuesyStudent;
public interface StudentMapper {
//查询使用
List<Student> findAll();
//根据id查询
Student findOne(Long id);
//修改
void update(Student s);
//删除
void delete(Long id);
//新增
void add(Student s);
//条件查询
List<Student> QuesyStudentAll(QuesyStudent quesy);
}
StudentMapper.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="cn.weiwei.mapper.StudentMapper">
<select id="findAll" resultType="Student">
select * from student
</select>
<!-- 查询 -->
<select id="QuesyStudentAll" resultType="Student">
select * from student
<where>
<if test="name!=null and name!=''">
and name like concat('%',#{name},'%')
</if>
<if test="ageScope!=null and ageScope!=''">
<if test="ageScope==1">
<![CDATA[and age>=0 and age<18 ]]>
</if>
<if test="ageScope==2">
<![CDATA[and age>=18 and age<50 ]]>
</if>
<if test="ageScope==3">
<![CDATA[and age>=50 ]]>
</if>
</if>
<if test="sexScope!=null and sexScope!=''">
<if test="sexScope==1">
and sex=1
</if>
<if test="sexScope==2">
and sex=0
</if>
</if>
</where>
</select>
<delete id="delete">
delete from student where id= #{id}
</delete>
<!-- 新增 -->
<insert id="add">
insert into student(name,age,sex,wages,date) values(
#{name},
#{age},
#{sex},
#{wages},
#{date}
)
</insert>
<!-- 根据id查询 -->
<select id="findOne" resultType="Student">
select * from student where id= #{id}
</select>
<!-- 修改 -->
<update id="update">
update student set
name=#{name},age=#{age},sex=#{sex},wages=#{wages},date=#{date} where
id=#{id}
</update>
</mapper>
ISttdentService.java
package cn.weiwei.service;
import java.util.List;
import cn.weiwei.domain.Student;
import cn.weiwei.quesy.QuesyStudent;
public interface IStudentService {
//查询使用
List<Student> findAll();
//根据id查询
Student findOne(Long id);
//修改
void update(Student s);
//删除
void delete(Long id);
//新增
void add(Student s);
//条件查询
List<Student> QuesyStudentAll(QuesyStudent quesy);
}
StudentServiceImpl.java
package cn.weiwei.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.weiwei.domain.Student;
import cn.weiwei.mapper.StudentMapper;
import cn.weiwei.quesy.QuesyStudent;
import cn.weiwei.service.IStudentService;
@Service
public class StudentServiceImpl implements IStudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public List<Student> findAll() {
// TODO Auto-generated method stub
return studentMapper.findAll();
}
@Override
public Student findOne(Long id) {
// TODO Auto-generated method stub
return studentMapper.findOne(id);
}
@Override
public void update(Student s) {
// TODO Auto-generated method stub
studentMapper.update(s);
}
@Override
public void delete(Long id) {
// TODO Auto-generated method stub
studentMapper.delete(id);
}
@Override
public void add(Student s) {
// TODO Auto-generated method stub
studentMapper.add(s);
}
@Override
public List<Student> QuesyStudentAll(QuesyStudent quesy) {
// TODO Auto-generated method stub
return studentMapper.QuesyStudentAll(quesy);
}
}
StudentConyroller.java
package cn.weiwei.controller;
import java.util.List;
import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.weiwei.domain.Student;
import cn.weiwei.quesy.QuesyStudent;
import cn.weiwei.service.impl.StudentServiceImpl;
@Controller
@RequestMapping("/stu")
public class StudentController {
@Autowired
private StudentServiceImpl studentServiceImpl;
//查询方法
@RequestMapping("/list")
public String list(Model model,QuesyStudent quesy){
// List<Student> findAll = studentServiceImpl.findAll();
List<Student> findAll = studentServiceImpl.QuesyStudentAll(quesy);
System.out.println(quesy);
model.addAttribute("student", findAll);
model.addAttribute("quesy", quesy);
return "student/list";
}
//删除方法
@RequestMapping("/del")
public String del(Long id){
studentServiceImpl.delete(id);
return "redirect:/stu/list";
}
//新增跳转页面 新增和修改
@RequestMapping("/jump")
public String Jump(Long id,Model model){
if(id!=null){
Student findOne = studentServiceImpl.findOne(id);
model.addAttribute("one", findOne);
System.out.println(findOne);
return "student/update";
}
return "student/add";
}
//新增
@RequestMapping("/add")
private String add(Student student) {
studentServiceImpl.add(student);
return "redirect:/stu/list";
}
//修改
@RequestMapping("/update")
public String update(Student student){
studentServiceImpl.update(student);
return "redirect:/stu/list";
}
}
applicationContext-mvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描控制层 -->
<context:component-scan base-package="cn.weiwei.controller"></context:component-scan>
<!-- 放行静态资源 这里会关闭注解-->
<mvc:default-servlet-handler/>
<!-- 开启注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描service -->
<context:component-scan base-package="cn.weiwei.service"></context:component-scan>
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 注入mybatis核心配置文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:cn/weiwei/mapper/*Mapper.xml"></property>
<!-- 配置别名 -->
<property name="typeAliasesPackage" value="cn.weiwei.domain"></property>
</bean>
<!-- 管理mapper -->
<bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描mapper层,交给spring管理 -->
<property name="basePackage" value="cn.weiwei.mapper"></property>
<!-- 注意:不要配置sqlSessionFactory spring会配置 -->
</bean>
</beans>
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///ssm
jdbc.username=root
jdbc.password=root
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
table {
width: 70%;
border: 1px solid red;
}
table tr, tr td {
padding: 5px;
}
</style>
</head>
<body>
<a href="/stu/jump"><button style="background-color: blue;">新增</button></a>
<%-- ${student} --%>
<form action="/stu/list" method="post">
查询 姓名:<input type="text" name="name" value="${quesy.name}">
年龄<select name="ageScope">
<option value="">--全部--</option>
<option <c:if test="${quesy.ageScope==1}">selected="selected"</c:if> value="1">0~18</option>
<option <c:if test="${quesy.ageScope==2}">selected="selected"</c:if> value="2">18~50</option>
<option <c:if test="${quesy.ageScope==3}">selected="selected"</c:if> value="3">50以上</option>
</select>
性别:<select name="sexScope">
<option value="">--全部--</option>
<option <c:if test="${quesy.sexScope==1}">selected="selected"</c:if> value="1">男</option>
<option <c:if test="${quesy.sexScope==2}">selected="selected"</c:if> value="2">女</option>
</select>
<button>提交</button>
</form>
<table>
<tr style="background-color: turquoise">
<th>序号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>工资</th>
<th>出生年份</th>
<th>当前日期</th>
<th>操作</th>
</tr>
<c:forEach items="${student}" var="s" varStatus="stu">
<tr style="background-color: ${stu.count%2==0?'beige':'brown09'}">
<td>${stu.count}</td>
<td>${s.name}</td>
<td>${s.age}</td>
<td>${s.sex?"男":"女"}</td>
<td>${s.wages}</td>
<td>
<!-- 格式化日期时间 --> <fmt:formatDate value="${s.date}"
pattern="yyyy-MM-dd" />
</td>
<td><fmt:formatDate value="${s.currentdate}"
pattern="yyyy-MM-dd hh:MM:ss" /></td>
<td><a href="/stu/jump?id=${s.id}">修改</a>/<a href="/stu/del?id=${s.id}">删除</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1 align="center">新增</h1>
<form action="/stu/add">
姓名:<input type="text" name="name"><br>
年龄:<input type="number" name="age"><br>
性别 <input type="radio" value="1" name="sex" checked="checked" >男
<input type="radio" value="0" name="sex" >女<br>
工资:<input type="number" name="wages"><br>
生日:<input type="date" name="date"><br>
<button>提交</button>
</form>
</body>
</html>
update.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1 align="center">修改</h1>
<form action="/stu/update">
<!-- 隐身属性 -->
<input type="hidden" name="id" value="${one.id}">
姓名:<input type="text" name="name" value="${one.name}"><br>
年龄:<input type="number" name="age" value="${one.age}"><br>
性别 <input type="radio" value="1" name="sex" <c:if test="${one.sex}">checked="checked"</c:if> >男
<input type="radio" value="0" name="sex" <c:if test="${!one.sex}">checked="checked"</c:if> >女<br>
工资:<input type="number" name="wages" value="${one.wages}"><br>
<!-- 后台传过来的时间格式 -->
生日:<input type="date" name="date" value="<fmt:formatDate value="${one.date}" pattern="yyyy-MM-dd"/>"><br>
<button>提交</button>
</form>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!-- Spring容器初始化 监听spring启动 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置springmvc前端控制器 -->
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-mvc.xml</param-value>
</init-param>
<!-- 配置spring和tomcat一起加载 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!--指定字符编码-->
<init-param>
<!--因为当前成员变量为encoding,所以param-name必须为encoding-->
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<!--强制指定字符编码,即使request或response设置了字符编码,也会强制使用当前设置的,任何情况下强制使用此编码-->
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>