package org.fkit.test.impl;
import java.util.ArrayList;
import java.util.List;
import org.fkit.test.dao.PersonnelDAO;
import org.fkit.test.model.Personnel;
import org.fkit.test.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
public class PersonnelDAOImpl implements PersonnelDAO{
@Override
public void update(Personnel personnel) {
// TODO Auto-generated method stub
if(exists(personnel.getName())){
Personnel p = getByName(personnel.getName());
int id = p.getId();
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.getCurrentSession();
session.beginTransaction();
if(id > 0){
System.out.println(id);
personnel.setId(id);
session.update(personnel);
session.getTransaction().commit();
}
}else{
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.getCurrentSession();
session.beginTransaction();
session.save(personnel);
session.getTransaction().commit();
}
}
@Override
public void removeById(int id) {
// TODO Auto-generated method stub
Personnel personnel = new Personnel();
personnel.setId(id);
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.getCurrentSession();
session.beginTransaction();
session.delete(personnel);
session.getTransaction().commit();
}
@Override
public void remove(Personnel personnel) {
// TODO Auto-generated method stub
removeById(personnel.getId());
}
@SuppressWarnings("unchecked")
@Override
public List<Personnel> list() {
// TODO Auto-generated method stub
List<Personnel> personnels = new ArrayList<Personnel>();
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.getCurrentSession();
session.beginTransaction();
Query query = (Query) session.createQuery("from Personnel");
personnels = (List<Personnel>)query.list();
session.getTransaction().commit();
return personnels;
}
@Override
public Personnel getByName(String name) {
// TODO Auto-generated method stub
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.getCurrentSession();
session.beginTransaction();
Personnel personnel = (Personnel) session.createQuery("from Personnel p where p.name = ?")
.setString(0, name)
.uniqueResult();
session.getTransaction().commit();
return personnel;
}
@Override
public boolean exists(String name) {
// TODO Auto-generated method stub
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.getCurrentSession();
session.beginTransaction();
long count = (Long)session.createQuery("select count(*) from Personnel personnel where personnel.name = ?")
.setString(0, name)
.uniqueResult();
session.getTransaction().commit();
if(count > 0){
return true;
}else{
return false;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
本实例应用了struts2和hibernate实现对数据库的增删改查能力。程序虽小,却包含着数据库 操作的许多知识点。在eclipse环境下开发,数据库用的是mysql。代码通过测试。下载代码后 创建起数据库,即可运行查看效果。代码中连接数据库的参数:路径是localhost/test,用户 名:root,密码:root。表结构看model中的类。还有什么问题可以提出来讨论一下。希望大 家多交流。对代码结构有什么建议也可以提出讨论。
资源推荐
资源详情
资源评论
收起资源包目录
test2.rar (52个子文件)
.externalToolBuilders
org.eclipse.wst.jsdt.core.javascriptValidator (1).launch 548B
org.eclipse.wst.validation.validationbuilder (1).launch 547B
src
struts.xml 773B
org
fkit
test
dao
PersonnelDAO.java 367B
model
Personnel.hbm.xml 448B
Personnel.java 470B
util
HibernateUtil.java 727B
action
PersonnelAction.java 1KB
impl
PersonnelDAOImpl.java 3KB
service
PersonnelService.java 701B
hibernate.cfg.xml 1KB
.classpath 588B
build
classes
struts.xml 773B
org
fkit
test
dao
PersonnelDAO.class 447B
model
Personnel.hbm.xml 448B
Personnel.class 929B
util
HibernateUtil.class 940B
action
PersonnelAction.class 2KB
impl
PersonnelDAOImpl.class 3KB
service
PersonnelService.class 1KB
hibernate.cfg.xml 1KB
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 305B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 469B
.jsdtscope 503B
org.eclipse.jdt.core.prefs 364B
WebContent
META-INF
MANIFEST.MF 39B
Personnel.jsp 1KB
success.jsp 429B
WEB-INF
lib
javassist-3.12.0.GA.jar 618KB
xwork-core-2.3.3.jar 612KB
slf4j-api-1.6.1.jar 25KB
dom4j-1.6.1.jar 307KB
slf4j-jdk14-1.6.1.jar 9KB
asm-tree-3.3.jar 21KB
hibernate3.jar 3.97MB
asm-3.3.jar 43KB
hibernate-jpa-2.0-api-1.0.1.Final.jar 100KB
commons-fileupload-1.2.2.jar 58KB
antlr-2.7.6.jar 433KB
asm-commons-3.3.jar 37KB
struts2-core-2.3.3.jar 764KB
commons-lang3-3.1.jar 308KB
freemarker-2.3.19.jar 909KB
ognl-3.0.5.jar 222KB
commons-io-2.0.1.jar 156KB
mysql-connector-java-5.1.18-bin.jar 771KB
commons-collections-3.1.jar 546KB
jta-1.1.jar 11KB
web.xml 791B
.project 1KB
共 52 条
- 1
碧波之心
- 粉丝: 7
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页