首页 > 代码库 > 房屋代码

房屋代码

package com.dao;

import java.util.List;

import com.entity.TblHouse;
import com.entity.WhereParam;

public interface HouseDao {

public List<TblHouse> findHouses(String hql,int offset,int pageSize);

public int findRows();

//增加一条房源信息
public void saveHouse(TblHouse house);
//删除一条房源信息
public void delHouse(TblHouse house);
//修改一条房源信息
public void updateHouse(TblHouse house);
//查询一条房源信息
public TblHouse findHouse(Integer houseid);

public int findRows(String string);

public List<TblHouse> findHouses(String hql, int offset, int pageSize,
WhereParam wp);
}

 

====================================================

package com.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dao.HouseDao;
import com.entity.TblHouse;
import com.entity.TblUsers;
import com.entity.WhereParam;

public class HouseDaoImpl extends HibernateDaoSupport implements HouseDao{

public List<TblHouse> findHouses(String hql, int offset, int pageSize) {
//1.获取数据库会话Session
Session session=this.getSession();
List<TblHouse> hlist=null;
try {
//2.创建Query执行对象
Query query=session.createQuery(hql);
//3.查询返回对象集合
hlist=query.setFirstResult(offset).setMaxResults(pageSize).list();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
session.close();//必须要关,不然几次之后就卡死了
}
return hlist;
}

public int findRows() {
//这个模板Template,就是把上面的代码再次封装的使用,但是只能给一个hql语句,而因为上面要分页没用封装
return this.getHibernateTemplate().find("from TblHouse").size();
}

public void saveHouse(TblHouse house) {
// TODO Auto-generated method stub
this.getHibernateTemplate().save(house);
}

public void delHouse(TblHouse house) {
// TODO Auto-generated method stub
this.getHibernateTemplate().delete(house);
}

public void updateHouse(TblHouse house) {
// TODO Auto-generated method stub
this.getHibernateTemplate().update(house);
}

public TblHouse findHouse(Integer houseid) {
return this.getHibernateTemplate().get(TblHouse.class, houseid);//获取实体的class和主键
}

public int findRows(String hql) {

return this.getHibernateTemplate().find(hql).size();
}

@SuppressWarnings("unchecked")
public List<TblHouse> findHouses(String hql, int offset, int pageSize,
WhereParam wp) {
Session session=this.getSession();
List<TblHouse> houselist=null;

try {
Query query=session.createQuery(hql);
query.setProperties(wp);//给hql语句赋值
houselist=query.setFirstResult(offset).setMaxResults(pageSize).list();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
session.close();
}
return houselist;
}

}

=================================================


package com.service;

import java.util.List;

import com.entity.TblHouse;
import com.entity.WhereParam;

public interface HouseService {

//房源列表
public List<TblHouse> findHouses(int offset,int pageSize);
//房源总数
public int findRows();

//增加一条房源信息
public void saveHouse(TblHouse house);
//删除一条房源信息
public void delHouse(Integer houseid);//action传给service的是id,查一下,有才删除
//修改一条房源信息
public void updateHouse(TblHouse house);
//查询一条房源信息
public TblHouse findHouse(Integer houseid);

public int findRows(WhereParam wp);
//模糊查询
public List<TblHouse> findHouses(int offset, int pageSize, WhereParam wp);


}

 

===============================================

package com.service.impl;

import java.util.List;

import com.dao.HouseDao;
import com.entity.TblHouse;
import com.entity.WhereParam;
import com.service.HouseService;

public class HouseServiceImpl implements HouseService{

private HouseDao houseDao;

//set get后面用来依赖注入

public HouseDao getHouseDao() {
return houseDao;
}

public void setHouseDao(HouseDao houseDao) {
this.houseDao = houseDao;
}

public List<TblHouse> findHouses( int offset, int pageSize) {
String hql="from TblHouse th order by th.id desc";

return this.houseDao.findHouses(hql, offset, pageSize);
}

public int findRows() {
// TODO Auto-generated method stub
return this.houseDao.findRows();
}

//增删改查操作

public void saveHouse(TblHouse house) {
// TODO Auto-generated method stub
this.houseDao.saveHouse(house);
}

public void delHouse(Integer houseid) {
// TODO Auto-generated method stub
TblHouse house=this.houseDao.findHouse(houseid);
if(house!=null){
this.houseDao.delHouse(house);
}
}

public void updateHouse(TblHouse house) {
// TODO Auto-generated method stub
this.houseDao.updateHouse(house);
}

public TblHouse findHouse(Integer houseid) {
// TODO Auto-generated method stub
return this.houseDao.findHouse(houseid);
}

public int findRows(WhereParam wp) {

StringBuffer hql=new StringBuffer("from TblHouse th where 1=1");
if(wp.getUsername()!=null&&!"".equals(wp.getUsername())){
hql.append(" and th.user.username = ‘"+wp.getUsername()+"‘");
}
//模糊查询
if(wp.getTitle()!=null && !"".equals(wp.getTitle())){
hql.append(" and th.title like ‘%"+wp.getTitle()+"%‘");
}

return this.houseDao.findRows(hql.toString());
}

public List<TblHouse> findHouses( int offset, int pageSize,
WhereParam wp) {
StringBuffer hql=new StringBuffer("from TblHouse th where 1=1");
if(wp.getUsername()!=null&&!"".equals(wp.getUsername())){
hql.append(" and th.user.username = :username");
}
if(wp.getTitle()!=null && !"".equals(wp.getTitle())){
hql.append(" and th.title like concat(‘%‘,:title,‘%‘)");//拼接
}

return this.houseDao.findHouses(hql.toString(), offset, pageSize,wp);
}
}

=========================================================

package com.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext;

import com.entity.TblHouse;
import com.entity.TblUsers;
import com.entity.WhereParam;
import com.opensymphony.xwork2.ActionSupport;
import com.service.HouseService;
import com.service.UsersService;

public class HouseAction extends ActionSupport{

private HouseService houseService;
private TblHouse house;//读取和返回对象的属性
private Integer houseid;//接收前台请求的房源主键id


//分页所需属性参数
private List<TblHouse> hlist; //1.对象集合
private int curPage=1; //2.当前页码
private int totalRows; //3.总条数
private int totalPage; //4.总页数
private int offset; //5.起始行
private int pageSize=5; //6.每页大小


//图片上传
private File pic;
private String picContextType;
private String picFileName;
//模糊查询对象
private WhereParam wp;
//模糊查询的用户列表
private List<TblUsers> ulist;
private UsersService usersService;


public String saveHouse() throws IOException{
//上传图片保存路径到服务器
imgUpload();
//7.持久化数据到数据库
this.houseService.saveHouse(house);
return SUCCESS;
}
//删除房源信息
public String delHouse(){
this.houseService.delHouse(houseid);
return SUCCESS;
}

public String updateHouse() throws IOException{
//上传图片保存路径到服务器
if(pic!=null){
imgUpload();
}
//7.持久化数据到数据库
this.houseService.updateHouse(house);
return SUCCESS;
}

//根据id查询房源信息
public String findHouse(){
house=this.houseService.findHouse(houseid);
return SUCCESS;
}

private void imgUpload() throws IOException {
//1.创建输入输出流对象
FileInputStream fis=null;
FileOutputStream fos=null;
//2.截取图片后缀名xxx.jpg
String suffix=this.picFileName.substring(this.picFileName.length()-4);
//3.创建新的图片文件名
String newFileName=System.currentTimeMillis()+suffix;
//4.获取文件保存路径C://newPic/upload/.jpg
String savePath=ServletActionContext.getServletContext().getRealPath("/upload/"+newFileName);
try {
//5.边读边写,上传图片
fis=new FileInputStream(pic);
fos=new FileOutputStream(savePath);
IOUtils.copy(fis, fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
fos.close();
fis.close();
}
//6.保存图片所在服务器相对路径,放入house对象
house.setPicPath("upload/"+newFileName);
}


public String findHouseList(){
//下拉列表,模糊查询所有的方法
ulist=usersService.findAllUsers();

//-----------------------

if(wp!=null){
//查询总条数
totalRows=houseService.findRows(wp);
//计算总页数
totalPage=(totalRows%pageSize==0) ? totalRows/pageSize : totalRows/pageSize+1;
//计算起始行(当前页码-1)*每页大小
offset=(curPage-1)*pageSize;
//获取数据列表
hlist=houseService.findHouses(offset, pageSize,wp);
}else{
findHouseNotParam();
}
return SUCCESS;
}
//没有对象属性的时候查询
private void findHouseNotParam() {
//查询总条数
totalRows=houseService.findRows();
//计算总页数
totalPage=(totalRows%pageSize==0) ? totalRows/pageSize : totalRows/pageSize+1;
//计算起始行(当前页码-1)*每页大小
offset=(curPage-1)*pageSize;
//获取数据列表
hlist=houseService.findHouses(offset, pageSize);
}


public List<TblHouse> getHlist() {
return hlist;
}

public void setHlist(List<TblHouse> hlist) {
this.hlist = hlist;
}

public int getCurPage() {
return curPage;
}

public void setCurPage(int curPage) {
this.curPage = curPage;
}

public int getTotalRows() {
return totalRows;
}

public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}

public int getTotalPage() {
return totalPage;
}

public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}

public int getOffset() {
return offset;
}

public void setOffset(int offset) {
this.offset = offset;
}

public int getPageSize() {
return pageSize;
}

public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}

public HouseService getHouseService() {
return houseService;
}

public void setHouseService(HouseService houseService) {
this.houseService = houseService;
}


public TblHouse getHouse() {
return house;
}


public void setHouse(TblHouse house) {
this.house = house;
}


public Integer getHouseid() {
return houseid;
}


public void setHouseid(Integer houseid) {
this.houseid = houseid;
}
public File getPic() {
return pic;
}
public void setPic(File pic) {
this.pic = pic;
}
public String getPicContextType() {
return picContextType;
}
public void setPicContextType(String picContextType) {
this.picContextType = picContextType;
}
public String getPicFileName() {
return picFileName;
}
public void setPicFileName(String picFileName) {
this.picFileName = picFileName;
}
public WhereParam getWp() {
return wp;
}
public void setWp(WhereParam wp) {
this.wp = wp;
}
public List<TblUsers> getUlist() {
return ulist;
}
public void setUlist(List<TblUsers> ulist) {
this.ulist = ulist;
}
public UsersService getUsersService() {
return usersService;
}
public void setUsersService(UsersService usersService) {
this.usersService = usersService;
}


}

 

房屋代码