林炳文Evankaka原創作品。轉載請註明出處http://blog.csdn.net/evankaka
摘要:這幾天研究了下Ajax註冊的方法,通過在註冊時輸入用戶名或郵箱等,就可以判斷這個用戶是否存在,以免用戶來註冊,然後提交了,系統才提示該用戶名或郵箱不可用。使用Ajax便可實現這一功能,看了網上的都是php的,想想索性來寫一個SpringMVC+Spring+Mybatis的。文章內容用到了很多技術,包括javascript、jquery、json、e表達式等。
本文工程免費下載
先來看看最終效果:
![](http://static.javashuo.com/static/loading.gif)
註冊成功的樣子:
![](http://static.javashuo.com/static/loading.gif)
註冊過程中參數的檢驗:
![](http://static.javashuo.com/static/loading.gif)
下面,讓我們開始這次的編程吧!
首先,數據庫準備,新建一張用戶表,並自己插入一些數據
- 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
本文工程免費下載
工程的其它參數:
![](http://static.javashuo.com/static/loading.gif)
1、用到的JAR包
json使用的包。這裏下載
spring+sprngMvc的包。
mybatis的包
mysql連接的包
logger的包(日記 打印的)
如下:
![](http://static.javashuo.com/static/loading.gif)
2、需要的外部js
jquery-1.11.3.min.js
3、項目結構
這是還沒有展開的
![](http://static.javashuo.com/static/loading.gif)
然後把各個都展開:
![](http://static.javashuo.com/static/loading.gif)
其中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
然後還有一個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 Criteria andUserIdBetween(Integer value1, Integer value2) {
- addCriterion("USER_ID between", value1, value2, "userId");
- return (Criteria) this;
- }
-
- public Criteria andUserIdNotBetween(Integer value1, Integer value2) {
- addCriterion("USER_ID not between", value1, value2, "userId");
- return (Criteria) this;
- }
-
- public Criteria andUserNameIsNull() {
- addCriterion("USER_NAME is null");
- return (Criteria) this;
- }
-
- public Criteria andUserNameIsNotNull() {
- addCriterion("USER_NAME is not null");
- return (Criteria) this;
- }
-
- public Criteria andUserNameEqualTo(String value) {
- addCriterion("USER_NAME =", value, "userName");
- return (Criteria) this;
- }
-
- public Criteria andUserNameNotEqualTo(String value) {
- addCriterion("USER_NAME <>", value, "userName");
- return (Criteria) this;
- }
-
- public Criteria andUserNameGreaterThan(String value) {
- addCriterion("USER_NAME >", value, "userName");
- return (Criteria) this;
- }
-
- public Criteria andUserNameGreaterThanOrEqualTo(String value) {
- addCriterion("USER_NAME >=", value, "userName");
- return (Criteria) this;
- }
-
- public Criteria andUserNameLessThan(String value) {
- addCriterion("USER_NAME <", value, "userName");
- return (Criteria) this;
- }
-
- public Criteria andUserNameLessThanOrEqualTo(String value) {
- addCriterion("USER_NAME <=", value, "userName");
- return (Criteria) this;
- }
-
- public Criteria andUserNameLike(String value) {
- addCriterion("USER_NAME like", value, "userName");
- return (Criteria) this;
- }
-
- public Criteria andUserNameNotLike(String value) {
- addCriterion("USER_NAME not like", value, "userName");
- return (Criteria) this;
- }
-
- public Criteria andUserNameIn(List<String> values) {
- addCriterion("USER_NAME in", values, "userName");
- return (Criteria) this;
- }
-
- public Criteria andUserNameNotIn(List<String> values) {
- addCriterion("USER_NAME not in", values, "userName");
- return (Criteria) this;
- }
-
- public Criteria andUserNameBetween(String value1, String value2) {
- addCriterion("USER_NAME between", value1, value2, "userName");
- return (Criteria) this;
- }
-
- public Criteria andUserNameNotBetween(String value1, String value2) {
- addCriterion("USER_NAME not between", value1, value2, "userName");
- return (Criteria) this;
- }
-
- public Criteria andUserPasswordIsNull() {
- addCriterion("USER_PASSWORD is null");
- return (Criteria) this;
- }
-
- public Criteria andUserPasswordIsNotNull() {
- addCriterion("USER_PASSWORD is not null");
- return (Criteria) this;
- }
-
- public Criteria andUserPasswordEqualTo(String value) {
- addCriterion("USER_PASSWORD =", value, "userPassword");
- return (Criteria) this;
- }
-
- public Criteria andUserPasswordNotEqualTo(String value) {
- addCriterion("USER_PASSWORD <>", value, "userPassword");
- return (Criteria) this;
- }
-
- public Criteria andUserPasswordGreaterThan(String value) {
- addCriterion("USER_PASSWORD >", value, "userPassword");
- return (Criteria) this;
- }
-
- public Criteria andUserPasswordGreaterThanOrEqualTo(String value) {
- addCriterion("USER_PASSWORD >=", value, "userPassword");
- return (Criteria) this;
- }
-
- public Criteria andUserPasswordLessThan(String value) {
- addCriterion("USER_PASSWORD <", value, "userPassword");
- return (Criteria) this;
- }
-
- public Criteria andUserPasswordLessThanOrEqualTo(String value) {
- addCriterion("USER_PASSWORD <=", value, "userPassword");
- return (Criteria) this;
- }
-
- public Criteria andUserPasswordLike(String value) {
- addCriterion("USER_PASSWORD like", value, "userPassword");
- return (Criteria) this;
- }
-
- public Criteria andUserPasswordNotLike(String value) {
- addCriterion("USER_PASSWORD not like", value, "userPassword");
- return (Criteria) this;
- }
-
- public Criteria andUserPasswordIn(List<String> values) {
- addCriterion("USER_PASSWORD in", values, "userPassword");
- return (Criteria) this;
- }
-
- public Criteria andUserPasswordNotIn(List<String> values) {
- addCriterion("USER_PASSWORD not in", values, "userPassword");
- return (Criteria) this;
- }
-
- public Criteria andUserPasswordBetween(String value1, String value2) {
- addCriterion("USER_PASSWORD between", value1, value2, "userPassword");
- return (Criteria) this;
- }
-
- public Criteria andUserPasswordNotBetween(String value1, String value2) {
- addCriterion("USER_PASSWORD not between", value1, value2, "userPassword");
- return (Criteria) this;
- }
-
- public Criteria andUserEmailIsNull() {
- addCriterion("USER_EMAIL is null");
- return (Criteria) this;
- }
-
- public Criteria andUserEmailIsNotNull() {
- addCriterion("USER_EMAIL is not null");
- return (Criteria) this;
- }
-
- public Criteria andUserEmailEqualTo(String value) {
- addCriterion("USER_EMAIL =", value, "userEmail");
- return (Criteria) this;
- }
-
- public Criteria andUserEmailNotEqualTo(String value) {
- addCriterion("USER_EMAIL <>", value, "userEmail");
- return (Criteria) this;
- }
-
- public Criteria andUserEmailGreaterThan(String value) {
- addCriterion("USER_EMAIL >", value, "userEmail");
- return (Criteria) this;
- }
-
- public Criteria andUserEmailGreaterThanOrEqualTo(String value) {
- addCriterion("USER_EMAIL >=", value, "userEmail");
- return (Criteria) this;
- }
-
- public Criteria andUserEmailLessThan(String value) {
- addCriterion("USER_EMAIL <", value, "userEmail");
- return (Criteria) this;
- }
-
- public Criteria andUserEmailLessThanOrEqualTo(String value) {
- addCriterion("USER_EMAIL <=", value, "userEmail");
- return (Criteria) this;
- }
-
- public Criteria andUserEmailLike(String value) {
- addCriterion("USER_EMAIL like", value, "userEmail");
- return (Criteria) this;
- }
-
- public Criteria andUserEmailNotLike(String value) {
- addCriterion("USER_EMAIL not like", value, "userEmail");
- return (Criteria) this;
- }
-
- public Criteria andUserEmailIn(List<String> values) {
- addCriterion("USER_EMAIL in", values, "userEmail");
- return (Criteria) this;
- }
-
- public Criteria andUserEmailNotIn(List<String> values) {
- addCriterion("USER_EMAIL not in", values, "userEmail");
- return (Criteria) this;
- }
-
- public Criteria andUserEmailBetween(String value1, String value2) {
- addCriterion("USER_EMAIL between", value1, value2, "userEmail");
- return (Criteria) this;
- }
-
- public Criteria andUserEmailNotBetween(String value1, String value2) {
- addCriterion("USER_EMAIL not between", value1, value2, "userEmail");
- return (Criteria) this;
- }
- }
-
- /**
- * This class was generated by MyBatis Generator.
- * This class corresponds to the database table t_user
- *
- * @mbggenerated do_not_delete_during_merge
- */
- public static class Criteria extends GeneratedCriteria {
-
- protected Criteria() {
- super();
- }
- }
-
- /**
- * This class was generated by MyBatis Generator.
- * This class corresponds to the database table t_user
- *
- * @mbggenerated
- */
- 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;
-
- 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;
- }
-
- protected Criterion(String condition) {
- super();
- this.condition = condition;
- this.noValue = true;
- }
-
- protected Criterion(String condition, Object value) {
- super();
- this.condition = condition;
- this.value = value;
- if (value instanceof List<?>) {
- this.listValue = true;
- } else {
- this.singleValue = true;
- }
- }
-
- protected Criterion(String condition, Object value, Object secondValue) {
- super();
- this.condition = condition;
- this.value = value;
- this.secondValue = secondValue;
- this.betweenValue = true;
- }
- }
- }
2、dao層
這裏是使用Mybatis Generator自動生成的:UserDao.java
- package com.lin.dao;
-
- import com.lin.domain.User;
- import com.lin.domain.UserExample;
- import java.util.List;
- import org.apache.ibatis.annotations.Param;
-
- public interface UserDao {
- /**
- * This method was generated by MyBatis Generator.
- * This method corresponds to the database table t_user
- *
- * @mbggenerated
- */
- int countByExample(UserExample example);
-
- /**
- * This method was generated by MyBatis Generator.
- * This method corresponds to the database table t_user
- *
- * @mbggenerated
- */
- int deleteByExample(UserExample example);
-
- /**
- * This method was generated by MyBatis Generator.
- * This method corresponds to the database table t_user
- *
- * @mbggenerated
- */
- int deleteByPrimaryKey(Integer userId);
-
- /**
- * This method was generated by MyBatis Generator.
- * This method corresponds to the database table t_user
- *
- * @mbggenerated
- */
- int insert(User record);
-
- /**
- * This method was generated by MyBatis Generator.
- * This method corresponds to the database table t_user
- *
- * @mbggenerated
- */
- int insertSelective(User record);
-
- /**
- * This method was generated by MyBatis Generator.
- * This method corresponds to the database table t_user
- *
- * @mbggenerated
- */
- List<User> selectByExample(UserExample example);
-
- /**
- * This method was generated by MyBatis Generator.
- * This method corresponds to the database table t_user
- *
- * @mbggenerated
- */
- User selectByPrimaryKey(Integer userId);
-
- /**
- * This method was generated by MyBatis Generator.
- * This method corresponds to the database table t_user
- *
- * @mbggenerated
- */
- int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example);
-
- /**
- * This method was generated by MyBatis Generator.
- * This method corresponds to the database table t_user
- *
- * @mbggenerated
- */
- int updateByExample(@Param("record") User record, @Param("example") UserExample example);
-
- /**
- * This method was generated by MyBatis Generator.
- * This method corresponds to the database table t_user
- *
- * @mbggenerated
- */
- int updateByPrimaryKeySelective(User record);
-
- /**
- * This method was generated by MyBatis Generator.
- * This method corresponds to the database table t_user
- *
- * @mbggenerated
- */
- int updateByPrimaryKey(User record);
- }
3、service層
這裏就設計了兩個方法,一個查找和一個插入
接口類:
- package com.lin.service;
-
- import com.lin.domain.User;
- import com.lin.domain.UserExample;
-
- public interface IRegisterService {
-
- public int insert(User record);
-
- public int countByExample(UserExample example);
-
- }
實現類:
- package com.lin.service.impl;
-
- import javax.annotation.Resource;
-
- import org.apache.log4j.Logger;
- import org.springframework.stereotype.Service;
-
- import com.lin.dao.UserDao;
- import com.lin.domain.User;
- import com.lin.domain.UserExample;
- import com.lin.service.IRegisterService;
- @Service("registerService")
- public class RegisterServiceImpl implements IRegisterService{
- private static Logger logger = Logger.getLogger(RegisterServiceImpl.class);
- @Resource
- private UserDao userDao;
-
- @Override
- public int insert(User record) {
- try {
- return userDao.insert(record);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return 0;
- }
-
- @Override
- public int countByExample(UserExample example) {
- try {
- return userDao.countByExample(example);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return 0;
- }
-
- }
2.2 配置文件
![](http://static.javashuo.com/static/loading.gif)
1、mappp文件配置
這裏是使用Mybatis Generator自動生成的:UserMapper.xml。放在src下面的com.lin.mapper包下
- <?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="com.lin.dao.UserDao">
- <resultMap id="BaseResultMap" type="com.lin.domain.User">
- <!--
- WARNING - @mbggenerated
- This element is automatically generated by MyBatis Generator, do not modify.
- -->
- <id column="USER_ID" jdbcType="INTEGER" property="userId" />
- <result column="USER_NAME" jdbcType="CHAR" property="userName" />
- <result column="USER_PASSWORD" jdbcType="CHAR" property="userPassword" />
- <result column="USER_EMAIL" jdbcType="CHAR" property="userEmail" />
- </resultMap>
- <sql id="Example_Where_Clause">
- <!--
- WARNING - @mbggenerated
- This element is automatically generated by MyBatis Generator, do not modify.
- -->
- <where>
- <foreach collection="oredCriteria" item="criteria" separator="or">
- <if test="criteria.valid">
- <trim prefix="(" prefixOverrides="and" suffix=")">
- <foreach collection="criteria.criteria" item="criterion">
- <choose>
- <when test="criterion.noValue">
- and ${criterion.condition}
- </when>
- <when test="criterion.singleValue">
- and ${criterion.condition} #{criterion.value}
- </when>
- <when test="criterion.betweenValue">
- and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
- </when>
- <when test="criterion.listValue">
- and ${criterion.condition}
- <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
- #{listItem}
- </foreach>
- </when>
- </choose>
- </foreach>
- </trim>
- </if>
- </foreach>
- </where>
- </sql>
- <sql id="Update_By_Example_Where_Clause">
- <!--
- WARNING - @mbggenerated
- This element is automatically generated by MyBatis Generator, do not modify.
- -->
- <where>
- <foreach collection="example.oredCriteria" item="criteria" separator="or">
- <if test="criteria.valid">
- <trim prefix="(" prefixOverrides="and" suffix=")">
- <foreach collection="criteria.criteria" item="criterion">
- <choose>
- <when test="criterion.noValue">
- and ${criterion.condition}
- </when>
- <when test="criterion.singleValue">
- and ${criterion.condition} #{criterion.value}
- </when>
- <when test="criterion.betweenValue">
- and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
- </when>
- <when test="criterion.listValue">
- and ${criterion.condition}
- <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
- #{listItem}
- </foreach>
- </when>
- </choose>
- </foreach>
- </trim>
- </if>
- </foreach>
- </where>
- </sql>
- <sql id="Base_Column_List">
- <!--
- WARNING - @mbggenerated
- This element is automatically generated by MyBatis Generator, do not modify.
- -->
- USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL
- </sql>
- <select id="selectByExample" parameterType="com.lin.domain.UserExample" resultMap="BaseResultMap">
- <!--
- WARNING - @mbggenerated
- This element is automatically generated by MyBatis Generator, do not modify.
- -->
- select
- <if test="distinct">
- distinct
- </if>
- <include refid="Base_Column_List" />
- from t_user
- <if test="_parameter != null">
- <include refid="Example_Where_Clause" />
- </if>
- <if test="orderByClause != null">
- order by ${orderByClause}
- </if>
- </select>
- <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
- <!--
- WARNING - @mbggenerated
- This element is automatically generated by MyBatis Generator, do not modify.
- -->
- select
- <include refid="Base_Column_List" />
- from t_user
- where USER_ID = #{userId,jdbcType=INTEGER}
- </select>
- <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
- <!--
- WARNING - @mbggenerated
- This element is automatically generated by MyBatis Generator, do not modify.
- -->
- delete from t_user
- where USER_ID = #{userId,jdbcType=INTEGER}
- </delete>
- <delete id="deleteByExample" parameterType="com.lin.domain.UserExample">
- <!--
- WARNING - @mbggenerated
- This element is automatically generated by MyBatis Generator, do not modify.
- -->
- delete from t_user
- <if test="_parameter != null">
- <include refid="Example_Where_Clause" />
- </if>
- </delete>
- <insert id="insert" parameterType="com.lin.domain.User">
- <!--
- WARNING - @mbggenerated
- This element is automatically generated by MyBatis Generator, do not modify.
- -->
- insert into t_user (USER_ID, USER_NAME, USER_PASSWORD,
- USER_EMAIL)
- values (#{userId,jdbcType=INTEGER}, #{userName,jdbcType=CHAR}, #{userPassword,jdbcType=CHAR},
- #{userEmail,jdbcType=CHAR})
- </insert>
- <insert id="insertSelective" parameterType="com.lin.domain.User">
- <!--
- WARNING - @mbggenerated
- This element is automatically generated by MyBatis Generator, do not modify.
- -->
- insert into t_user
- <trim prefix="(" suffix=")" suffixOverrides=",">
- <if test="userId != null">
- USER_ID,
- </if>
- <if test="userName != null">
- USER_NAME,
- </if>
- <if test="userPassword != null">
- USER_PASSWORD,
- </if>
- <if test="userEmail != null">
- USER_EMAIL,
- </if>
- </trim>
- <trim prefix="values (" suffix=")" suffixOverrides=",">
- <if test="userId != null">
- #{userId,jdbcType=INTEGER},
- </if>
- <if test="userName != null">
- #{userName,jdbcType=CHAR},
- </if>
- <if test="userPassword != null">
- #{userPassword,jdbcType=CHAR},
- </if>
- <if test="userEmail != null">
- #{userEmail,jdbcType=CHAR},
- </if>
- </trim>
- </insert>
- <select id="countByExample" parameterType="com.lin.domain.UserExample" resultType="java.lang.Integer">
- <!--
- WARNING - @mbggenerated
- This element is automatically generated by MyBatis Generator, do not modify.
- -->
- select count(*) from t_user
- <if test="_parameter != null">
- <include refid="Example_Where_Clause" />
- </if>
- </select>
- <update id="updateByExampleSelective" parameterType="map">
- <!--
- WARNING - @mbggenerated
- This element is automatically generated by MyBatis Generator, do not modify.
- -->
- update t_user
- <set>
- <if test="record.userId != null">
- USER_ID = #{record.userId,jdbcType=INTEGER},
- </if>
- <if test="record.userName != null">
- USER_NAME = #{record.userName,jdbcType=CHAR},
- </if>
- <if test="record.userPassword != null">
- USER_PASSWORD = #{record.userPassword,jdbcType=CHAR},
- </if>
- <if test="record.userEmail != null">
- USER_EMAIL = #{record.userEmail,jdbcType=CHAR},
- </if>
- </set>
- <if test="_parameter != null">
- <include refid="Update_By_Example_Where_Clause" />
- </if>
- </update>
- <update id="updateByExample" parameterType="map">
- <!--
- WARNING - @mbggenerated
- This element is automatically generated by MyBatis Generator, do not modify.
- -->
- update t_user
- set USER_ID = #{record.userId,jdbcType=INTEGER},
- USER_NAME = #{record.userName,jdbcType=CHAR},
- USER_PASSWORD = #{record.userPassword,jdbcType=CHAR},
- USER_EMAIL = #{record.userEmail,jdbcType=CHAR}
- <if test="_parameter != null">
- <include refid="Update_By_Example_Where_Clause" />
- </if>
- </update>
- <update id="updateByPrimaryKeySelective" parameterType="com.lin.domain.User">
- <!--
- WARNING - @mbggenerated
- This element is automatically generated by MyBatis Generator, do not modify.
- -->
- update t_user
- <set>
- <if test="userName != null">
- USER_NAME = #{userName,jdbcType=CHAR},
- </if>
- <if test="userPassword != null">
- USER_PASSWORD = #{userPassword,jdbcType=CHAR},
- </if>
- <if test="userEmail != null">
- USER_EMAIL = #{userEmail,jdbcType=CHAR},
- </if>
- </set>
- where USER_ID = #{userId,jdbcType=INTEGER}
- </update>
- <update id="updateByPrimaryKey" parameterType="com.lin.domain.User">
- <!--
- WARNING - @mbggenerated
- This element is automatically generated by MyBatis Generator, do not modify.
- -->
- update t_user
- set USER_NAME = #{userName,jdbcType=CHAR},
- USER_PASSWORD = #{userPassword,jdbcType=CHAR},
- USER_EMAIL = #{userEmail,jdbcType=CHAR}
- where USER_ID = #{userId,jdbcType=INTEGER}
- </update>
- </mapper>
2、mybatis配置,沒有其實也行,因爲都 在spring中進行配置了,但是保留一下,以免要用,mybatis-config.xml放在config下面
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
- </configuration>
3、spingMVC配置
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- 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"
- xsi:schemaLocation="
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <!-- 把標記了@Controller註解的類轉換爲bean -->
- <context:component-scan base-package="com.lin.controller" />
- <!-- 啓動Spring MVC的註解功能,完成請求和註解POJO的映射 -->
- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
- <!-- 靜態資源訪問(不攔截此目錄下的東西的訪問) -->
- <mvc:annotation-driven />
- <mvc:resources location="/WEB-INF//js/" mapping="/js/**" />
- <!-- 對模型視圖名稱的解析,即在模型視圖名稱添加前後綴 -->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
- p:prefix="/WEB-INF/views/" p:suffix=".jsp"/>
-
-
- </beans>
4、spring配置:application.xml放在config下面
- <?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:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <!-- 配置數據源 -->
- <bean id="dataSource"
- class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://localhost:3306/learning" />
- <property name="username" value="root" />
- <property name="password" value="[email protected]" />
- </bean>
-
- <!-- 自動掃描了所有的XxxxMapper.xml對應的mapper接口文件,這樣就不用一個一個手動配置Mpper的映射了,只要Mapper接口類和Mapper映射文件對應起來就可以了。 -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage"
- value="com.lin.dao" />
- </bean>
-
- <!-- 配置Mybatis的文件 ,mapperLocations配置**Mapper.xml文件位置,configLocation配置mybatis-config文件位置-->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="mapperLocations" value="classpath*:com/lin/mapper/**/*.xml"/>
- <property name="configLocation" value="classpath:mybatis-config.xml" />
- <!-- <property name="typeAliasesPackage" value="com.tiantian.ckeditor.model"
- /> -->
- </bean>
-
- <!-- 自動掃描註解的bean -->
- <context:component-scan base-package="com.lin.controller" />
- <context:component-scan base-package="com.lin.service.impl" />
-
- </beans>
5、日誌打印配置:log4j.properties放在config下面
- log4j.rootLogger =DEBEG,stdout,debug
-
-
- log4j.appender.stdout = org.apache.log4j.ConsoleAppender
- log4j.appender.stdout.Target = System.out
- log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
- log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
-
- log4j.logger.java.sql.ResultSet=INFO
- log4j.logger.org.apache=INFO
- log4j.logger.java.sql.Connection=DEBUG
- log4j.logger.java.sql.Statement=DEBUG
- log4j.logger.java.sql.PreparedStatement=DEBUG
6、web文件配置,放在WebContent下面
- <?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>JsLearning3</display-name>
- <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>
-
- <!-- Spring 容器加載 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:application.xml</param-value>
- </context-param>
-
- <!-- 統一設置編碼,防止出現中文亂碼 -->
- <filter>
- <filter-name>Set Character Encoding</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>Set Character Encoding</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
- <!-- SpringMVC的前端控制器 -->
- <servlet>
- <servlet-name>dispatcherServlet</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <!-- 設置自己定義的控制器xml文件 -->
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath*:spring-servlet.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <!-- Spring MVC配置文件結束 -->
-
- <!-- 攔截設置 -->
- <servlet-mapping>
- <servlet-name>dispatcherServlet</servlet-name>
- <!-- 由SpringMVC攔截所有請求 -->
- <url-pattern>/</url-pattern>
- </servlet-mapping>
-
- <!-- webAppRootKey:值缺省爲webapp.root,當tomcat下部署多個應用時(每個都用到了log4j), 每個應用的web.xml中都要配置該參數,該參數與Log4j.xml文件中的${webapp.root}
- 否則每個應用的webAppRootKey值都相同,就會引起衝突
- -->
- <context-param>
- <param-name>webAppRootKey</param-name>
- <param-value>webApp.root</param-value>
- </context-param>
- <!-- log4jConfigLocation:log4j配置文件存放路徑 -->
- <context-param>
- <param-name>log4jConfigLocation</param-name>
- <param-value>classpath:log4j.properties</param-value>
- </context-param>
- <!-- 3000表示 開一條watchdog線程每60秒掃描一下配置文件的變化;這樣便於日誌存放位置的改變 -->
- <context-param>
- <param-name>log4jRefreshInterval</param-name>
- <param-value>3000</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
- </listener>
- </web-app>
三、web頁面編寫
![](http://static.javashuo.com/static/loading.gif)
1、使用到的js編程
- var process_request = "<img src='loading.gif' width='16' height='16' border='0' align='absmiddle'>正在數據處理中...";
- var username_empty = "<span style='COLOR:#ff0000'> × 用戶名不能爲空!</span>";
- var username_shorter = "<span style='COLOR:#ff0000'> × 用戶名長度不能少於 3 個字符。</span>";
- var username_longer = "<span style='COLOR:#ff0000'> × 用戶名長度不能大於 30個字符。</span>";
- var username_invalid = "- 用戶名只能是由字母數字以及下劃線組成。";
- var username_have_register = "<span style='COLOR:#ff0000'> × 用戶名已經存在,請重新輸入!</span>";
- var username_can_register="<span style='COLOR:#006600'> √ 恭喜您!該用戶名可以註冊!</span>";
- var password_empty = "<span style='COLOR:#ff0000'> × 登錄密碼不能爲空。</span>";
- var password_shorter_s = "<span style='COLOR:#ff0000'> × 登錄密碼不能少於 6 個字符。</span>";
- var password_shorter_m = "<span style='COLOR:#ff0000'> × 登錄密碼不能多於 30 個字符。</span>";
- var confirm_password_invalid = "<span style='COLOR:#ff0000'> × 兩次輸入密碼不一致!</span>";
- var email_empty = "<span style='COLOR:#ff0000'> × 郵箱不能爲空!</span>";
- var email_invalid = "<span style='COLOR:#ff0000'> × 郵箱格式出錯!</span>";
- var email_have_register = "<span style='COLOR:#ff0000'> × 該郵箱已被註冊! </span>";
- var email_can_register = "<span style='COLOR:#006600'> √ 郵箱可以註冊!</span>";
- var agreement_no = "<span style='COLOR:#ff0000'> × 您沒有接受協議</span>";
- var agreement_yes= "<span style='COLOR:#006600'> √ 已經接受協議</span>";
- var info_can="<span style='COLOR:#006600'> √ 可以註冊!</span>";
- var info_right="<span style='COLOR:#006600'> √ 填寫正確!</span>";
- var name_flag=false;
- var email_flag=false;
- var password_flag=false;
- var accept_flag=false;
-
- $(function(){
- change_submit();
- if(document.getElementById("agreement").checked){
- alert("checkbox is checked");
- }
- });
-
- /*
- * 獲取工程的路徑
- */
- function getRootPath() {
- var pathName = window.location.pathname.substring(1);
- var webName = pathName == '' ? '' : pathName.substring(0, pathName
- .indexOf('/'));
- return window.location.protocol + '//' + window.location.host + '/'
- + webName + '/';
- }
- /*
- * 用戶名檢測
- */
- function checkUserName(obj) {
- if (checks(obj.value) == false) {
- showInfo("username_notice", username_invalid);
- } else if (obj.value.length < 1) {
- showInfo("username_notice", username_empty);
- }else if (obj.value.length < 3) {
- showInfo("username_notice", username_shorter);
- } else if(obj.value.length>30){
- showInfo("username_notice", username_longer);
- }else {
- // 調用Ajax函數,向服務器端發送查詢
- $.ajax({ //一個Ajax過程
- type: "post", //以post方式與後臺溝通
- url :getRootPath()+"/register/checkUserName", //與此頁面溝通
- dataType:'json',//返回的值以 JSON方式 解釋
- data: 'userName='+obj.value, //發給的數據
- success: function(json){//如果調用成功
- if(json.flag){
- showInfo("username_notice", username_have_register);
- }else {
- showInfo("username_notice", username_can_register);
- name_flag=true;
- change_submit();
- return;
- }
- }
- });
- }
- name_flag=false;
- change_submit();
- }
- /*
- * 用戶名檢測是否包含非法字符
- */
- function checks(t) {
- szMsg = "[#%&\'\"\\,;:=!^@]"
- for (i = 1; i < szMsg.length + 1; i++) {
- if (t.indexOf(szMsg.substring(i - 1, i)) > -1) {
- return false;
- }
- }
- return true;
- }
- /*
- * 郵箱檢測
- */
- function checkEmail(email) {
- var re = /^(\w-*\.*)[email protected](\w-?)+(\.\w{2,})+$/
- if (email.value.length < 1) {
- showInfo("email_notice", email_empty);
- } else if (!re.test(email.value)) {
- email.className = "FrameDivWarn";
- showInfo("email_notice", email_invalid);
- } else {
- // 調用Ajax函數,向服務器端發送查詢
- $.ajax({ //一個Ajax過程
- type: "post", //以post方式與後臺溝通
- url :getRootPath()+"/register/checkEmail", //與此頁面溝通
- dataType:'json',//返回的值以 JSON方式 解釋
- data: 'email='+email.value, //發給的數據
- success: function(json){//如果調用成功
- if(json.flag){