林炳文Evankaka原创作品。转载请注明出处http://blog.csdn.net/evankaka
摘要:这几天研究了下Ajax注册的方法,通过在注册时输入用户名或邮箱等,就可以判断这个用户是否存在,以免用户来注册,然后提交了,系统才提示该用户名或邮箱不可用。使用Ajax便可实现这一功能,看了网上的都是php的,想想索性来写一个SpringMVC+Spring+Mybatis的。文章内容用到了很多技术,包括javascript、jquery、json、e表达式等。
先来看看最终效果:
注册成功的样子:
注册过程中参数的检验:
下面,让我们开始这次的编程吧!
首先,数据库准备,新建一张用户表,并自己插入一些数据
CREATE TABLE
t_user
(
USER_ID INT NOT NULL AUTO_INCREMENT,
USER_NAME CHAR(30) NOT NULL,
USER_PASSWORD CHAR(10) NOT NULL,
USER_EMAIL CHAR(30) NOT NULL,
PRIMARY KEY (USER_ID)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
好,数据库建好了,接下来就是整个工程了。
一、工程整体
项目类型:Dynamic Web Project
开发环境:
Eclipse Java EE IDE for Web Developers.
Version: Luna Service Release 2 (4.4.2)
Build id: 20150219-0600
JDK版本:
jdk1.6.0_45
工程的其它参数:
1、用到的JAR包
json使用的包。这里下载
spring+sprngMvc的包。
mybatis的包
mysql连接的包
logger的包(日记 打印的)
如下:
2、需要的外部js
jquery-1.11.3.min.js
3、项目结构
这是还没有展开的
然后把各个都展开:
其中src放置java的文件、config放置SpringMVC+Spring+Mybatis的配置文件以及日记打印的log4j.properties
web-inf下面:js用来放置调用 的js文件,lib就是最上面说的jar包的位置,view是各个jsp文件
二、编程
2.1 java代码
这里分了5层,按照标准的web工程来
1、domain层
这里是使用Mybatis Generator自动生成的:User.java
package com.lin.domain;
public class User {
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_user.USER_ID
*
* @mbggenerated
*/
private Integer userId;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_user.USER_NAME
*
* @mbggenerated
*/
private String userName;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_user.USER_PASSWORD
*
* @mbggenerated
*/
private String userPassword;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_user.USER_EMAIL
*
* @mbggenerated
*/
private String userEmail;
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column t_user.USER_ID
*
* @return the value of t_user.USER_ID
*
* @mbggenerated
*/
public Integer getUserId() {
return userId;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_user.USER_ID
*
* @param userId the value for t_user.USER_ID
*
* @mbggenerated
*/
public void setUserId(Integer userId) {
this.userId = userId;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column t_user.USER_NAME
*
* @return the value of t_user.USER_NAME
*
* @mbggenerated
*/
public String getUserName() {
return userName;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_user.USER_NAME
*
* @param userName the value for t_user.USER_NAME
*
* @mbggenerated
*/
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 t_user.USER_PASSWORD
*
* @return the value of t_user.USER_PASSWORD
*
* @mbggenerated
*/
public String getUserPassword() {
return userPassword;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_user.USER_PASSWORD
*
* @param userPassword the value for t_user.USER_PASSWORD
*
* @mbggenerated
*/
public void setUserPassword(String userPassword) {
this.userPassword = userPassword == null ? null : userPassword.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column t_user.USER_EMAIL
*
* @return the value of t_user.USER_EMAIL
*
* @mbggenerated
*/
public String getUserEmail() {
return userEmail;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_user.USER_EMAIL
*
* @param userEmail the value for t_user.USER_EMAIL
*
* @mbggenerated
*/
public void setUserEmail(String userEmail) {
this.userEmail = userEmail == null ? null : userEmail.trim();
}
}
然后还有一个example文件:UserExample.java
package com.lin.domain;
import java.util.ArrayList;
import java.util.List;
public class UserExample {
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database table t_user
*
* @mbggenerated
*/
protected String orderByClause;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database table t_user
*
* @mbggenerated
*/
protected boolean distinct;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database table t_user
*
* @mbggenerated
*/
protected List<Criteria> oredCriteria;
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table t_user
*
* @mbggenerated
*/
public UserExample() {
oredCriteria = new ArrayList<Criteria>();
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table t_user
*
* @mbggenerated
*/
public void setOrderByClause(String orderByClause) {
this.orderByClause = orderByClause;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table t_user
*
* @mbggenerated
*/
public String getOrderByClause() {
return orderByClause;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table t_user
*
* @mbggenerated
*/
public void setDistinct(boolean distinct) {
this.distinct = distinct;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table t_user
*
* @mbggenerated
*/
public boolean isDistinct() {
return distinct;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table t_user
*
* @mbggenerated
*/
public List<Criteria> getOredCriteria() {
return oredCriteria;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table t_user
*
* @mbggenerated
*/
public void or(Criteria criteria) {
oredCriteria.add(criteria);
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table t_user
*
* @mbggenerated
*/
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 t_user
*
* @mbggenerated
*/
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 t_user
*
* @mbggenerated
*/
protected Criteria createCriteriaInternal() {
Criteria criteria = new Criteria();
return criteria;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table t_user
*
* @mbggenerated
*/
public void clear() {
oredCriteria.clear();
orderByClause = null;
distinct = false;
}
/**
* This class was generated by MyBatis Generator.
* This class corresponds to the database table t_user
*
* @mbggenerated
*/
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> 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));
}
public Criteria andUserIdIsNull() {
addCriterion("USER_ID is null");
return (Criteria) this;
}
public Criteria andUserIdIsNotNull() {
addCriterion("USER_ID is not null");
return (Criteria) this;
}
public Criteria andUserIdEqualTo(Integer value) {
addCriterion("USER_ID =", value, "userId");
return (Criteria) this;
}
public Criteria andUserIdNotEqualTo(Integer value) {
addCriterion("USER_ID <>", value, "userId");
return (Criteria) this;
}
public Criteria andUserIdGreaterThan(Integer value) {
addCriterion("USER_ID >", value, "userId");
return (Criteria) this;
}
public Criteria andUserIdGreaterThanOrEqualTo(Integer value) {
addCriterion("USER_ID >=", value, "userId");
return (Criteria) this;
}
public Criteria andUserIdLessThan(Integer value) {
addCriterion("USER_ID <", value, "userId");
return (Criteria) this;
}
public Criteria andUserIdLessThanOrEqualTo(Integer value) {
addCriterion("USER_ID <=", value, "userId");
return (Criteria) this;
}
public Criteria andUserIdIn(List<Integer> values) {
addCriterion("USER_ID in", values, "userId");
return (Criteria) this;
}
public Criteria andUserIdNotIn(List<Integer> values) {
addCriterion("USER_ID not in", values, "userId");
return (Criteria) this;
}
public Criteri