WEB开发网
开发学院软件开发Java Struts 2.1.6 精简实例系列教程(5):用户注册模块... 阅读

Struts 2.1.6 精简实例系列教程(5):用户注册模块(整合Jquery)

 2009-09-23 00:00:00 来源:WEB开发网   
核心提示: 首先在MySQL中,新建一个user表,Struts 2.1.6 精简实例系列教程(5):用户注册模块(整合Jquery)(2),建表SQL语句为:createtableuser(IDintauto_incrementnotnullprimarykey,LOGIN_NAMEvarchar(25)

首先在MySQL中,新建一个user表,建表SQL语句为:

create table user
(
    ID int auto_increment not null primary key,
    LOGIN_NAME varchar(25),
    PASSWORD varchar(25)
);

将用户信息封装到一个POJO类里,User.java的代码如下:

package cn.simple.pojo;
/** *//**
 * 用户类
 * @author rongxinhua
 *
 */
public class User {
    
    private int id;        //编号
    private String loginName;    //登录账号
    private String password;    //密码
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getLoginName() {
        return loginName;
    }
    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

User类与user表之间的属性列映射、查询语句、更新语句的定义等,我们写在配置文件User.xml里,代码如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap      
    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"      
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="User">
    
    <!-- 用"User"表示cn.simple.pojo.User类,下面用到此类时,用简称"User"即行 -->
    <typeAlias alias="User" type="cn.simple.pojo.User"/>
    
    <!-- 定义结果集,用户类与用户表的映射 -->
    <resultMap id="UserResult" class="User">
        <result property="id" column="ID"/>
        <result property="loginName" column="LOGIN_NAME"/>
        <result property="password" column="PASSWORD"/>
    </resultMap>
    
    <!-- 查询用户列表 -->
    <select id="selectAllUsers" resultMap="UserResult">
        select * from user
    </select>
    
    <!-- 根据ID单查用户 -->
    <select id="selectUserById" parameterClass="int" resultClass="User">
        select
        ID as id,
        LOGIN_NAME as loginName,
        PASSWORD as password
        from user
        where ID=#id#
    </select>
    
    <!-- 根据用户名查询用户 -->
    <select id="selectUserByLoginName" parameterClass="String" resultClass="User">
        select
        ID as id,
        LOGIN_NAME as loginName,
        PASSWORD as password
        from user
        where LOGIN_NAME=#loginName#
    </select>
    
    <!-- 添加用户 -->
    <insert id="insertUser" parameterClass="User">
        insert into user (
            LOGIN_NAME,
            PASSWORD
        ) values (
            #loginName#,
            #password#
        )
    </insert>
    
    <!-- 修改密码 -->
    <update id="updatePassword" parameterClass="User">
        update user set
        PASSWORD = #password#
        where ID=#id#
    </update>
    
    <!-- 删除用户 -->
    <delete id="deleteUserById" parameterClass="int">
        delete from user where ID=#id#
    </delete>
    
    
</sqlMap>

上一页  1 2 3 4 5  下一页

Tags:Struts 精简 实例

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接