当前位置:   article > 正文

SSH框架的增、删、查、改的实现_ssh框架增删改查

ssh框架增删改查

搭建SSH框架请参考文章(http://blog.csdn.net/qq_39189632/article/details/78401657

这里写图片描述

1.创建User.java

package com.user.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * 用户表
 * @author LICHENGLONG
 *
 */
@Entity
@Table(name="tb_user")
public class User {

    private Integer id;
    private String username;
    private String password;
    private Integer userid;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id")
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    @Column(name="username")
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    @Column(name="password")
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Column(name="userid")
    public Integer getUserid() {
        return userid;
    }
    public void setUserid(Integer userid) {
        this.userid = userid;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", password="
                + password + ", userid=" + userid + "]";
    }


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

2、创建UserDao.java

package com.user.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.user.model.User;

@Repository(value="userDao")
public class UserDao extends HibernateDaoSupport {

    boolean bool = false;
    @Resource(name="sessionFactory")
    public void setSuperSessionFactory(SessionFactory sessionFactory){
        super.setSessionFactory(sessionFactory);
    }

    /**
     * 新增
     * @param obj
     * @return
     */
    public boolean save(Object obj){
        try {
            super.getHibernateTemplate().save(obj);
            bool = true;
        } catch (Exception e) {
            bool = false;
        }
        return bool;
    }

    /**
     * 修改
     * @param obj
     * @return
     */
    public boolean update(Object obj){
        try {
            super.getHibernateTemplate().update(obj);
            bool = true;
        } catch (Exception e) {
            bool = false;
        }
        return bool;
    }


    /**
     * 删除
     * @param obj
     * @return
     */
    public boolean delete(Object obj){
        try {
            super.getHibernateTemplate().delete(obj);
            bool = true;
        } catch (Exception e) {
            bool = false;
        }
        return bool;
    }

    /**
     * 查询
     * @param hql
     * @return
     */
    public List<User> find(String hql){
        return super.getHibernateTemplate().find(hql);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76

3.创建接口IUserService.java

package com.user.service;

import java.util.List;

import com.user.model.User;

public interface IUserService {
    public abstract boolean save(User user);
    public abstract List<User> listUser();
    public abstract List<User> listUserId(Integer id);
    public abstract List<User> listUserLogin(String username,String password);
    public abstract boolean update(User user);
    public abstract boolean delete(User user);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

4、创建UserService.java

package com.user.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.user.dao.UserDao;
import com.user.model.User;
import com.user.service.IUserService;

@Service
public class UserService implements IUserService {

    @Resource(name="userDao")
    private UserDao userDao;

    public boolean save(User user) {
        boolean bool = userDao.save(user);
        return bool;
    }

    public List<User> listUser() {
        List<User> listUser = userDao.find("from User");
        return listUser;
    }

    public List<User> listUserId(Integer id) {
        List<User> listUser = userDao.find("from User where id="+id);
        return listUser;
    }


    public List<User> listUserLogin(String username, String password) {
        List<User> listUser = userDao.find("from User where username="+"'"+username+"'"+" and password="+"'"+password+"'");
        return listUser;
    }

    public boolean update(User user) {
        boolean bool = userDao.update(user);
        return bool;
    }

    public boolean delete(User user) {
        boolean bool = userDao.delete(user);
        return bool;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

5、创建 UserAction.java

package com.user.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;
import com.user.model.User;
import com.user.service.IUserService;

@Scope("prototype")
@Component(value="user.userAction")
public class UserAction extends ActionSupport {

    @Autowired
    private IUserService userService;
    private Integer id;
    private String username;
    private String password;
    private Integer userid;
    private List<User> listUser;

    public String add(){
        if(!username.equals("") && !password.equals("") && userid > 0){
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setUserid(userid);
            boolean bool = userService.save(user);
            if (bool) {
                return "login";
            }
        }
        return "add";
    }

    public String userlogin(){
        if(!username.equals("") && !password.equals("")){
            System.out.println("username:"+username+",password:"+password);
            List<User> listUser = userService.listUserLogin(username, password);
            System.out.println(listUser);
            if(listUser.size() > 0){
                return findData();
            }
        }
        return "login";
    }

    public String findData(){
        listUser = userService.listUser();
        return "main";
    }

    public String findDatauserId(){
        if(id > 0){
            listUser = userService.listUserId(id);
            return "update";
        }
        return findData();
    }
    public String update(){
        if(id > 0 && !username.equals("") && !password.equals("") && userid > 0){
            User user = new User();
            user.setId(id);
            user.setUsername(username);
            user.setPassword(password);
            user.setUserid(userid);
            boolean bool = userService.update(user);
            if (bool) {
                return findData();
            }
        }
        return "update";
    }

    public String delete(){
        if(id > 0){
            User user = new User();
            user.setId(id);
            boolean bool = userService.delete(user);
            if (bool) {
                return findData();
            }
        }
        return findData();
    }


    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public Integer getUserid() {
        return userid;
    }
    public void setUserid(Integer userid) {
        this.userid = userid;
    }
    public List<User> getListUser() {
        return listUser;
    }
    public void setListUser(List<User> listUser) {
        this.listUser = listUser;
    }


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123

6、创建 struts-user.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
  <package name="user" extends="struts-default" namespace="/user">
    <action name="*User" class="user.userAction" method="{1}">
        <result name="login">/login.jsp</result>
        <result name="add">/add.jsp</result>
        <result name="main">/main.jsp</result>
        <result name="update">/update.jsp</result>
    </action>
  </package>
</struts>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

7、创建 struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
  <include file="struts-user.xml"></include>
</struts>
  • 1
  • 2
  • 3
  • 4
  • 5

以下是页面代码

1、创建login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>

  <body>
    <form action="<%=path %>/user/userloginUser.action" method="post">
       <table>
          <tr>
            <td>用户:</td>
            <td><input type="text" name="username" ></td>
          </tr>
          <tr>
            <td>密码:</td>
            <td><input type="password" name="password" ></td>
          </tr>
          <tr>
            <td><a href="<%=path %>/add.jsp">注册</a></td>
            <td><input type="submit" value="登录" ></td>
          </tr>
       </table>
    </form>


  </body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

2、创建 main.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'main.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    <table>
       <tr>
         <th>id</th>
         <th>username</th>
         <th>password</th>
         <th>userid</th>
         <th colspan="3">编辑</th>
       </tr>
       <c:forEach items="${listUser }" var="data" >
       <tr>
         <td>${data.id }</td>
         <td>${data.username }</td>
         <td>${data.password }</td>
         <td>${data.userid }</td>
         <td><a href="<%=path %>/add.jsp">新增</a></td>
         <td><a href="<%=path %>/user/findDatauserIdUser.action?id=${data.id }">修改</a></td>
         <td><a href="<%=path %>/user/deleteUser.action?id=${data.id }">删除</a></td>
       </tr>
       </c:forEach>
    </table>
  </body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

3、创建add.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'add.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    <form action="<%=path %>/user/addUser.action" method="post">
        <table>
           <tr>
             <td>用户名称:</td>
             <td><input type="text" name="username" ></td>
           </tr>
           <tr>
             <td>密码:</td>
             <td><input type="password" name="password" ></td>
           </tr>
           <tr>
             <td>用户ID:</td>
             <td><input type="text" name="userid" ></td>
           </tr>
           <tr>
             <td></td>
             <td><input type="submit" value="注册" ></td>
           </tr>
        </table>
    </form>
  </body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

4、创建 update.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'update.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    <form action="<%=path %>/user/updateUser.action" method="post">
        <c:forEach items="${listUser}" var="data">
        <input type="hidden" name="id" value="${data.id }" >
        <table>
           <tr>
             <td>用户名称:</td>
             <td><input type="text" name="username" value="${data.username }" ></td>
           </tr>
           <tr>
             <td>密码:</td>
             <td><input type="password" name="password" value="${data.password }" ></td>
           </tr>
           <tr>
             <td>用户ID:</td>
             <td><input type="text" name="userid" value="${data.userid }" ></td>
           </tr>
           <tr>
             <td></td>
             <td><input type="submit" value="修改" ></td>
           </tr>
        </table>
        </c:forEach>
    </form>
  </body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

原码下载:http://download.csdn.net/download/qq_39189632/10046918

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/185267
推荐阅读
相关标签
  

闽ICP备14008679号