Struts2
额,能不能给一个完整的Struts2的增删改查的例子,自己使用。。。。
------解决方案--------------------public interface ProductDao {
public void save(Product p);//添加产品
public Product queryByID(String id);//按ID查询
public void delete(String id);//删除产品
public void Update(Product p);//更新产品
public List queryAll();//查询全部产品s
}
public class ProductDaoImpl implements ProductDao {
//获得一个session,打开一个交易,保存,提交,关闭
public void save(Product p) {
Session session=HibernateSessionFactory.getSession();
session.beginTransaction();
session.save(p);
session.getTransaction().commit();
session.close();
}
//id查询:获得一个session hql 查询hql
public Product queryByID(String id) {
Session session=HibernateSessionFactory.getSession();
String hql="from Product as pro where pro.id=?";
Query q=session.createQuery(hql);
q.setString(0,id);
List list=q.list();
Product p=null;
Iterator iter=list.iterator();
if(iter.hasNext()){
p=(Product)iter.next();
}
session.close();
return p;
}
//获得一个session,打开一个交易,hql, 执行hql
public void delete(String id) {
Session session=HibernateSessionFactory.getSession();
session.beginTransaction();
String hql="delete Product where id=?";//修改hql语句
Query q=session.createQuery(hql);
q.setString(0,id);
q.executeUpdate();
session.getTransaction().commit();
session.close();
}
public void Update(Product p) {
Session session=HibernateSessionFactory.getSession();
session.beginTransaction();
session.update(p);
session.getTransaction().commit();
session.close();
}
public List queryAll() {
Session session=HibernateSessionFactory.getSession();
String hql="from Product as pro";
Query q= session.createQuery(hql);
List list=q.list();
session.close();
return list;
}
}
public interface ProductService {
public void addProduct(Product p);//添加产品
public void deleteProduct(String id);//删除
public void updateProduct(Product p);//更新产品
public List queryAllProduct();//查询全部
public Product queryProductByID(String id);//按id查询
}
public class ProductServiceImpl implements ProductService {
public void addProduct(Product p) {
ProductDao dao=new ProductDaoImpl();
if(dao.queryByID(p.getId())==null){
dao.save(p);
}
else{
System.out.print("id已存在");