欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SSH sql+entity+dao+biz

程序员文章站 2022-06-09 17:10:47
...

Sql 语句

-- 以system登录
create user fav identified by bdqn;
grant dba to fav;
-- 以fav登录
create table FAVORITE
(
  F_ID    NUMBER(19) not null,
  F_LABEL VARCHAR2(200 CHAR) not null,
  F_URL   VARCHAR2(200 CHAR) not null,
  F_TAGS  VARCHAR2(200 CHAR),
  F_DESC  VARCHAR2(500 CHAR)
);
alter table FAVORITE
  add primary key (F_ID);
create table TAG
(
  T_ID    NUMBER(19) not null,
  T_NAME  VARCHAR2(100 CHAR) not null,
  T_COUNT NUMBER(19) not null
);
alter table TAG
  add primary key (T_ID);
-- 建了默认的序列
create sequence hibernate_sequence;
insert into FAVORITE (F_ID, F_LABEL, F_URL, F_TAGS, F_DESC)
values (hibernate_sequence.nextval, 'Spring', 'http://www.spring.org', 'spring', 'Spring官网');
commit;
insert into TAG (T_ID, T_NAME, T_COUNT)
values (hibernate_sequence.nextval, 'spring', 1);
commit;

 

Favorite 实体类及映射文件

package cn.entity;
@SuppressWarnings("serial")
public class Favorite implements java.io.Serializable {
	private Integer id;
	private String label;
	private String url;
	private String tags;
	private String desc;
	@Override
	public String toString() {
		return "Favorite [id=" + id + ", label=" + label + ", url=" + url
				+ ", tags=" + tags + ", desc=" + desc + "]";
	}
	public Favorite() {
		super();
	}
	public Favorite(String label, String url, String tags, String desc) {
		super();
		this.label = label;
		this.url = url;
		this.tags = tags;
		this.desc = desc;
	}
	public Favorite(Integer id, String label, String url, String tags,
			String desc) {
		super();
		this.id = id;
		this.label = label;
		this.url = url;
		this.tags = tags;
		this.desc = desc;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getLabel() {
		return label;
	}
	public void setLabel(String label) {
		this.label = label;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getTags() {
		return tags;
	}
	public void setTags(String tags) {
		this.tags = tags;
	}
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
}

 

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Mapping file autogenerated by MyEclipse Persistence Tools -->
<hibernate-mapping>
	<class name="cn.entity.Favorite" table="FAVORITE" schema="FAV">
		<id name="id" type="java.lang.Integer">
			<column name="F_ID" scale="0" />
			<generator class="native" />
		</id>
		<property name="label" type="java.lang.String">
			<column name="F_LABEL" length="200" not-null="true" />
		</property>
		<property name="url" type="java.lang.String">
			<column name="F_URL" length="200" not-null="true" />
		</property>
		<property name="tags" type="java.lang.String">
			<column name="F_TAGS" length="200" />
		</property>
		<property name="desc" type="java.lang.String">
			<column name="F_DESC" length="500" />
		</property>
	</class>
</hibernate-mapping>

 

Tag 实体类及映射文件

package cn.entity;
@SuppressWarnings("serial")
public class Tag implements java.io.Serializable {
	private Integer id;
	private String name;
	private Integer count;
	@Override
	public String toString() {
		return "Tag [id=" + id + ", name=" + name + ", count=" + count + "]";
	}
	public Tag() {
		super();
	}
	public Tag(String name, Integer count) {
		super();
		this.name = name;
		this.count = count;
	}
	public Tag(Integer id, String name, Integer count) {
		super();
		this.id = id;
		this.name = name;
		this.count = count;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getCount() {
		return count;
	}
	public void setCount(Integer count) {
		this.count = count;
	}
}

 

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Mapping file autogenerated by MyEclipse Persistence Tools -->
<hibernate-mapping>
	<class name="cn.entity.Tag" table="TAG" schema="FAV">
		<id name="id" type="java.lang.Integer">
			<column name="T_ID" scale="0" />
			<generator class="native" />
		</id>
		<property name="name" type="java.lang.String">
			<column name="T_NAME" length="100" not-null="true" />
		</property>
		<property name="count" type="java.lang.Integer">
			<column name="T_COUNT" scale="0" not-null="true" />
		</property>
	</class>
</hibernate-mapping>

 

BaseDao Dao类

package cn.dao;
import java.io.Serializable;
import java.util.List;
/**
 * BaseDao接口,包含常用的方法 
 */
public interface BaseDao<T> {
	/**
	 * 查询所有的记录
	 */
	List<T> findAll(Class<T> entityClass);
	/**
	 * 根据主键加载一条数据。
	 * @param id 主键
	 * @return 查找到的数据
	 */
	T get(Class<T> entityClass,Serializable id);
	/**
	 * 添加数据到数据库。
	 * @param entity 将要添加的数据
	 * @return 包含了主键信息的数据
	 */
	T add(T entity);
	/**
	 * 根据主键删除数据。
	 * @param id
	 */
	void deleteById(Class<T> entityClass,Serializable id);
	/**
	 * 更新数据。
	 * @param entity 将要更新的数据,包含主键信息
	 */
	void update(T entity);
	/**
	 * 查找数据。
	 * @param condition 查询条件
	 * @return 查询结果
	 */
	List<T> find(T condition);
	/**
	 * 查找数据。
	 * @param hql 查询语句
	 * @return 查询结果
	 */
	List<T> find(String hql);
	/**
	 * 查找数据。
	 * @param hql 查询语句 paramNames 参数名 values参数值
	 * @return 查询结果
	 */
	List<T> find(String hql, String[] paramNames, Object[] values);
}

 

package cn.dao.impl;
import java.io.Serializable;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.dao.BaseDao;
/**
 * BaseDao 实现类
 */
public class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T> {
	@Override
	public T add(T entity) {
		super.getHibernateTemplate().save(entity);
		return entity;
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<T> find(T condition) {
		return super.getHibernateTemplate().findByExample(condition);
	}
	@Override
	public void update(T entity) {
		super.getHibernateTemplate().update(entity);
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<T> find(String hql) {
		return super.getHibernateTemplate().find(hql);
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<T> find(String hql, String[] paramNames, Object[] values) {
		return super.getHibernateTemplate().findByNamedParam(hql, paramNames, values);
	}
	@Override
	public T get(Class<T> entityClass, Serializable id) {
		return super.getHibernateTemplate().get(entityClass, id);
	}
	@Override
	public void deleteById(Class<T> entityClass, Serializable id) {
		super.getHibernateTemplate().delete(this.get(entityClass, id));
	}
	@Override
	public List<T> findAll(Class<T> entityClass) {
		return super.getHibernateTemplate().loadAll(entityClass);
	}
}

 

FavDAO Dao类

package cn.dao;
import java.util.List;
import cn.entity.Favorite;
/**
 * 收藏夹的DAO
 */
public interface FavDAO extends BaseDao<Favorite> {
	/**
	 * 根据指定的类型查询收藏列表
	 * @param type
	 * @return
	 */
	public List<Favorite> search(String type);
	
}

 

package cn.dao.impl;
import java.util.List;
import cn.dao.FavDAO;
import cn.entity.Favorite;
public class FavDAOImpl extends BaseDaoImpl<Favorite> implements FavDAO {
	@Override
	public List<Favorite> search(String type) {
		String hql = "from Favorite f where 1=1";
		if("-1".equals(type)){
			//-1 指全部,什么也不处理
		}else if("0".equals(type)){//0 表示未分类
			hql +="and f.tags is null or f.tags=''";
		}else if(type != null&&!"".equals(type)){
			hql+="and f.tags like '%"+type+"%'";
		}
		return super.find(hql);
	}
}

 

TagDAO Dao类

package cn.dao;
import java.util.List;
import cn.entity.Tag;
/**
 * 标签接口
 */
public interface TagDAO extends BaseDao<Tag> {
	public List<Tag> search(Tag condition);
	/**
	 * 根据标签名字查询标签
	 * @param tagName
	 * @return
	 */
	public Tag get(String tagName);
}

 

package cn.dao.impl;
import java.util.List;
import cn.dao.TagDAO;
import cn.entity.Tag;
public class TagDAOImpl extends BaseDaoImpl<Tag> implements TagDAO {
	@Override
	public Tag get(String tagName) {
		@SuppressWarnings("unchecked")
		List<Tag> rets = super.getHibernateTemplate().find("from Tag t where t.name = ?",tagName);
		if(rets.size() != 0){
			return rets.get(0);
		}
		return null;
	}
	@Override
	public List<Tag> search(Tag condition) {
		if(condition==null){
			return super.find("from Tag t order by t.name asc");
		}else{
			return super.find(condition);
		}
	}
}

 

FavBiz 业务类

package cn.biz;
import java.util.List;
import cn.entity.Favorite;
public interface FavBiz {
	public void add(Favorite fav);
	public List<Favorite> search(String type);
}

 

package cn.biz.impl;
import java.util.List;
import cn.biz.FavBiz;
import cn.biz.TagBiz;
import cn.dao.FavDAO;
import cn.entity.Favorite;
public class FavBizImpl implements FavBiz {
	private FavDAO favDAO;
	public void setFavDAO(FavDAO favDAO) {
		this.favDAO = favDAO;
	}
	private TagBiz tagBiz;
	public void setTagBiz(TagBiz tagBiz) {
		this.tagBiz = tagBiz;
	}
	@Override
	public void add(Favorite fav) {
		//往 Fav 表中加入一条记录
		favDAO.add(fav);
		String tags = fav.getTags();
		//循环处理每一条标签
		if(null != tags && !"".equals(tags.trim())){
			String[] arrTags = tags.split(",");
			if(null != arrTags && arrTags.length > 0){
				for (String tag : arrTags) {
					//调用另一个业务类的方法
					tagBiz.dealWithTag(tag);
				}
			}
		}
	}
	@Override
	public List<Favorite> search(String type) {
		return this.favDAO.search(type);
	}

}

 

TagBiz 业务类

package cn.biz;
import java.util.List;
import cn.entity.Tag;
/**
 * 标签
 *
 */
public interface TagBiz {
	/**
	 * 通过标签名字进行处理
	 * @param tagName
	 */
	public void dealWithTag(String tagName);
	public List<Tag> search(Tag condition);
}

 

package cn.biz.impl;
import java.util.List;
import cn.biz.TagBiz;
import cn.dao.TagDAO;
import cn.entity.Tag;
public class TagBizImpl implements TagBiz {
	private TagDAO tagDAO;
	public void setTagDAO(TagDAO tagDAO) {
		this.tagDAO = tagDAO;
	}
	@Override
	public void dealWithTag(String tagName) {
		Tag tag = tagDAO.get(tagName);
		//有则加 1 更新
		if(null != tag){
			tag.setCount(tag.getCount()+1);
			tagDAO.update(tag);
		}else{
			//没有则新加 1 个
			tag = new Tag();
			tag.setName(tagName);
			tag.setCount(1);
			tagDAO.add(tag);
		}
	}
	@Override
	public List<Tag> search(Tag condition) {
		return this.tagDAO.search(condition);
	}
}