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

SSH sql+entity+dao+biz+test

程序员文章站 2022-06-09 17:20:57
...

SQL 语句

create user standDB identified by 123456;
grant connect,resource to standDB;

-- Oracle 删除用户
--drop user standDB cascade;

--标准信息表
create table standards (
       std_id number(10) primary key not null,--标准 id
       std_num varchar2(50) not null,--标准号
       zhname varchar2(40) not null,--中文名称
       std_version varchar2(10) not null,--版本
       keys varchar2(50) not null,--关键字/词
       release_date date null,--发布日期
       impl_date date null,--实施日期
       package_path varchar2(100) not null--//附件路径
);

create sequence seq_standards increment by 1 start with 1 cache 10;

insert into standards
  (std_id, std_num, zhname, std_version, keys, release_date, impl_date, package_path)
values
  (1, 'GB 2312-1511', 'Java 典型', '1999', 'Java',
   to_date('1991-10-16','yyyy-mm-dd'),  to_date('1999-10-16','yyyy-mm-dd'), 'xxx.jpg');
insert into standards
  (std_id, std_num, zhname, std_version, keys, release_date, impl_date, package_path)
values
  (seq_standards.nextval, 'GB 2312-1511', 'Java 典型1', '1999', 'Java',
   to_date('1991-10-16','yyyy-mm-dd'),  to_date('1999-10-16','yyyy-mm-dd'), 'xxx.jpg');
insert into standards
  (std_id, std_num, zhname, std_version, keys, release_date, impl_date, package_path)
values
  (seq_standards.nextval, 'GB 2312-1512', 'Java 典型2', '1999', 'Java',
   to_date('1991-10-16','yyyy-mm-dd'),  to_date('1999-10-16','yyyy-mm-dd'), 'xxx.jpg');
insert into standards
  (std_id, std_num, zhname, std_version, keys, release_date, impl_date, package_path)
values
  (seq_standards.nextval, 'GB 2312-1541', 'Java 典型3', '1999', 'Java',
   to_date('1991-10-16','yyyy-mm-dd'),  to_date('1999-10-16','yyyy-mm-dd'), 'xxx.jpg');
insert into standards
  (std_id, std_num, zhname, std_version, keys, release_date, impl_date, package_path)
values
  (seq_standards.nextval, 'GB 2312-1551', 'Java 典型4', '1999', 'Java',
   to_date('1991-10-16','yyyy-mm-dd'),  to_date('1999-10-16','yyyy-mm-dd'), 'xxx.jpg');
insert into standards
  (std_id, std_num, zhname, std_version, keys, release_date, impl_date, package_path)
values
  (seq_standards.nextval, 'GB 2312-1561', 'Java 典型5', '1999', 'Java',
   to_date('1991-10-16','yyyy-mm-dd'),  to_date('1999-10-16','yyyy-mm-dd'), 'xxx.jpg');
insert into standards
  (std_id, std_num, zhname, std_version, keys, release_date, impl_date, package_path)
values
  (seq_standards.nextval, 'GB 2312-1511', 'Java 典型6', '1999', 'Java',
   to_date('1991-10-16','yyyy-mm-dd'),  to_date('1999-10-16','yyyy-mm-dd'), 'xxx.jpg');
insert into standards
  (std_id, std_num, zhname, std_version, keys, release_date, impl_date, package_path)
values
  (seq_standards.nextval, 'GB 2312-1511', 'Java 典型7', '1999', 'Java',
   to_date('1991-10-16','yyyy-mm-dd'),  to_date('1999-10-16','yyyy-mm-dd'), 'xxx.jpg');
insert into standards
  (std_id, std_num, zhname, std_version, keys, release_date, impl_date, package_path)
values
  (seq_standards.nextval, 'GB 2312-1511', 'Java 典型8', '1999', 'Java',
   to_date('1991-10-16','yyyy-mm-dd'),  to_date('1999-10-16','yyyy-mm-dd'), 'xxx.jpg');
insert into standards
  (std_id, std_num, zhname, std_version, keys, release_date, impl_date, package_path)
values
  (seq_standards.nextval, 'GB 2312-1511', 'Java 典型9', '1999', 'Java',
   to_date('1991-10-16','yyyy-mm-dd'),  to_date('1999-10-16','yyyy-mm-dd'), 'xxx.jpg');

select std_id, std_num, zhname, std_version, keys, release_date, impl_date, package_path from standards

 

 Standards 实体类及映射文件

package cn.entity;
import java.util.Date;
/**
 * 标准信息表
 * */
@SuppressWarnings("serial")
public class Standards implements java.io.Serializable {
	private Integer stdId;//标准 id
	private String stdNum;//标准号
	private String zhname;//中文名称
	private String stdVersion;//版本
	private String keys;//关键字/词
	private Date releaseDate;//发布日期
	private Date implDate;//实施日期
	private String packagePath;//附件路径
	public Standards() {
		super();
	}
	public Standards(String stdNum, String zhname, String stdVersion,
			String keys, Date releaseDate, Date implDate, String packagePath) {
		super();
		this.stdNum = stdNum;
		this.zhname = zhname;
		this.stdVersion = stdVersion;
		this.keys = keys;
		this.releaseDate = releaseDate;
		this.implDate = implDate;
		this.packagePath = packagePath;
	}
	public Standards(Integer stdId, String stdNum, String zhname,
			String stdVersion, String keys, Date releaseDate, Date implDate,
			String packagePath) {
		super();
		this.stdId = stdId;
		this.stdNum = stdNum;
		this.zhname = zhname;
		this.stdVersion = stdVersion;
		this.keys = keys;
		this.releaseDate = releaseDate;
		this.implDate = implDate;
		this.packagePath = packagePath;
	}
	public Integer getStdId() {
		return stdId;
	}
	public void setStdId(Integer stdId) {
		this.stdId = stdId;
	}
	public String getStdNum() {
		return stdNum;
	}
	public void setStdNum(String stdNum) {
		this.stdNum = stdNum;
	}
	public String getZhname() {
		return zhname;
	}
	public void setZhname(String zhname) {
		this.zhname = zhname;
	}
	public String getStdVersion() {
		return stdVersion;
	}
	public void setStdVersion(String stdVersion) {
		this.stdVersion = stdVersion;
	}
	public String getKeys() {
		return keys;
	}
	public void setKeys(String keys) {
		this.keys = keys;
	}
	public Date getReleaseDate() {
		return releaseDate;
	}
	public void setReleaseDate(Date releaseDate) {
		this.releaseDate = releaseDate;
	}
	public Date getImplDate() {
		return implDate;
	}
	public void setImplDate(Date implDate) {
		this.implDate = implDate;
	}
	public String getPackagePath() {
		return packagePath;
	}
	public void setPackagePath(String packagePath) {
		this.packagePath = packagePath;
	}
	@Override
	public String toString() {
		return "Standards [stdId=" + stdId + ", stdNum=" + stdNum + ", zhname="
				+ zhname + ", stdVersion=" + stdVersion + ", keys=" + keys
				+ ", releaseDate=" + releaseDate + ", implDate=" + implDate
				+ ", packagePath=" + packagePath + "]";
	}
}

 

<?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.Standards" table="STANDARDS" schema="STANDDB">
        <id name="stdId" type="java.lang.Integer">
            <column name="STD_ID" precision="10" scale="0" />
            <generator class="sequence">
            	<param name="sequence">seq_standards</param>
            </generator>
        </id>
        <property name="stdNum" type="java.lang.String">
            <column name="STD_NUM" length="50" not-null="true" />
        </property>
        <property name="zhname" type="java.lang.String">
            <column name="ZHNAME" length="40" not-null="true" />
        </property>
        <property name="stdVersion" type="java.lang.String">
            <column name="STD_VERSION" length="10" not-null="true" />
        </property>
        <property name="keys" type="java.lang.String">
            <column name="KEYS" length="50" not-null="true" />
        </property>
        <property name="releaseDate" type="java.util.Date">
            <column name="RELEASE_DATE" length="7" />
        </property>
        <property name="implDate" type="java.util.Date">
            <column name="IMPL_DATE" length="7" />
        </property>
        <property name="packagePath" type="java.lang.String">
            <column name="PACKAGE_PATH" length="100" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

 

 StandardsDao DAO类

package cn.dao;
import java.util.List;
import cn.entity.Standards;
public interface StandardsDao {
	/**
	 * 列表
	 * @return
	 */
	List<Standards> findAll();
	/**
	 * 列表分页
	 * @param begin
	 * @param end
	 * @return
	 */
	List<Standards> findByPage(int begin,int end);
	/**
	 * 根据标准号查询列表分页
	 * @param begin
	 * @param end
	 * @param stdNum
	 * @return
	 */
	List<Standards> findByPageOfsearch(int begin,int end,String stdNum);
	/**
	 * 根据 id 查询
	 * @param stdId
	 * @return
	 */
	Standards findById(Integer stdId);
	/**
	 * 修改内容
	 * @param standards
	 */
	void update(Standards standards);
	/**
	 * 增加内容
	 * @param standards
	 */
	void insert(Standards standards);
	/**
	 * 多条信息删除
	 * @param stdIds
	 */
	void deleteByIds(Integer[] stdIds);
}

 

package cn.dao.impl;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.dao.StandardsDao;
import cn.entity.Standards;
public class StandardsDaoImpl extends HibernateDaoSupport implements
StandardsDao {
	@SuppressWarnings("unchecked")
	@Override
	public List<Standards> findAll() {
		return super.getHibernateTemplate().find("from Standards");
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Standards> findByPage(int begin, int end) {
		DetachedCriteria criteria=DetachedCriteria.forClass(Standards.class);
		return super.getHibernateTemplate().findByCriteria(criteria, begin, end);
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Standards> findByPageOfsearch(int begin, int end, String stdNum) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Standards.class);
		criteria.add(Restrictions.like("stdNum", "%"+stdNum+"%"));
		return super.getHibernateTemplate().findByCriteria(criteria, begin, end);
	}
	@Override
	public Standards findById(Integer stdId) {
		return (Standards) super.getHibernateTemplate().get(Standards.class, stdId);
	}
	@Override
	public void update(Standards standards) {
		super.getHibernateTemplate().update(standards); 
	}
	@Override
	public void insert(Standards standards) {
		super.getHibernateTemplate().save(standards);
	}
	@Override
	public void deleteByIds(Integer[] stdIds) {
		for (int i = 0; i < stdIds.length; i++) {
			Standards standard = (Standards) super.getHibernateTemplate().get(Standards.class, stdIds[i]);
			if (standard != null) {
				super.getHibernateTemplate().delete(standard);
			}
		}
	}
}

 

 StandardsBiz 业务类

package cn.biz;
import java.util.List;
import cn.entity.Standards;
public interface StandardsBiz {
	/**
	 * 列表
	 * @return
	 */
	List<Standards> findAll();
	/**
	 * 列表分页
	 * @param begin
	 * @param end
	 * @return
	 */
	List<Standards> findByPage(int pageIndex, int pageSize);
	/**
	 * 根据标准号查询列表分页
	 * @param begin
	 * @param end
	 * @param stdNum
	 * @return
	 */
	List<Standards> findByPageOfsearch(int pageIndex,int pageSize,String stdNum);
	/**
	 * 根据 id 查询
	 * @param stdId
	 * @return
	 */
	Standards findById(Integer stdId);
	/**
	 * 修改内容
	 * @param standards
	 */
	void update(Standards standards);
	/**
	 * 增加内容
	 * @param standards
	 */
	void insert(Standards standards);
	/**
	 * 多条信息删除
	 * @param stdIds
	 */
	void deleteByIds(Integer[] stdIds);
}

 

package cn.biz.impl;
import java.util.List;
import cn.biz.StandardsBiz;
import cn.dao.StandardsDao;
import cn.entity.Standards;
public class StandardsBizImpl implements StandardsBiz {
	private StandardsDao standardsDao;
	public void setStandardsDao(StandardsDao standardsDao) {
		this.standardsDao = standardsDao;
	}
	@Override
	public List<Standards> findAll() {
		return standardsDao.findAll();
	}
	@Override
	public List<Standards> findByPage(int pageIndex, int pageSize) {
		int begin = (pageIndex - 1) * pageSize;
		return standardsDao.findByPage(begin, pageSize);
	}
	@Override
	public List<Standards> findByPageOfsearch(int pageIndex, int pageSize,
			String stdNum) {
		int begin = (pageIndex - 1) * pageSize;
		return standardsDao.findByPageOfsearch(begin, pageSize,stdNum);
	}
	@Override
	public Standards findById(Integer stdId) {
		return standardsDao.findById(stdId);
	}
	@Override
	public void update(Standards standards) {
		standardsDao.update(standards);
	}
	@Override
	public void insert(Standards standards) {
		standardsDao.insert(standards);
	}
	@Override
	public void deleteByIds(Integer[] stdIds) {
		for (Integer integer : stdIds) {
			System.out.println(integer);
		}
		standardsDao.deleteByIds(stdIds);
	}
}

 

StandardsTest 测试类

package cn.test;
import java.sql.Date;
import java.util.List;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.biz.StandardsBiz;
import cn.entity.Standards;
public class StandardsTest {
	StandardsBiz standardsBiz;
	static ApplicationContext ctx;
	@BeforeClass  
	public static void init(){
		ctx=new	ClassPathXmlApplicationContext("applicationContext.xml");
	}
	@Before 
	public void setUp(){
		standardsBiz=(StandardsBiz) ctx.getBean("standardsBiz");
	}
	@After
	public void tearDown(){
		standardsBiz=null;
	}
	@AfterClass
	public static void destory(){
		ctx=null;
	}
	//@Test
	public void testFindAll(){
		List<Standards> standards = standardsBiz.findAll();
		for (Standards standard : standards) {
			System.out.println(standard);
		}
	}
	//@Test
	public void testFindByPage(){
		List<Standards> standards = standardsBiz.findByPage(2, 3);
		for (Standards standard : standards) {
			System.out.println(standard);
		}
	}
	//@Test
	public void testFindByPageOfsearch(){
		List<Standards> standards = standardsBiz.findByPageOfsearch(2, 3, "号");
		for (Standards standard : standards) {
			System.out.println(standard);
		}
	}
	//@Test
	public void testFindById(){
		Standards standards = standardsBiz.findById(2);
		System.out.println(standards);
	}
	//@Test
	public void testUpdate(){
		@SuppressWarnings("deprecation")
		Standards standards = new Standards(1, "aaaa", "bbb", "cccc", "bbbb", new Date(1911, 10, 16), new Date(1912, 10, 18), "imges.abc");
		standardsBiz.update(standards);
	}
	//@Test
	public void testInsert(){
		@SuppressWarnings("deprecation")
		Standards standards = new Standards( "aaaa", "bbb", "cccc", "bbbb", new Date(1911, 10, 16), new Date(1912, 10, 18), "imges.abc");
		standardsBiz.insert(standards);
	}
	//@Test
	public void testDeleteByIds(){
		Integer[] ids = {1,3,5};
		standardsBiz.deleteByIds(ids);
	}
}