Spring MVC 简单查询与修改
一、mvc设计模式,创建 Web 应用程序的模式:
spring mvc : 基于web层(请求处理和响应发送 b/s)的框架. (sturts1,struts2,spring mvc ,spring webFlux等等都属于web层框架)
二、传统的b/s架构与spring mvc的架构有区别
mvc模式:m (model ) v(view--jsp,html) c (control--servlet) 1)传统的b/s架构 客户端------请求-----控制器(servlet)----->封装数据model中 -------响应-----------jsp-------------------------| 缺点: 1. servlet生产周期长,大量servlet的产生。占据大量内存。 2)spring mvc 的b/s架构 优点:1.只有1个servlet。 2.分工明确 中央处理器(servlet):接受请求,分配任务,发送响应 映射器:根据url,确定找谁(处理器),干什么(办什么业务),将其告诉老大 适配器:根据映射器结果,找到对应的处理器,完成任务,并将结果返回给老大 视图解析器:解析处理器返回的结果(model view)---返回哪个jsp,里面要显示什么数据 渲染--->|客户
三、spring mvc开发的流程
第1步: mybatis spring mvc需要的包(spring+ spring-web包+spring-webmvc)
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.11</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.caches/mybatis-ehcache -->
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-spec</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.41</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>5.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.liferay/com.fasterxml.jackson.databind -->
<dependency>
<groupId>com.liferay</groupId>
<artifactId>com.fasterxml.jackson.databind</artifactId>
<version>2.10.3.LIFERAY-PATCHED-1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.catalina/com.springsource.org.apache.catalina -->
<dependency>
<groupId>org.apache.catalina</groupId>
<artifactId>com.springsource.org.apache.catalina</artifactId>
<version>7.0.26</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson/jackson-jaxrs-base -->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson/jackson-json-schema-plugin -->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson/jackson-base -->
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-base</artifactId>
<version>2.12.0</version>
<type>pom</type>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.12.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson/jackson-bom -->
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
<version>2.12.0</version>
<type>pom</type>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jms -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>5.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-messaging -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>5.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-oxm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.25</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
第2步:web.xml配置中央处理器(spring 写好的一个servelt)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringWeb_02</display-name>
<!--一、spring的ioc容器配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 三、spring的编码过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 二、中央处理器(DispatcherServlet):映射器、适配器、视图解析器 -->
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
第3步:配置spring-web.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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!-- 开启注解扫描 .** 所有所有包以及子包-->
<context:component-scan base-package="cc.**"/>
<!-- 1.处理器映射器 -->
<!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>-->
<!-- 2.处理器适配器 -->
<!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>-->
<!-- 3.试图解析器 -->
<mvc:annotation-driven/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 解析jstl标签 -->
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<!-- 动态页面的前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 动态页面的后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
第4步:定义处理器
@Controller
@requestMapping
在定义处理器前把接口实现类mapper都建好
package cc.entity;
import java.io.Serializable;
import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonFormat;
public class Users implements Serializable {
/**
*
* This field was generated by MyBatis Generator. This field corresponds to the
* database column users.id
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
private Integer id;
/**
*
* This field was generated by MyBatis Generator. This field corresponds to the
* database column users.username
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
private String username;
/**
*
* This field was generated by MyBatis Generator. This field corresponds to the
* database column users.birthday
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
// private String birthday;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date birthday;
/**
*
* This field was generated by MyBatis Generator. This field corresponds to the
* database column users.sex
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
private String sex;
/**
*
* This field was generated by MyBatis Generator. This field corresponds to the
* database column users.address
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
private String address;
/**
* This field was generated by MyBatis Generator. This field corresponds to the
* database table users
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
private static final long serialVersionUID = 1L;
/**
* This method was generated by MyBatis Generator. This method returns the value
* of the database column users.id
*
* @return the value of users.id
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
public Integer getId() {
return id;
}
/**
* This method was generated by MyBatis Generator. This method sets the value of
* the database column users.id
*
* @param id the value for users.id
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
public void setId(Integer id) {
this.id = id;
}
/**
* This method was generated by MyBatis Generator. This method returns the value
* of the database column users.username
*
* @return the value of users.username
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
public String getUsername() {
return username;
}
/**
* This method was generated by MyBatis Generator. This method sets the value of
* the database column users.username
*
* @param username the value for users.username
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
public void setUsername(String username) {
this.username = username == null ? null : username.trim();
}
/**
* This method was generated by MyBatis Generator. This method returns the value
* of the database column users.birthday
*
* @return the value of users.birthday
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
//@JsonFormat(pattern="yyyy-MM-dd")
public Date getBirthday() {
return birthday;
}
/**
* This method was generated by MyBatis Generator. This method sets the value of
* the database column users.birthday
*
* @param birthday the value for users.birthday
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
/**
* This method was generated by MyBatis Generator. This method returns the value
* of the database column users.sex
*
* @return the value of users.sex
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
public String getSex() {
return sex;
}
/**
* This method was generated by MyBatis Generator. This method sets the value of
* the database column users.sex
*
* @param sex the value for users.sex
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
public void setSex(String sex) {
this.sex = sex == null ? null : sex.trim();
}
/**
* This method was generated by MyBatis Generator. This method returns the value
* of the database column users.address
*
* @return the value of users.address
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
public String getAddress() {
return address;
}
/**
* This method was generated by MyBatis Generator. This method sets the value of
* the database column users.address
*
* @param address the value for users.address
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
public void setAddress(String address) {
this.address = address == null ? null : address.trim();
}
/**
* This method was generated by MyBatis Generator. This method corresponds to
* the database table users
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", username=").append(username);
sb.append(", birthday=").append(birthday);
sb.append(", sex=").append(sex);
sb.append(", address=").append(address);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]\n");
return sb.toString();
}
/**
* This method was generated by MyBatis Generator. This method corresponds to
* the database table users
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
Users other = (Users) that;
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
&& (this.getUsername() == null ? other.getUsername() == null
: this.getUsername().equals(other.getUsername()))
&& (this.getBirthday() == null ? other.getBirthday() == null
: this.getBirthday().equals(other.getBirthday()))
&& (this.getSex() == null ? other.getSex() == null : this.getSex().equals(other.getSex()))
&& (this.getAddress() == null ? other.getAddress() == null
: this.getAddress().equals(other.getAddress()));
}
/**
* This method was generated by MyBatis Generator. This method corresponds to
* the database table users
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getUsername() == null) ? 0 : getUsername().hashCode());
result = prime * result + ((getBirthday() == null) ? 0 : getBirthday().hashCode());
result = prime * result + ((getSex() == null) ? 0 : getSex().hashCode());
result = prime * result + ((getAddress() == null) ? 0 : getAddress().hashCode());
return result;
}
}
package cc.entity;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
public class UsersExample {
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database table users
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
protected String orderByClause;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database table users
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
protected boolean distinct;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database table users
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
protected List<Criteria> oredCriteria;
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
public UsersExample() {
oredCriteria = new ArrayList<Criteria>();
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
public void setOrderByClause(String orderByClause) {
this.orderByClause = orderByClause;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
public String getOrderByClause() {
return orderByClause;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
public void setDistinct(boolean distinct) {
this.distinct = distinct;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
public boolean isDistinct() {
return distinct;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
public List<Criteria> getOredCriteria() {
return oredCriteria;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
public void or(Criteria criteria) {
oredCriteria.add(criteria);
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
public Criteria or() {
Criteria criteria = createCriteriaInternal();
oredCriteria.add(criteria);
return criteria;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
public Criteria createCriteria() {
Criteria criteria = createCriteriaInternal();
if (oredCriteria.size() == 0) {
oredCriteria.add(criteria);
}
return criteria;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
protected Criteria createCriteriaInternal() {
Criteria criteria = new Criteria();
return criteria;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
public void clear() {
oredCriteria.clear();
orderByClause = null;
distinct = false;
}
/**
* This class was generated by MyBatis Generator.
* This class corresponds to the database table users
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
protected abstract static class GeneratedCriteria {
protected List<Criterion> criteria;
protected GeneratedCriteria() {
super();
criteria = new ArrayList<Criterion>();
}
public boolean isValid() {
return criteria.size() > 0;
}
public List<Criterion> getAllCriteria() {
return criteria;
}
public List<Criterion> getCriteria() {
return criteria;
}
protected void addCriterion(String condition) {
if (condition == null) {
throw new RuntimeException("Value for condition cannot be null");
}
criteria.add(new Criterion(condition));
}
protected void addCriterion(String condition, Object value, String property) {
if (value == null) {
throw new RuntimeException("Value for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value));
}
protected void addCriterion(String condition, Object value1, Object value2, String property) {
if (value1 == null || value2 == null) {
throw new RuntimeException("Between values for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value1, value2));
}
protected void addCriterionForJDBCDate(String condition, Date value, String property) {
if (value == null) {
throw new RuntimeException("Value for " + property + " cannot be null");
}
addCriterion(condition, new java.sql.Date(value.getTime()), property);
}
protected void addCriterionForJDBCDate(String condition, List<Date> values, String property) {
if (values == null || values.size() == 0) {
throw new RuntimeException("Value list for " + property + " cannot be null or empty");
}
List<java.sql.Date> dateList = new ArrayList<java.sql.Date>();
Iterator<Date> iter = values.iterator();
while (iter.hasNext()) {
dateList.add(new java.sql.Date(iter.next().getTime()));
}
addCriterion(condition, dateList, property);
}
protected void addCriterionForJDBCDate(String condition, Date value1, Date value2, String property) {
if (value1 == null || value2 == null) {
throw new RuntimeException("Between values for " + property + " cannot be null");
}
addCriterion(condition, new java.sql.Date(value1.getTime()), new java.sql.Date(value2.getTime()), property);
}
public Criteria andIdIsNull() {
addCriterion("id is null");
return (Criteria) this;
}
public Criteria andIdIsNotNull() {
addCriterion("id is not null");
return (Criteria) this;
}
public Criteria andIdEqualTo(Integer value) {
addCriterion("id =", value, "id");
return (Criteria) this;
}
public Criteria andIdNotEqualTo(Integer value) {
addCriterion("id <>", value, "id");
return (Criteria) this;
}
public Criteria andIdGreaterThan(Integer value) {
addCriterion("id >", value, "id");
return (Criteria) this;
}
public Criteria andIdGreaterThanOrEqualTo(Integer value) {
addCriterion("id >=", value, "id");
return (Criteria) this;
}
public Criteria andIdLessThan(Integer value) {
addCriterion("id <", value, "id");
return (Criteria) this;
}
public Criteria andIdLessThanOrEqualTo(Integer value) {
addCriterion("id <=", value, "id");
return (Criteria) this;
}
public Criteria andIdIn(List<Integer> values) {
addCriterion("id in", values, "id");
return (Criteria) this;
}
public Criteria andIdNotIn(List<Integer> values) {
addCriterion("id not in", values, "id");
return (Criteria) this;
}
public Criteria andIdBetween(Integer value1, Integer value2) {
addCriterion("id between", value1, value2, "id");
return (Criteria) this;
}
public Criteria andIdNotBetween(Integer value1, Integer value2) {
addCriterion("id not between", value1, value2, "id");
return (Criteria) this;
}
public Criteria andUsernameIsNull() {
addCriterion("username is null");
return (Criteria) this;
}
public Criteria andUsernameIsNotNull() {
addCriterion("username is not null");
return (Criteria) this;
}
public Criteria andUsernameEqualTo(String value) {
addCriterion("username =", value, "username");
return (Criteria) this;
}
public Criteria andUsernameNotEqualTo(String value) {
addCriterion("username <>", value, "username");
return (Criteria) this;
}
public Criteria andUsernameGreaterThan(String value) {
addCriterion("username >", value, "username");
return (Criteria) this;
}
public Criteria andUsernameGreaterThanOrEqualTo(String value) {
addCriterion("username >=", value, "username");
return (Criteria) this;
}
public Criteria andUsernameLessThan(String value) {
addCriterion("username <", value, "username");
return (Criteria) this;
}
public Criteria andUsernameLessThanOrEqualTo(String value) {
addCriterion("username <=", value, "username");
return (Criteria) this;
}
public Criteria andUsernameLike(String value) {
addCriterion("username like", value, "username");
return (Criteria) this;
}
public Criteria andUsernameNotLike(String value) {
addCriterion("username not like", value, "username");
return (Criteria) this;
}
public Criteria andUsernameIn(List<String> values) {
addCriterion("username in", values, "username");
return (Criteria) this;
}
public Criteria andUsernameNotIn(List<String> values) {
addCriterion("username not in", values, "username");
return (Criteria) this;
}
public Criteria andUsernameBetween(String value1, String value2) {
addCriterion("username between", value1, value2, "username");
return (Criteria) this;
}
public Criteria andUsernameNotBetween(String value1, String value2) {
addCriterion("username not between", value1, value2, "username");
return (Criteria) this;
}
public Criteria andBirthdayIsNull() {
addCriterion("birthday is null");
return (Criteria) this;
}
public Criteria andBirthdayIsNotNull() {
addCriterion("birthday is not null");
return (Criteria) this;
}
public Criteria andBirthdayEqualTo(Date value) {
addCriterionForJDBCDate("birthday =", value, "birthday");
return (Criteria) this;
}
public Criteria andBirthdayNotEqualTo(Date value) {
addCriterionForJDBCDate("birthday <>", value, "birthday");
return (Criteria) this;
}
public Criteria andBirthdayGreaterThan(Date value) {
addCriterionForJDBCDate("birthday >", value, "birthday");
return (Criteria) this;
}
public Criteria andBirthdayGreaterThanOrEqualTo(Date value) {
addCriterionForJDBCDate("birthday >=", value, "birthday");
return (Criteria) this;
}
public Criteria andBirthdayLessThan(Date value) {
addCriterionForJDBCDate("birthday <", value, "birthday");
return (Criteria) this;
}
public Criteria andBirthdayLessThanOrEqualTo(Date value) {
addCriterionForJDBCDate("birthday <=", value, "birthday");
return (Criteria) this;
}
public Criteria andBirthdayIn(List<Date> values) {
addCriterionForJDBCDate("birthday in", values, "birthday");
return (Criteria) this;
}
public Criteria andBirthdayNotIn(List<Date> values) {
addCriterionForJDBCDate("birthday not in", values, "birthday");
return (Criteria) this;
}
public Criteria andBirthdayBetween(Date value1, Date value2) {
addCriterionForJDBCDate("birthday between", value1, value2, "birthday");
return (Criteria) this;
}
public Criteria andBirthdayNotBetween(Date value1, Date value2) {
addCriterionForJDBCDate("birthday not between", value1, value2, "birthday");
return (Criteria) this;
}
public Criteria andSexIsNull() {
addCriterion("sex is null");
return (Criteria) this;
}
public Criteria andSexIsNotNull() {
addCriterion("sex is not null");
return (Criteria) this;
}
public Criteria andSexEqualTo(String value) {
addCriterion("sex =", value, "sex");
return (Criteria) this;
}
public Criteria andSexNotEqualTo(String value) {
addCriterion("sex <>", value, "sex");
return (Criteria) this;
}
public Criteria andSexGreaterThan(String value) {
addCriterion("sex >", value, "sex");
return (Criteria) this;
}
public Criteria andSexGreaterThanOrEqualTo(String value) {
addCriterion("sex >=", value, "sex");
return (Criteria) this;
}
public Criteria andSexLessThan(String value) {
addCriterion("sex <", value, "sex");
return (Criteria) this;
}
public Criteria andSexLessThanOrEqualTo(String value) {
addCriterion("sex <=", value, "sex");
return (Criteria) this;
}
public Criteria andSexLike(String value) {
addCriterion("sex like", value, "sex");
return (Criteria) this;
}
public Criteria andSexNotLike(String value) {
addCriterion("sex not like", value, "sex");
return (Criteria) this;
}
public Criteria andSexIn(List<String> values) {
addCriterion("sex in", values, "sex");
return (Criteria) this;
}
public Criteria andSexNotIn(List<String> values) {
addCriterion("sex not in", values, "sex");
return (Criteria) this;
}
public Criteria andSexBetween(String value1, String value2) {
addCriterion("sex between", value1, value2, "sex");
return (Criteria) this;
}
public Criteria andSexNotBetween(String value1, String value2) {
addCriterion("sex not between", value1, value2, "sex");
return (Criteria) this;
}
public Criteria andAddressIsNull() {
addCriterion("address is null");
return (Criteria) this;
}
public Criteria andAddressIsNotNull() {
addCriterion("address is not null");
return (Criteria) this;
}
public Criteria andAddressEqualTo(String value) {
addCriterion("address =", value, "address");
return (Criteria) this;
}
public Criteria andAddressNotEqualTo(String value) {
addCriterion("address <>", value, "address");
return (Criteria) this;
}
public Criteria andAddressGreaterThan(String value) {
addCriterion("address >", value, "address");
return (Criteria) this;
}
public Criteria andAddressGreaterThanOrEqualTo(String value) {
addCriterion("address >=", value, "address");
return (Criteria) this;
}
public Criteria andAddressLessThan(String value) {
addCriterion("address <", value, "address");
return (Criteria) this;
}
public Criteria andAddressLessThanOrEqualTo(String value) {
addCriterion("address <=", value, "address");
return (Criteria) this;
}
public Criteria andAddressLike(String value) {
addCriterion("address like", value, "address");
return (Criteria) this;
}
public Criteria andAddressNotLike(String value) {
addCriterion("address not like", value, "address");
return (Criteria) this;
}
public Criteria andAddressIn(List<String> values) {
addCriterion("address in", values, "address");
return (Criteria) this;
}
public Criteria andAddressNotIn(List<String> values) {
addCriterion("address not in", values, "address");
return (Criteria) this;
}
public Criteria andAddressBetween(String value1, String value2) {
addCriterion("address between", value1, value2, "address");
return (Criteria) this;
}
public Criteria andAddressNotBetween(String value1, String value2) {
addCriterion("address not between", value1, value2, "address");
return (Criteria) this;
}
}
/**
* This class was generated by MyBatis Generator.
* This class corresponds to the database table users
*
* @mbg.generated do_not_delete_during_merge Sat Aug 03 10:42:16 CST 2019
*/
public static class Criteria extends GeneratedCriteria {
protected Criteria() {
super();
}
}
/**
* This class was generated by MyBatis Generator.
* This class corresponds to the database table users
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
public static class Criterion {
private String condition;
private Object value;
private Object secondValue;
private boolean noValue;
private boolean singleValue;
private boolean betweenValue;
private boolean listValue;
private String typeHandler;
public String getCondition() {
return condition;
}
public Object getValue() {
return value;
}
public Object getSecondValue() {
return secondValue;
}
public boolean isNoValue() {
return noValue;
}
public boolean isSingleValue() {
return singleValue;
}
public boolean isBetweenValue() {
return betweenValue;
}
public boolean isListValue() {
return listValue;
}
public String getTypeHandler() {
return typeHandler;
}
protected Criterion(String condition) {
super();
this.condition = condition;
this.typeHandler = null;
this.noValue = true;
}
protected Criterion(String condition, Object value, String typeHandler) {
super();
this.condition = condition;
this.value = value;
this.typeHandler = typeHandler;
if (value instanceof List<?>) {
this.listValue = true;
} else {
this.singleValue = true;
}
}
protected Criterion(String condition, Object value) {
this(condition, value, null);
}
protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
super();
this.condition = condition;
this.value = value;
this.secondValue = secondValue;
this.typeHandler = typeHandler;
this.betweenValue = true;
}
protected Criterion(String condition, Object value, Object secondValue) {
this(condition, value, secondValue, null);
}
}
}
package cc.mapper;
import java.util.List;
import cc.entity.Users;
import cc.entity.UsersExample;
import org.apache.ibatis.annotations.Param;
public interface UsersMapper {
List<Users> selectByExample(UsersExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
Users selectByPrimaryKey(Integer id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
int updateByExampleSelective(@Param("record") Users record, @Param("example") UsersExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
int updateByExample(@Param("record") Users record, @Param("example") UsersExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
int updateByPrimaryKeySelective(Users record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Sat Aug 03 10:42:16 CST 2019
*/
int updateByPrimaryKey(Users record);
int deleteByPrimaryKey(Integer id);
int deleteBatch(@Param("ids") Integer[] ids);
}
定义UsersService接口
package cc.service;
import cc.entity.Users;
import java.util.List;
public interface UsersService {
/**
*
* @Title: list
* @Description: 获取所有用户的所有信息
* @param @return
* @return List<Users>
* @throws
*/
List<Users> list();
/**
* @Title: selectById
* @Description: 根据id查询用户信息
* @param @param uid
* @param @return
* @return Users
* @throws
*/
Users selectById(int uid);
/**
* @Title: update
* @Description: 根据id来修改用户信息
* @param @param user
* @param @return
* @return int
* @throws
*/
int update(Users user);
}
实现impl
package cc.service.impl;
import java.util.List;
import cc.entity.Users;
import cc.mapper.UsersMapper;
import cc.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UsersServiceImpl implements UsersService {
@Autowired
private UsersMapper usersMapper;
@Override
public List<Users> list() {
return usersMapper.selectByExample(null);
}
@Override
public Users selectById(int uid) {
return usersMapper.selectByPrimaryKey(uid);
}
@Override
public int update(Users user) {
return usersMapper.updateByPrimaryKey(user);
}
}
实现Controller
package cc.web;
import java.util.List;
import cc.entity.Users;
import cc.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
//http"://localhost:8080/项目名/user/list
@Controller
@RequestMapping("/user")
public class UsersController {
@Autowired
private UsersService userService;
@RequestMapping("/list")
public ModelAndView list() {
ModelAndView mv= new ModelAndView();
//1.调用用户的service层的方法获取所有用户所有数据
List<Users> userses = userService.list();
//2.将数据绑定mv上
mv.addObject("userses",userses);
//3.设置视图
mv.setViewName("user/list");
//4.放回mv
return mv;
}
/**
* @Title: toUpdate
* @Description: 去修改页面
* @param @param uid
* @param @return
* @return ModelAndView
* @throws
注意:1.默认情况下这个方法的参数名必须和请求的url请求参数名一致
*/
@RequestMapping("/to_edit")
public ModelAndView toUpdate(int uid) {
System.out.println("====进入toUpdate===uid=>"+uid);
ModelAndView mv = new ModelAndView();
//1.查询该用户的所有信息 web--servcice-maper-数据
Users users = userService.selectById(uid);
//2.将该用户数据绑定到mv
mv.addObject("user", users);
//3.将修改页面的jsp名称绑定到mv中
mv.setViewName("user/edit");
//4.返回mv
return mv;
}
/**
* @Title: update
* @Description: 更新用户信息的方法
* @param @param user
* @param @return
* @return ModelAndView
* @throws
注意:edit.jsp表单元素的name值必须与update的参数实体类
的对应属性名一致!!
*/
@RequestMapping("/update")
public ModelAndView update(Users user) {
System.out.println("====进入update===user="+user);
ModelAndView mv=new ModelAndView();
//1.调用service更新用户信息
int result = userService.update(user);
//2.绑定视图
if(result>0) {//更新成功
//重定向 ,user/list的映射的list()方法,再由list()跳转到user/list.jsp
mv.setViewName("redirect:../user/list");
}else {//更新失败
}
//3.返回mv
return mv;
}
}
【补充】
@Component这个注解 在后期spring后期版本中进行细化
@Controller ----web层
@Service -----业务层
@Repository -----持久层 spring data jpa spring JDBCTemplate
四:jsp中关于Date类型数据的显示
1、方式:jsp的jstl的format库 (c库)
第1步:<%@taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt"%>
第2步:<fmt:formatDate value="${user.birthday}" pattern="yyyy-MM-dd HH:mm:ss"/>
将controller传递过来的Date类型数据,格式成指定格式的字符串日期
注:HH是24小时制 hh是12小时制
五:spring mvc 处理器@controller的参数的绑定
5.1 基本类型参数绑定 : 自动绑定 要求:处理器方法的参数必须与url的参数一致。 5.2 pojo对象类型参数绑定: 自动绑定 要求:表达的元素的name必须与pojo的属性名一致
补充:
1)http请求的响应码:
404 找打不资源
500 系统代码错误
200 请求成功
400 Bad Request 请求无法接受--一般参数绑定失败【重点】
405 请求方式不支持
2)spring mvc处理日期参数绑定的问题【重点】
方式一:@DateTimeFormat(pattern="yyyy-MM-dd") 实现
前提:spring-web.xml开启mvc注解配置
<mvc:annotation-driven></mvc:annotation-driven>替换
<!-- 1.映射器:HandlerMapping url==>handler -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!-- 2.适配器: HandlerAdatper 调用 Handler==>Controller-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
mvc:annotation-driven支持@DateTimeFomat和@NumberFormat
注意:mvc:annotation-driven配置自动加载RequestMappingHandlerMapping和RequestMappingHandlerAdapter
方法二:@InitBinder只会对当前Controller的String的日期转Date的日期进行自动转换
方法三:spring mvc的扩展插件-HandlerMethodArgumentResovle(了解)
:表单----->controller的方法edit(Users users)
HandlerMethodArgumentResovle用于处理客户端将请求参数传递给Controller方法
进行参数的自动绑定
第1步:定义一个Date注解
public @Interface 注解名称{
//属性名
}
第2步:在Controller方法中使用Date注解
@RequestMapping("/edit2")
public String edit(@Date(params="birthday",pattern="yyyy-MM-dd")Users users) {}
第3步:定义spring mvc参数处理扩展接口HandlerMethodArgumentResovler
第4步:spring mvc扩展插件的实现类 注入到spring mvc中
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class="com.cc.annotation.support.DateHandlerMethodArgumentResolver"/>
</mvc:argument-resolvers>
</mvc:annotation-driven>
第5步:测试
5.3 控制的方法默认参数:request ,response,model,session,application
5.4 集合
1、数组
注意:批量删除不要逐个删,应该通过batch的删除语句
delete from users where id in (33,57)
2、list集合 (自学)
3、参数Map集合(自学)
5.5 Controller参数其他东西
1、@RequestParam
@RequestParam(name="pageNum",defaultValue="1")
作用1) 可以设置默认值
作用2) 参数映射 前端传过来的参数名和后端controller接口的参数名不一致
2、restFul风格--参数传递
http://localhost:8080/应用名/user/delete?id=33 非restFul风格传参
http://localhost:8080/应用名/user/delete/33 restFul风格传参
注意:需要@PathVariable 进行参数绑定
特别注意:必须在web.xml中配置spring的编码过滤器,否则乱码。
六:controller的方法返回值的问题
1)返回ModelAndView
controller方法中定义ModelAndView对象并返回,对象中可添加model数据、指定view。
2)String 配合Model参数
String---->视图名称
Model---> 数据
4)void
在controller方法形参上可以定义request和response,使用request或response指定响应结果:
4.1.使用request转向页面,如下:
request.getRequestDispatcher("页面路径").forward(request, response);
4.2.通过response页面重定向:
response.sendRedirect("url")
4.3.通过response指定响应结果,例如响应json数据如下:
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
response.getWriter().write("json串");
七,编写jsp运行
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table width="80%" border="1">
<tr>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>生日</th>
<th>地址</th>
<th>操作</th>
</tr>
<c:forEach items="${userses}" var="user">
<tr>
<th>${user.id }</th>
<th>${user.username}</th>
<th>${user.sex}</th>
<th>${user.birthday }</th>
<th>${user.address}</th>
<th>
<a href="${pageContext.request. contextPath}/user/to_edit?uid=${user.id}">修改</a>
</th>
</tr>
</c:forEach>
</table>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/update" method="post">
<table border="1" align="center" width="50%" height="100px">
<caption><h1>Java6班-用户系统</h1></caption>
<tr>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>生日</th>
<th>住址</th>
</tr>
<tr align="center">
<td ><input value="${user.id }" name="id" readonly="readonly"/></td>
<td><input type="text" name="username" value="${user.username }"/> </td>
<td><input type="text" name="sex" value="${user.sex }"/> </td>
<td>
<!-- html5 中新增的标签:日期标签 -->
<input type="date" name="birthday"
value="<fmt:formatDate value='${user.birthday}' pattern='yyyy-MM-dd'/>"/>
</td>
<td><input type="text" name="address" value=" ${user.address}"/> </td>
</tr>
<tr>
<td colspan="5" align="center">
<input type="submit" value="更新"/>
</td>
</tr>
</table>
</form>
</body>
</html>
注意:UsersMapper.xml,beans.xml等就没写了。上文有
浙公网安备 33010602011771号