Hibernate+Spring+Struts2+ExtJS开发CRUD功能(2)

3、  建立的项目目录:

Root:

/resource/ext2.0/  将下载的ext-2.0-beta1.zip文件解压到该目录

/WEB-INF/web.xml

/WEB-INF/lib

/WEB-INF/classes/struts.xml

/WEB-INF/spring/applicationContext.xml

4、  代码清单: 

Level.java

 

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.Table;

 

 

@Entity

@Table(name = "LOON_LEVEL")

public class Level implements java.io.Serializable {

    private Long levelid;

    private String levelname;

    private String description;

 

    public Level() {

    }

 

    public Level(Long levelid) {

       this.levelid = levelid;

    }

 

    public Level(Long levelid, String levelname, String description) {

       this.levelid = levelid;

       this.levelname = levelname;

       this.description = description;

    }

 

    @Id

    @Column(name = "LEVELID", unique = true, nullable = false, precision = 5, scale = 0)

    public Long getLevelid() {

       return this.levelid;

    }

 

    public void setLevelid(Long levelid) {

       this.levelid = levelid;

    }

 

    @Column(name = "LEVELNAME", length = 64)

    public String getLevelname() {

       return this.levelname;

    }

 

    public void setLevelname(String levelname) {

       this.levelname = levelname;

    }

 

    @Column(name = "DESCRIPTION", length = 256)

    public String getDescription() {

       return this.description;

    }

 

    public void setDescription(String description) {

       this.description = description;

    }

}

 

ILevelDAO.java

 

import java.util.List;

public interface ILevelDAO {

    public Level findLevelById(Long id);

    public List<Level>  findAllLevels();

    public void persistLevel(Level level);

    public void removeLevel(Level level);

    public void removeById(Long id);

}

 

LevelDAO.java

 

import java.util.List;

import org.hibernate.Session;

import org.springframework.orm.hibernate3.HibernateCallback;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class LevelDAO extends HibernateDaoSupport implements ILevelDAO {

    public LevelDAO() {

       super();

    }

    public Level findLevelById(Long id) {

       return (Level) getHibernateTemplate().get(Level.class, id);

    }

    public List<Level> findAllLevels() {

       return getHibernateTemplate().loadAll(Level.class);// .find("from Level o");//

    }

    public void persistLevel(Level level) {

       getHibernateTemplate().saveOrUpdate(level);

    }

    public void removeLevel(Level level) {

       getHibernateTemplate().delete(level);

    }

 

    public void removeById(final Long id) {

       this.getHibernateTemplate().execute(new HibernateCallback() {

           public Object doInHibernate(Session session) {

              session.createQuery("delete from Level o where o.levelid=" + id + "").executeUpdate(); 

              return 1;

           }

       });

    }

}

 

ILevelService.java

 

import java.util.List;

public interface ILevelService {

    public Level findLevelById(Long id) throws Exception;

    public List<Level> findAllLevels() throws Exception;

    public List<Level> findLevelsByExample(Level level) throws Exception;

    public void persistLevel(Level level) throws Exception;

    public void removeLevel(Level level) throws Exception;

public void removeLevelById(Long id) throws Exception;

}

 

LevelService.java

 

import java.util.List;

import privilege.dao.*;

import privilege.database.Level;

import org.springframework.context.ApplicationContext;

public class LevelService implements ILevelService {

    private ILevelDAO dao;

    private static final String SERVICE_BEAN_ID = "LevelService";

    public LevelService() {

       super();

    }

    public static ILevelService getInstance(ApplicationContext context) {

       return (ILevelService) context.getBean(SERVICE_BEAN_ID);

    }

    public Level findLevelById(Long id) throws Exception {

       try {

           return getDao().findLevelById(id);

       } catch (RuntimeException e) {

           throw new Exception("findLevelById failed with the id " + id + ": " + e.getMessage());

       }

    }

    public void persistLevel(Level level) throws Exception {

       try {

           getDao().persistLevel(level);

       } catch (RuntimeException e) {

           throw new Exception("persistLevel failed: " + e.getMessage());

       }

    }

    public void removeLevel(Level level) throws Exception {

       try {

           getDao().removeLevel(level);

       } catch (RuntimeException e) {

           throw new Exception("removeLevel failed: " + e.getMessage());

       }

    }

    public void removeLevelById(Long id) throws Exception {

       try {

           getDao().removeById(id);

       } catch (RuntimeException e) {

           throw new Exception("removeLevel failed: " + e.getMessage());

       }

    }

 

    public void setDao(ILevelDAO dao) {

       this.dao = dao;

    }

    public ILevelDAO getDao() {

       return this.dao;

    }

}

 

ExtJSONActionSuport.java

辅助类,继承了ActionSupport

import com.opensymphony.xwork2.ActionSupport;

 

public class ExtJSONActionSuport extends ActionSupport {

    private int totalCount = 0;// 总数

    private transient int start = 0;// 开始数

    private transient int limit = 0;// 限制数量

    private String jsonString = "";

 

    public String getJsonString() {

       return jsonString;

    }

 

    public void setJsonString(String jsonString) {

       this.jsonString = jsonString;

    }

 

    public String jsonExecute() throws Exception {

       return super.execute();

    }

 

     

 

    public int getTotalCount() {

       return totalCount;

    }

 

    public void setTotalCount(int totalCount) {

       this.totalCount = totalCount;

    }

 

    public int getStart() {

       return start;

    }

 

    public void setStart(int start) {

       this.start = start;

    }

 

    public int getLimit() {

       return limit;

    }

 

    public void setLimit(int limit) {

       this.limit = limit;

    }

}

 

 

LevelAction.java

 

import java.util.List;

import java.util.ArrayList;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONArray;

import privilege.database.Level;

import privilege.service.*;

import commons.utils.action.ExtJSONActionSuport;

 

public class LevelAction extends ExtJSONActionSuport {

    private static final long serialVersionUID = 1L;

    private Level level = null;

    private List<Level> levels = new ArrayList<Level>(0);

    private ILevelService levelService = null;

    private String delData;

 

    public String execute() {

       return this.SUCCESS;

    }

 

    @Override

    public String jsonExecute() throws Exception {

       if (this.getDelData() != null && !"".equals(this.getDelData())) {

           if (this.getDelData().indexOf(",") < 0) {

              this.levelService.removeLevelById(Long.parseLong(this.getDelData()));

               System.out.println("del_id:" + getDelData());

           } else {

              String id[] = this.getDelData().split(",");

              for (int i = 0; i < id.length; i++) {

                  System.out.println("del:" + id[i]);

                  this.levelService.removeLevelById(Long.parseLong(id[i]));

               }

           }

       }

       HttpSession session = ServletActionContext.getRequest().getSession();

       Object o = null;// session.getAttribute("Level_Data1");

       if (o == null) {

           try {

              this.levels = this.getLevelService().findAllLevels();

              session.setAttribute("Level_Data1", this.levels);

              System.out.println("query database");

           } catch (Exception e) {

              e.printStackTrace();

           }

       } else {

           this.setLevels(((List<Level>) o));

       }

       this.setTotalCount(this.levels.size());

       JSONArray array = JSONArray.fromObject(this.levels);

       // System.out.println(this.getStart() + "---" + this.getLimit());

       this.setJsonString("{success:true,totalCount : " + this.getTotalCount() + ", list:" + array.toString() + "}");

       // System.out.println(this.getJsonString());

       return super.jsonExecute();

    }

 

    /**

     * Find an entity by its id (primary key).

     * 

     * @param id

     * @return The found entity instance or null if the entity does not exist.

     */

    public String findLevelById(Long id) {

       try {

           this.level = this.getLevelService().findLevelById(id);

       } catch (Exception e) {

           e.printStackTrace();

       }

       JSONArray array = JSONArray.fromObject(this.levels);

       this.setJsonString(array.toString());

       return SUCCESS;

    }

 

    public String findLevelById() {

       System.out.println(this.level.getLevelid());

       try {

           this.level = this.getLevelService().findLevelById(this.level.getLevelid());

       } catch (Exception e) {

           e.printStackTrace();

       }

       JSONArray array = JSONArray.fromObject(this.level);

       this.setJsonString(array.toString());

       this.setJsonString("{success:true,totalCount:1,list:" + array.toString() + "}");

       System.out.println(array.toString());

       return SUCCESS;

    }

 

    /**

     * @return Return all persistent instances of the <code>Level</code> entity.

     */

    public String getAllLevels() {

       try {

           this.levels = this.getLevelService().findAllLevels();

       } catch (Exception e) {

           e.printStackTrace();

       }

       return SUCCESS;

    }

 

 

    /**

     * Make the given instance managed and persistent.

     * 

     * @return

     */

    public String persistLevel() {

       System.out.println(this.level.getLevelid() + "---" + this.level.getLevelname() + "---"

              + this.level.getDescription());

       this.setJsonString("{success:true}");

       try {

           this.getLevelService().persistLevel(this.getLevel());

       } catch (Exception e) {

           e.printStackTrace();

       }

       return SUCCESS;

    }

 

    /**

     * Remove the given persistent instance.

     * 

     * @return

     */

    public String removeLevel() {

       try {

           this.getLevelService().removeLevel(this.getLevel());

       } catch (Exception e) {

           e.printStackTrace();

       }

       return SUCCESS;

    }

 

    /**

     * Remove an entity by its id (primary key). *

     * 

     * @return

     */

    public String removeLevelById(Long id) {

       try {

           this.getLevelService().removeLevelById(id);

       } catch (Exception e) {

           e.printStackTrace();

       }

       return SUCCESS;

    }

 

    public Level getLevel() {

       return level;

    }

 

    public void setLevel(Level level) {

       this.level = level;

    }

 

    public List<Level> getLevels() {

       return levels;

    }

 

    public void setLevels(List<Level> levels) {

       this.levels = levels;

    }

 

    public ILevelService getLevelService() {

       return levelService;

    }

 

    public void setLevelService(ILevelService levelService) {

       this.levelService = levelService;

    }

 

    public String getDelData() {

       return delData;

    }

 

    public void setDelData(String delData) {

       this.delData = delData;

    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值