📄 questiondaoimp.java
字号:
package dao.hibernate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import po.Oexampaper;
import po.Oquestion;
import dao.IQuestionDao;
public class QuestionDaoImp implements IQuestionDao
{
private SessionFactory sf;
private HibernateTemplate template;
public SessionFactory getSf()
{
return sf;
}
public void setSf(SessionFactory sf)
{
this.sf = sf;
template = new HibernateTemplate(sf);
}
public void addQuestion(Oquestion que)
{
this.transerQuestion(que);
template.save(que);
sf.close();
}
public void delQuestion(String id)
{
template.delete(this.findByid(id));
sf.close();
}
public List findAllQuestion()
{
List list = template.find("from Oquestion");
sf.close();
return list;
}
public Oquestion findByid(String id)
{
Oquestion que = (Oquestion)template.get(Oquestion.class, Long.parseLong(id));
sf.close();
return que;
}
public Collection findByQuestionkey(int currow, int pagesize,String text,String type,String style)
{
Collection TestList = null;
Query q = null;
SessionFactory sf = template.getSessionFactory();
Session s = sf.openSession();
String hql = "";
//没有条件
if ((null == text && null == type && null == style)|| ("".equals(text) && "null".equals(type) && "null".equals(style))||
("".equals(text) && "所有类型".equals(type) && "所有形式".equals(style)))
{
hql = "from Oquestion o order by o.id asc";
q = s.createQuery(hql);
}
//1个条件
else if(null!=text && "所有类型".equals(type) && "所有形式".equals(style))
{
hql = "from Oquestion o where o.name like :name";
q = s.createQuery(hql);
q.setString("name", "%" + text + "%");
}
else if("".equals(text) && ("所有类型".equals(type)==false) && "所有形式".equals(style))
{
hql = "from Oquestion o where o.type=?";
q = s.createQuery(hql);
q.setString(0,type);
}
else if("".equals(text) && "所有类型".equals(type) && ("所有形式".equals(style))==false)
{
hql = "from Oquestion o where o.style=?";
q = s.createQuery(hql);
q.setString(0,style);
}
//2个条件
else if(null!=text && ("所有类型".equals(type)==false) && "所有形式".equals(style))
{
hql = "from Oquestion o where o.type=? and o.name like :name";
q = s.createQuery(hql);
q.setString(0,type);
q.setString("name", "%" + text + "%");
}
else if(null!=text && "所有类型".equals(type) && ("所有形式".equals(style))==false)
{
hql = "from Oquestion o where o.style=? and o.name like :name";
q = s.createQuery(hql);
q.setString(0,style);
q.setString("name", "%" + text + "%");
}
else if("".equals(text)&& ("所有类型".equals(type)==false) && ("所有形式".equals(style))==false)
{
hql = "from Oquestion o where o.style=? and o.type=?";
q = s.createQuery(hql);
q.setString(0,style);
q.setString(1,type);
}
//3个条件
else if(null!=text && ("所有类型".equals(type)==false) && ("所有形式".equals(style))==false)
{
hql = "from Oquestion o where o.style=? and o.type=? and o.name like :name";
q = s.createQuery(hql);
q.setString(0,style);
q.setString(1,type);
q.setString("name", "%" + text + "%");
}
else
{
}
q.setFirstResult(currow);
q.setMaxResults(pagesize);
TestList = q.list();
s.close();
return TestList;
}
public int findByQuestionkey(String text,String type,String style)
{
Query q = null;
SessionFactory sf = template.getSessionFactory();
Session s = sf.openSession();
String hql = "";
int count = 0;
//没有条件
if ((null == text && null == type && null == style)|| ("".equals(text) && "null".equals(type) && "null".equals(style))||
("".equals(text) && "所有类型".equals(type) && "所有形式".equals(style)))
{
hql = "from Oquestion o order by o.id asc";
q = s.createQuery(hql);
System.out.println("111");
}
//1个条件
else if(null!=text && "所有类型".equals(type) && "所有形式".equals(style))
{
hql = "from Oquestion o where o.name like :name";
q = s.createQuery(hql);
q.setString("name", "%" + text + "%");
System.out.println("222");
}
else if("".equals(text) && ("所有类型".equals(type)==false) && "所有形式".equals(style))
{
hql = "from Oquestion o where o.type=?";
q = s.createQuery(hql);
q.setString(0,type);
System.out.println("333");
}
else if("".equals(text) && "所有类型".equals(type) && ("所有形式".equals(style))==false)
{
hql = "from Oquestion o where o.style=?";
q = s.createQuery(hql);
q.setString(0,style);
System.out.println("444");
}
//2个条件
else if(null!=text && ("所有类型".equals(type)==false) && "所有形式".equals(style))
{
hql = "from Oquestion o where o.type=? and o.name like :name";
q = s.createQuery(hql);
q.setString(0,type);
q.setString("name", "%" + text + "%");
System.out.println("555");
}
else if(null!=text && "所有类型".equals(type) && ("所有形式".equals(style))==false)
{
hql = "from Oquestion o where o.style=? and o.name like :name";
q = s.createQuery(hql);
q.setString(0,style);
q.setString("name", "%" + text + "%");
System.out.println("666");
}
else if("".equals(text)&& ("所有类型".equals(type)==false) && ("所有形式".equals(style))==false)
{
hql = "from Oquestion o where o.style=? and o.type=?";
q = s.createQuery(hql);
q.setString(0,style);
q.setString(1,type);
System.out.println("777");
}
//3个条件
else if(null!=text && ("所有类型".equals(type)==false) && ("所有形式".equals(style))==false)
{
hql = "from Oquestion o where o.style=? and o.type=? and o.name like :name";
q = s.createQuery(hql);
q.setString(0,style);
q.setString(1,type);
q.setString("name", "%" + text + "%");
}
else
{
}
count = q.list().size();
s.close();
return count;
}
public void updataQuestion(Oquestion que)
{
this.transerQuestion(que);
template.saveOrUpdate(que);
sf.close();
}
public void transerQuestion(Oquestion que)
{
String style = que.getStyle();
if(style.equals("a"))
{
que.setStyle("判断");
}
if(style.equals("b"))
{
que.setStyle("单项选择");
}
if(style.equals("c"))
{
que.setStyle("多项选择");
}
}
public List findQuestionbyTy(String type)
{
SessionFactory sf = template.getSessionFactory();
Session s = sf.openSession();
String hql = "select o from Oquestion o where o.style=? and o.type=?";
Query q = s.createQuery(hql);
q.setString(0,"判断");
q.setString(1,type);
List list = q.list();
s.close();
return list;
}
public List findQuestionbyTySt(String type,String style)
{
SessionFactory sf = template.getSessionFactory();
Session s = sf.openSession();
String hql = "select o from Oquestion o where o.style=? and o.type=?";
Query q = s.createQuery(hql);
q.setString(0,style);
q.setString(1,type);
List list = q.list();
s.close();
return list;
}
public List DoQuestionLevel(String level, List list,int num)
{
List list_ = new ArrayList();
if("简单".equals(level))
{
if(num>=10)
{
int num_ = num%5;
num = num-num_;
int num_e = (num*3)/5 + num_;
int num_h = (num*1)/5;
int num_s = (num*1)/5;
Iterator iter = list.iterator();
while(iter.hasNext())
{
Oquestion que = (Oquestion)iter.next();
if(num_e>0&&que.getQlevel().equals("简单"))
{
list_.add(que);
num_e--;
}
if(num_s>0&&que.getQlevel().equals("中等"))
{
list_.add(que);
num_s--;
}
if(num_h>0&&que.getQlevel().equals("难"))
{
list_.add(que);
num_h--;
}
}
return list_;
}
else
{
return list;
}
}
if("中等".equals(level))
{
if(num>=10)
{
int num_ = num%5;
num = num-num_;
int num_e = (num*1)/5;
int num_h = (num*1)/5;
int num_s = (num*3)/5 + num_;
Iterator iter = list.iterator();
while(iter.hasNext())
{
Oquestion que = (Oquestion)iter.next();
if(num_e>0&&que.getQlevel().equals("简单"))
{
list_.add(que);
num_e--;
}
if(num_s>0&&que.getQlevel().equals("中等"))
{
list_.add(que);
num_s--;
}
if(num_h>0&&que.getQlevel().equals("难"))
{
list_.add(que);
num_h--;
}
}
return list_;
}
else
{
return list;
}
}
if("难".equals(level))
{
if(num>=10)
{
int num_ = num%5;
num = num-num_;
int num_e = (num*1)/5;
int num_h = (num*3)/5 + num_;
int num_s = (num*1)/5;
Iterator iter = list.iterator();
while(iter.hasNext())
{
Oquestion que = (Oquestion)iter.next();
if(num_e>0&&que.getQlevel().equals("简单"))
{
list_.add(que);
num_e--;
}
if(num_s>0&&que.getQlevel().equals("中等"))
{
list_.add(que);
num_s--;
}
if(num_h>0&&que.getQlevel().equals("难"))
{
list_.add(que);
num_h--;
}
}
return list_;
}
else
{
return list;
}
}
return list_;
}
public List randomQuestion(List list)
{
List list_ = new ArrayList();
int num = list.size();
int index[] = this.random(num);
for(int i=0;i <index.length; i++)
{
Oquestion que = (Oquestion)list.get(index[i]);
list_.add(que);
}
return list_;
}
public int[] random(int num)
{
int filled=0; //已填充数计数器
int card[]=new int[num]; //填充数组
int count; //存放随机数
boolean flag=true;
while(true)
{
flag=true;
count=(int)(Math.random()*num); //取得随机数
card[filled]=count;
for(int i=0;i<filled;i++)
{ //比较循环
if(card[i]==count)
{
filled--; //随机数已存在
flag=false;
break;
}
}
filled++; //计数器加1
if(filled==num)
break;
}
return card;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -