SSH的增删改查(通过AJAX查询显示)

*增删改查之前整合SSH和登录

步骤:
1,编写dao中的增删改查方法,
2,action中调用,
3,Spring中配置,struts中配置,
4,jsp页面,
5,测试

开始代码编写

1,编写dao中的增删改查方法,

增删改查方法

package org.xian.dao;

import java.util.List;

import org.springframework.orm.hibernate5.HibernateTemplate;
import org.xian.bean.Users;

public class UserDao {

    private HibernateTemplate hibernateTemplate;

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    public   Users findById(int id) {

        return hibernateTemplate.get(Users.class, id) ;     
    }
    public void update(Users users ) {

          hibernateTemplate.update(users);  
    }

    public   List findAll() {

        List list= hibernateTemplate.find("from Users");

        return  list;       
    }
    public void add(Users users) {

         hibernateTemplate.save(users);     
    }
    public void delete(Users users) {

         hibernateTemplate.delete(users);       
    }
    public boolean login (String name,String pwd) {

        List users= hibernateTemplate.find("from Users where name=? and pwd =?" ,name,pwd);
        if (users.size()>0) {
            return true;
        }

        return false;
    }
    public List checkName (String name) {

        List list=  hibernateTemplate.find("from Users where name=?" ,name);

        return list;
    }

}

2,action中调用,

package org.xian.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.xian.bean.Users;
import org.xian.dao.UserDao;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class HelloAction extends ActionSupport implements ModelDriven {

    private UserDao dao;
    private Users users =new Users();
    private List list;
    private List user;
    private Map map;


    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    public String findById() {

      users= dao.findById(users.getUid());

        return SUCCESS;
    }

    public  String login() {

        if (dao.login(users.getName(), users.getPwd())) {
            return SUCCESS;
        }

        return ERROR;
    }
    public  String getAll() {

        list=dao.findAll();

        return SUCCESS;
    }

    public  String showAll() {

        user=dao.findAll();
        map=new HashMap();
        map.put("status", "ok");
        map.put("show", user);

        return SUCCESS;
    }
    public  String update() {

        dao.update(users);

        return SUCCESS;
    }
    public  String delete() {

        dao.delete(users);

        return SUCCESS;
    }
    public  String add() {

        dao.add(users);

        return SUCCESS;
    }

    public List getUser() {
        return user;
    }

    public void setUser(List user) {
        this.user = user;
    }

    public UserDao getDao() {
        return dao;
    }

    public void setDao(UserDao dao) {
        this.dao = dao;
    }

    public Users getUsers() {
        return users;
    }

    public void setUsers(Users users) {
        this.users = users;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    @Override
    public Users getModel() {
        // TODO Auto-generated method stub
        return users;
    }

}

3,Spring中配置,struts中配置,
applicationContext.xml配置


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml">property>
        <property name="dataSource" ref="dataSource">property>
    bean>
    
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory">property>
    bean>

    
    <bean class="org.xian.action.HelloAction" id="helloAction">
        <property name="dao" ref="userDao">property>
    bean>
    <bean class="org.xian.action.ApiAction" id="apiAction">
        <property name="dao" ref="userDao">property>
    bean>


    
    <bean class="org.xian.dao.UserDao" id="userDao">
        <property name="hibernateTemplate" ref="hibernateTemplate">property>
    bean>
    bean>

struts 配置
增删改时需要重定向重新查询一次redirectAction




<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devMode" value="true" />
    <package name="default" namespace="/" extends="struts-default">
    
        <action name="login" class="helloAction" method="login">
            <result type="redirectAction">getAllresult>
            <result name="error">/error.jspresult>
        action>
        
        <action name="getAll" class="helloAction" method="getAll">
            <result name="success">/hello.jspresult>
        action>
        
        <action name="update" class="helloAction" method="update">
            <result type="redirectAction">getAllresult>
            <result name="error">/error.jspresult>
        action>
        
        <action name="getById" class="helloAction" method="findById">
            <result name="success">/edit.jspresult>
        action>
        
        <action name="delete" class="helloAction" method="delete">
            <result type="redirectAction">getAllresult>
            <result name="error">/error.jspresult>
        action>
        
        <action name="add" class="helloAction" method="add">
            <result name="success">/index.jspresult>
            <result name="error">/error.jspresult>
        action>
    package>

    <package name="json" namespace="/api" extends="json-default">

        <action name="showAll" class="helloAction" method="showAll">
            <result name="success" type="json">
                <param name="root">mapparam>
            result>
        action>
    package>
struts>

4,jsp页面,

显示页面

显示页面两种显示方式
使用迭代的方式显示,使用AJAX便利数据

<script type="text/javascript" src="res/jquery.min.js">script>
<style type="text/css">
#table tr td {
    border: solid 1px #666;
}
.red {
    background: pink;
}
.blue {
    background: orange;
}
div{
     color : red; 
}

<body onload="all()">
    <div align="center">哦吼吼,您好:${users.name}

:iterator value="list" var="listtt"> :iterator>
uid name pwd 操作
"formatPrice">${listtt.uid}td> <td>${listtt.name} ${listtt.pwd} 修改 删除
"table" align="center">
uid name pwd "center" colspan="2">操作

你可能感兴趣的:(ssh-增删改查,AJAX)