`
chaoyi
  • 浏览: 309185 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

SSH sql+entity+dao+biz+test

阅读更多

SQL 语句

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

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

--问题表
create table questions (
       id number(10) primary key not null,--问题 id
       title varchar2(100) not null,-- 问题名
       detailDesc varchar2(300) null,-- 问题描述
       ansewerCount number(10) not null,--回答次数
       lastModidfied date null--最后修改时间
);
--应答表
create table answers (
       id number(10) primary key not null,--答案 id
       ansContent varchar2(300) not null,--答案内容
       ansDate date null,--回答时间
       qid number(10) not null,--问题 id
       foreign key(qid) references questions(id)--约束
);

create sequence seq_questions increment by 1 start with 1 cache 10;
create sequence seq_answers  increment by 1 start with 1 cache 10;

insert into questions
  (id, title, detaildesc, ansewercount, lastmodidfied)
values
  (seq_questions.nextval, '这是问题1', '这是描述1', 1, to_date('1999-10-16','yyyy-mm-dd'));
insert into questions
  (id, title, detaildesc, ansewercount, lastmodidfied)
values
  (seq_questions.nextval, '这是问题2', '这是描述2', 1, to_date('1999-10-16','yyyy-mm-dd'));
insert into questions
  (id, title, detaildesc, ansewercount, lastmodidfied)
values
  (seq_questions.nextval, '这是问题3', '这是描述3', 1, to_date('1999-10-16','yyyy-mm-dd'));
insert into questions
  (id, title, detaildesc, ansewercount, lastmodidfied)
values
  (seq_questions.nextval, '这是问题4', '这是描述4', 1, to_date('1999-10-16','yyyy-mm-dd'));
insert into questions
  (id, title, detaildesc, ansewercount, lastmodidfied)
values
  (seq_questions.nextval, '这是问题5', '这是描述5', 1, to_date('1999-10-16','yyyy-mm-dd'));

insert into answers
  (id, anscontent, ansdate, qid)
values
  (seq_answers.nextval, '这是回答1', to_date('1999-10-16','yyyy-mm-dd'), 2);
insert into answers
  (id, anscontent, ansdate, qid)
values
  (seq_answers.nextval, '这是回答2', to_date('1999-10-16','yyyy-mm-dd'), 3);
insert into answers
  (id, anscontent, ansdate, qid)
values
  (seq_answers.nextval, '这是回答3', to_date('1999-10-16','yyyy-mm-dd'), 4);

select id, anscontent, ansdate, qid from answers;
select id, title, detaildesc, ansewercount, lastmodidfied from questions

 

Duestions 实体类及映射文件

package cn.entity;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
/**
 * 问题表
 * */
@SuppressWarnings("serial")
public class Duestions implements java.io.Serializable {
	private Integer id;//问题 id
	private String title;//问题名
	private String detaildesc;//问题描述
	private Integer ansewercount;//回答次数
	private Date lastmodidfied;//最后修改时间
	@SuppressWarnings("rawtypes")
	private Set answerses = new HashSet(0);//多对一映射
	@Override
	public String toString() {
		return "Duestions [id=" + id + ", title=" + title + ", detaildesc="
				+ detaildesc + ", ansewercount=" + ansewercount
				+ ", lastmodidfied=" + lastmodidfied + "]";
	}
	public Duestions() {
	}
	public Duestions(String title, Integer ansewercount) {
		this.title = title;
		this.ansewercount = ansewercount;
	}
	@SuppressWarnings("rawtypes")
	public Duestions(String title, String detaildesc, Integer ansewercount,
			Date lastmodidfied, Set answerses) {
		this.title = title;
		this.detaildesc = detaildesc;
		this.ansewercount = ansewercount;
		this.lastmodidfied = lastmodidfied;
		this.answerses = answerses;
	}
	public Duestions(String title, String detaildesc, Integer ansewercount,
			Date lastmodidfied) {
		this.title = title;
		this.detaildesc = detaildesc;
		this.ansewercount = ansewercount;
		this.lastmodidfied = lastmodidfied;
	}
	public Integer getId() {
		return this.id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getTitle() {
		return this.title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getDetaildesc() {
		return this.detaildesc;
	}
	public void setDetaildesc(String detaildesc) {
		this.detaildesc = detaildesc;
	}
	public Integer getAnsewercount() {
		return this.ansewercount;
	}
	public void setAnsewercount(Integer ansewercount) {
		this.ansewercount = ansewercount;
	}
	public Date getLastmodidfied() {
		return this.lastmodidfied;
	}
	public void setLastmodidfied(Date lastmodidfied) {
		this.lastmodidfied = lastmodidfied;
	}
	@SuppressWarnings("rawtypes")
	public Set getAnswerses() {
		return this.answerses;
	}
	@SuppressWarnings("rawtypes")
	public void setAnswerses(Set answerses) {
		this.answerses = answerses;
	}
}

 

<?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.Duestions" table="QUESTIONS" schema="ONLINEDB">
        <id name="id" type="java.lang.Integer">
            <column name="ID" precision="10" scale="0" />
            <generator class="sequence">
            	<param name="sequence">seq_questions</param>
            </generator>
        </id>
        <property name="title" type="java.lang.String">
            <column name="TITLE" length="100" not-null="true" />
        </property>
        <property name="detaildesc" type="java.lang.String">
            <column name="DETAILDESC" length="300" />
        </property>
        <property name="ansewercount" type="java.lang.Integer">
            <column name="ANSEWERCOUNT" precision="10" scale="0" not-null="true" />
        </property>
        <property name="lastmodidfied" type="java.util.Date">
            <column name="LASTMODIDFIED" length="7" />
        </property>
        <set name="answerses" inverse="true">
            <key>
                <column name="QID" precision="10" scale="0" not-null="true" />
            </key>
            <one-to-many class="cn.entity.Answers" />
        </set>
    </class>
</hibernate-mapping>

 

Answers 实体类及映射文件

package cn.entity;
import java.util.Date;
/**
 * 应答表
 * */
@SuppressWarnings("serial")
public class Answers implements java.io.Serializable {
	private Integer id;//答案 id
	private Duestions duestions;//一对多映射
	private String anscontent;//答案内容
	private Date ansdate;//回答时间
	@Override
	public String toString() {
		return "Answers [id=" + id + ", anscontent=" + anscontent + ", ansdate=" + ansdate + "]";
	}
	public Answers() {
	}
	public Answers(Duestions duestions, String anscontent) {
		this.duestions = duestions;
		this.anscontent = anscontent;
	}
	public Answers(Duestions duestions, String anscontent, Date ansdate) {
		this.duestions = duestions;
		this.anscontent = anscontent;
		this.ansdate = ansdate;
	}
	public Integer getId() {
		return this.id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Duestions getDuestions() {
		return this.duestions;
	}
	public void setDuestions(Duestions duestions) {
		this.duestions = duestions;
	}
	public String getAnscontent() {
		return this.anscontent;
	}
	public void setAnscontent(String anscontent) {
		this.anscontent = anscontent;
	}
	public Date getAnsdate() {
		return this.ansdate;
	}
	public void setAnsdate(Date ansdate) {
		this.ansdate = ansdate;
	}
}

 

<?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.Answers" table="ANSWERS" schema="ONLINEDB">
        <id name="id" type="java.lang.Integer">
            <column name="ID" precision="10" scale="0" />
            <generator class="sequence">
            	<param name="sequence">seq_answers</param>
            </generator>
        </id>
        <many-to-one name="duestions" class="cn.entity.Duestions" fetch="select">
            <column name="QID" precision="10" scale="0" not-null="true" />
        </many-to-one>
        <property name="anscontent" type="java.lang.String">
            <column name="ANSCONTENT" length="300" not-null="true" />
        </property>
        <property name="ansdate" type="java.util.Date">
            <column name="ANSDATE" length="7" />
        </property>
    </class>
</hibernate-mapping>

 

 DuestionsDao DAO类

package cn.dao;
import java.util.List;
import cn.entity.Duestions;
/**
 * 问题表 DAO
 * */
public interface DuestionsDao {
	/**
	 * 查询问题表列表
	 * @return
	 */
	List<Duestions> findAll();
	/**
	 * 根据 id 查询问题表的一条数据
	 * @param id
	 * @return
	 */
	Duestions findById(Integer id);
	/**
	 * 添加问题表数据
	 * @param duestions
	 */
	void add(Duestions duestions);
	/**
	 * 修改问题表数据
	 * @param duestions
	 */
	void update(Duestions duestions);
}

 

package cn.dao.impl;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.dao.DuestionsDao;
import cn.entity.Duestions;
public class DuestionsDaoImpl extends HibernateDaoSupport implements
		DuestionsDao {
	@SuppressWarnings("unchecked")
	@Override
	public List<Duestions> findAll() {
		return super.getHibernateTemplate().find("from Duestions");
	}
	@Override
	public Duestions findById(Integer id) {
		return (Duestions) super.getHibernateTemplate().get(Duestions.class,id);
	}
	@Override
	public void add(Duestions duestions) {
		super.getHibernateTemplate().save(duestions);
	}
	@Override
	public void update(Duestions duestions) {
		super.getHibernateTemplate().update(duestions); 
	}
}

 

  AnswersDao DAO类

package cn.dao;
import java.util.List;
import cn.entity.Answers;
/**
 * 应答表 DAO
 * */
public interface AnswersDao {
	/**
	 * 插入应答表数据
	 * @param answers
	 */
	void insert(Answers answers);
	/**
	 * 根据外键查询应答表列表
	 * @param qid
	 * @return
	 */
	List<Answers> findAnswersList(Integer qid);
}

 

package cn.dao.impl;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.dao.AnswersDao;
import cn.entity.Answers;
public class AnswersDaoImpl extends HibernateDaoSupport implements AnswersDao {
	@Override
	public void insert(Answers answers) {
		super.getHibernateTemplate().save(answers);
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Answers> findAnswersList(Integer qid) {
		return  (List<Answers>) super.getHibernateTemplate().find("from Answers a where a.duestions.id ="+qid);
	}
}

 

 DuestionsBiz 业务类

package cn.biz;
import java.util.List;
import cn.entity.Answers;
import cn.entity.Duestions;
/**
 * 问题表 业务类
 * */
public interface DuestionsBiz {
	/**
	 * 查询问题表列表
	 * @return
	 */
	List<Duestions> findAll();
	/**
	 * 根据 id 查询问题表的一条数据
	 * @param id
	 * @return
	 */
	Duestions findById(Integer id);
	/**
	 * 插入应答表数据
	 * @param answers
	 */
	void insert(Answers answers);
	/**
	 * 添加问题表数据
	 * @param duestions
	 */
	void add(Duestions duestions);
	/**
	 * 根据外键查询应答表列表
	 * @param qid
	 * @return
	 */
	List<Answers> findAnswersList(Integer qid);
	/**
	 * 修改问题表数据
	 * @param duestions
	 */
	void update(Duestions duestions);
}

 

package cn.biz.impl;
import java.util.List;
import cn.biz.DuestionsBiz;
import cn.dao.AnswersDao;
import cn.dao.DuestionsDao;
import cn.entity.Answers;
import cn.entity.Duestions;
public class DuestionsBizImpl implements DuestionsBiz {
	private AnswersDao answersDao;
	private DuestionsDao duestionsDao;
	public void setAnswersDao(AnswersDao answersDao) {
		this.answersDao = answersDao;
	}
	public void setDuestionsDao(DuestionsDao duestionsDao) {
		this.duestionsDao = duestionsDao;
	}
	@Override
	public List<Duestions> findAll() {
		return duestionsDao.findAll();
	}
	@Override
	public Duestions findById(Integer id) {
		return duestionsDao.findById(id);
	}
	@Override
	public void insert(Answers answers) {
		answersDao.insert(answers);
	}
	@Override
	public void add(Duestions duestions) {
		duestionsDao.add(duestions);
	}
	@Override
	public List<Answers> findAnswersList(Integer qid) {
		return answersDao.findAnswersList(qid);
	}
	@Override
	public void update(Duestions duestions) {
		duestionsDao.update(duestions);
	}
}

 

 DuestionsTest 测试类

package cn.test;
import java.util.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.DuestionsBiz;
import cn.entity.Answers;
import cn.entity.Duestions;
public class DuestionsTest {
	DuestionsBiz duestionsBiz;
	static ApplicationContext ctx;
	@BeforeClass  
	public static void init(){
		ctx=new	ClassPathXmlApplicationContext("applicationContext.xml");
	}
	@Before 
	public void setUp(){
		duestionsBiz=(DuestionsBiz) ctx.getBean("duestionsBiz");
	}
	@After
	public void tearDown(){
		duestionsBiz=null;
	}
	@AfterClass
	public static void destory(){
		ctx=null;
	}
	//@Test
	public void testFindAll(){
		List<Duestions> dList = duestionsBiz.findAll();
		for (Duestions duestions : dList) {
			System.out.println(duestions);
		}
	}
	@Test
	public void testFindById(){
		Duestions duestions = duestionsBiz.findById(2);
		System.out.println(duestions);
	}
	//@Test
	public void testInsert(){
		Duestions duestions = duestionsBiz.findById(2);
		Answers answers = new Answers(duestions,"这是回答XX",  new Date());
		duestionsBiz.insert(answers);
	}
	//@Test
	public void testFindAnswersList(){
		List<Answers> answers = duestionsBiz.findAnswersList(2);
		for (Answers answer : answers) {
			System.out.println(answer);
		}
	}
	//@Test
	public void testAdd(){
		Duestions duestions = new Duestions("这是title", 1);
		duestionsBiz.add(duestions);
	}
	//@Test
	public void testUpdate(){
		List<Answers> answers = duestionsBiz.findAnswersList(2);
		Duestions duestions = new Duestions();
		duestions.setId(2);
		duestions.setTitle("工");
		duestions.setAnsewercount(answers.size());
		duestionsBiz.update(duestions);
	}
	
}

 

 

 

分享到:
评论

相关推荐

    ssh2 + dao泛型

    在IT行业中,SSH2(Secure Shell 2)通常指的是用于远程访问和安全通信的协议,而DAO(Data Access Object)模式则是一种常见的软件设计模式,主要用于数据库操作的抽象。这篇博客"ssh2 + dao泛型"结合了这两个概念...

    SSH2+AJAX+ORACLE10G项目

    - `src/main/java`:Java源代码,包括实体类(Entity)、DAO(Data Access Object)、Service、Action等。 - `src/main/resources`:配置文件,如Spring的`beans.xml`、Struts2的`struts.xml`、Hibernate的`...

    JDBCTemplate+JavaPOJO实现通用DAO

    return jdbcTemplate.update(sql, new Object[]{entity.getColumn1(), entity.getColumn2()}); } ``` 5. **事务管理**:由于数据库操作往往涉及到事务,因此需要在Service层进行事务控制。Spring提供声明式事务...

    asp.net+wcf+entity framework

    ASP.NET、WCF(Windows Communication Foundation)和Entity Framework是微软.NET框架下的三个核心技术,用于构建分布式企业级应用程序。在本文中,我们将深入探讨这三个组件以及它们如何协同工作。 **ASP.NET** ...

    asp.net+spring.net+entity framework 的一个简单例子

    最后,`Entity Framework`(EF)是微软提供的一款ORM(Object-Relational Mapping)工具,它使得开发者可以使用.NET语言直接操作数据库,而无需编写大量的SQL语句。EF通过映射对象到数据库表,减少了数据访问层的...

    springboot+mybatis逆向生成controller+service+mapper+entity

    这个主题"springboot+mybatis逆向生成controller+service+mapper+entity"是关于如何利用工具自动化创建CRUD(Create、Read、Update、Delete)操作的相关代码,包括Controller、Service、Mapper以及Entity类。...

    ssh2+注解OA系统

    SSH2+注解OA系统是一种基于Spring、Struts2和Hibernate三大开源框架构建的企业级办公自动化(Office Automation,简称OA)系统。这个系统的开发利用了SSH2(Spring、Struts2、Hibernate)的集成优势,提供了高效、...

    微信+MVC+EntityFramework

    微信+MVC+EntityFramework Demo

    ExtJS4.2 MVC + VS2012 MVC + EntityFramework 搭建系統框架

    修改webconfig的sql密碼後 遠行項目 隨便輸入什麼點登錄 會自動生成數據庫 生成數據庫之後執行下面的語句 添加種子數據 --數據庫種子數據 use EXTMVC insert [dbo].[users] values('tomoto','tomoto',1,'2014-05-06 ...

    WCF+Silverlight+EntityFramework+Sqlite所做的学生管理系统

    WCF+Silverlight+EntityFramework+Sqlite所做的学生信息管理系统,自己闲暇时间做着玩的,页面什么的比较丑,不过不要在意那些细节……数据库采用Sqlite,非常小的一个数据库,我就不提供了,网上一大堆,表结构什么...

    我的第一个ASP.NET MVC(WebApi) + Entity FrameWork+ Unity+Redis+Sq

    我的第一个ASP.NET MVC(WebApi) + Entity FrameWork+ Unity+Redis+Sql Server-Other架构的web开发架构设计_EFMvcFrameApp

    ssh2+oracle 全注解 annotation lib包

    SSH2 (Secure Shell 2) 是一种网络协议,用于安全地远程访问和管理服务器,而Oracle则是一款广泛应用的关系型数据库管理系统。在这个特定的上下文中,"ssh2+oracle 全注解 annotation lib包" 指的是一个包含了支持...

    CMS(MVC+ Entity FramWork)

    它允许开发者使用面向对象的方式来操作数据库,无需关注底层SQL语句,大大简化了数据访问层的开发工作。EF的核心概念包括实体(Entity)、上下文(DbContext)、数据模型(Data Model)和映射(Mapping)。 在CMS...

    NHinbernate+LINQ+Entity Framework 课程设计 (毕设)(1)

    NHinbernate+LINQ+Entity Framework完成的课程设计平台,分为website和project两个部分,project主要是nhibernate,linq和ef在website的app_Code中。 数据库使用sql 2008.

    bootstrap+asp.net core+entity framework学习之范例

    Entity Framework (EF)是.NET框架下的一个ORM工具,它允许开发者使用面向对象的方式来操作数据库,而无需关注底层SQL语句。EF Core是其轻量级、跨平台的版本,与ASP.NET Core完美配合。在这个学习过程中,你将学习到...

    NHinbernate+LINQ+Entity Framework 课程设计 (毕设)(2)

    NHinbernate+LINQ+Entity Framework完成的课程设计平台,分为website和project两个部分,project主要是nhibernate,linq和ef在website的app_Code中。 数据库使用sql 2008 该部分是nhibernate

    ASP.NET MVC+EntityFramework项目实例

    ASP.NET MVC+EntityFramework项目实例,功能 页面登录,分页,用户添加,编辑以及关键词查询等

    .Net系列框架-Dapper+EntityFrameworkCore+Autofac+WebApi+Web

    自己搭建的 .Net系列框架-Dapper+EntityFrameworkCore+Autofac+WebApi+Web 详细介绍见:https://blog.csdn.net/zhangjiankun880/article/details/106540475

    WPF+EntityFramework6Demo

    **标题:“WPF+EntityFramework6Demo”** 这个标题表明我们正在探讨一个使用Windows Presentation Foundation (WPF) 和 Entity Framework 6 (EF6) 的演示项目。WPF 是微软提供的一个用于构建桌面应用程序的框架,它...

    WPF+Entity+Database first+自定义连接字符串

    WPF+Entity Frame的Database first模式,使用用户指定的连接字符串的方法,我也曾在网上找了很久,基本没看到有明确说明的,也没有想过示例。 终于结合相关人的描述让我找到了修改的地方。压缩包内为原版工程代码,...

Global site tag (gtag.js) - Google Analytics