SSH sql+entity+dao+biz+test
程序员文章站
2022-06-09 17:11:05
...
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); } }
上一篇: winform读写txt文件
下一篇: java方法重载和参数类型自动提升方式