?? questiondaoimp.java
字號(hào):
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個(gè)條件
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個(gè)條件
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個(gè)條件
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個(gè)條件
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個(gè)條件
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個(gè)條件
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("單項(xiàng)選擇");
}
if(style.equals("c"))
{
que.setStyle("多項(xiàng)選擇");
}
}
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("簡(jiǎn)單".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("簡(jiǎn)單"))
{
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("簡(jiǎn)單"))
{
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("簡(jiǎn)單"))
{
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; //已填充數(shù)計(jì)數(shù)器
int card[]=new int[num]; //填充數(shù)組
int count; //存放隨機(jī)數(shù)
boolean flag=true;
while(true)
{
flag=true;
count=(int)(Math.random()*num); //取得隨機(jī)數(shù)
card[filled]=count;
for(int i=0;i<filled;i++)
{ //比較循環(huán)
if(card[i]==count)
{
filled--; //隨機(jī)數(shù)已存在
flag=false;
break;
}
}
filled++; //計(jì)數(shù)器加1
if(filled==num)
break;
}
return card;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -