最近刚学了一个JSF框架,既然学了就用这个东西做一个小项目,做了一个可以选课考试的web程序!这是基于Hibernate4.39和JSF2.2写的!
地址在这:链接: https://pan.baidu.com/s/1bqzNkr9 密码: nkr6
这是一些代码[Java] 纯文本查看 复制代码 package com.examqdu.dao;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.examqdu.entity.Course;
import com.examqdu.entity.Examq;
import com.examqdu.entity.OptionalCou;
import com.examqdu.entity.OptionalCouId;
import com.examqdu.entity.UserAccount;
import com.examqdu.util.HibernateUtils;
/**
* @ClassName ManagerDao
* @Description
* [url=home.php?mod=space&uid=686208]@AuThor[/url] Jackjoily
* [url=home.php?mod=space&uid=686237]@date[/url] 2017年12月19日 下午1:44:46
*/
public class ManagerDao {
private SessionFactory factory;
private Session session;
private Transaction transaction;
/**
*
* @Title: orderByCourseModule @Description: 1)
* 查询按照课程模块排序的课程列表 @param [url=home.php?mod=space&uid=155549]@Return[/url] @return List<Course> @throws
*/
public List<Course> orderByCourseModule() {
List<Course> list = new ArrayList<Course>();
try {
session = HibernateUtils.getSession();
transaction = session.beginTransaction();
Query query = session.createQuery("from Course c order by c.cruModule desc ");
if (null != query.list())
list = query.list();
} catch (Exception e) {
e.printStackTrace();
} finally {
transaction.commit();
session.close();
}
return list;
}
/**
*
* @Title: getCourseByCourseModule @Description: 查询指定模块的所有课程 @param @param
* module @param @return @return List<Course> @throws
*/
public List<Course> getCourseByCourseModule(String module) {
List<Course> list = new ArrayList<Course>();
try {
session = HibernateUtils.getSession();
transaction = session.beginTransaction();
Query query = session.createQuery("from Course where cruModule=?");
query.setString(0, module);
if (null != query.list())
list = query.list();
} catch (Exception e) {
e.printStackTrace();
} finally {
transaction.commit();
session.close();
}
return list;
}
/**
*
* @Title: getCourseByIdOrName @Description: 按照课程编号或名称查询课程 @param @param
* nameOrid @param @return @return Course @throws
*/
public List<Course> getCourseByIdOrNameOrModule(String nameOridOrMoudle) {
List<Course> course =new ArrayList<>();
int i = 0;
char[] chars = nameOridOrMoudle.toCharArray();
for (char c : chars) {
if ('0' < c && c < '9') {
i++;
}
}
try {
session = HibernateUtils.getSession();
transaction = session.beginTransaction();
if (i != 0) {
course = session.createQuery("from Course where cruId=?").setString(0, nameOridOrMoudle).list();
} else {
if(nameOridOrMoudle.contains("开发") ){
course = session.createQuery("from Course where cruModule=?").setString(0, nameOridOrMoudle).list();
}else{
course = session.createQuery("from Course where cruName=?").setString(0, nameOridOrMoudle).list();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
transaction.commit();
session.close();
}
return course;
}
/**
*
* @Title: getOptionalCourseStudent @Description:
* 查询注册了指定课程的学生列表 @param @param curName @param @return @return
* List<UserAccount> @throws
*/
public List<UserAccount> getOptionalCourseStudent(String curName) {
List<UserAccount> list = new ArrayList<UserAccount>();
try {
session = HibernateUtils.getSession();
transaction = session.beginTransaction();
Query query = session.createQuery("select oc.userAccount from OptionalCou oc Where oc.course.cruName=?");
if (null != query.setString(0, curName).list()) {
list = query.setString(0, curName).list();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
transaction.commit();
session.close();
}
return list;
}
/**
*
* @Title: getOptionalCourseStudent @Description:提供从该课程删除一个学生的功能 @param @param
* curName @param @return @return List<UserAccount> @throws
*/
public int deleteStudentInOptionalCourse(String uid, String cruName) {
int i = 0;
try {
session = HibernateUtils.getSession();
transaction = session.beginTransaction();
Query query = session.createQuery("delete from OptionalCou oc where oc.id.OCruName=? and oc.id.OUid=? ");
query.setString(0, cruName);
query.setString(1, uid);
i = query.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
transaction.commit();
session.close();
}
return i;
}
/**
*
* @Title: ScoreorderByCourse @Description: 查询指定课程的考试成绩排名(该方法同时也把查询指定课程成绩
* 列表的功能实现了) @param @param cruName @param @return @return
* List<OptionalCou> @throws
*/
public List<OptionalCou> ScoreorderByCourse(String cruName) {
List<OptionalCou> list = new ArrayList();
try {
session = HibernateUtils.getSession();
transaction = session.beginTransaction();
Query query = session
.createQuery("from OptionalCou o where o.id.OCruName=? order by o.id.OCruScore desc ");
query.setString(0, cruName);
list = query.list();
} catch (Exception e) {
e.printStackTrace();
} finally {
transaction.commit();
session.close();
}
return list;
}
/**
*
* @Title: getScoreByUid @Description: 查询指定学号的学生的考试成绩单 @param @param
* uid @param @return @return List<OptionalCouId> @throws
*/
public List<OptionalCouId> getScoreByUid(String uid) {
List<OptionalCouId> list = new ArrayList();
try {
session = HibernateUtils.getSession();
transaction = session.beginTransaction();
Query query = session.createQuery("from OptionalCou ");
List<OptionalCou> cs = query.list();
for (OptionalCou optionalCou : cs) {
if (uid.equals(optionalCou.getId().getOUid())) {
list.add(optionalCou.getId());
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
transaction.commit();
session.close();
}
return list;
}
public List<UserAccount> getAllUserAccount(){
List<UserAccount> list=new ArrayList<>();
try{
session = HibernateUtils.getSession();
transaction = session.beginTransaction();
Query query = session.createQuery("from UserAccount");
list=query.list();
}catch(Exception e){
e.printStackTrace();
}
finally{
transaction.commit();
session.close();
}
return list;
}
public List<Course> getAllCourseList(){
List<Course> list=new ArrayList<>();
try{
session = HibernateUtils.getSession();
transaction = session.beginTransaction();
Query query = session.createQuery("from Course");
list=query.list();
}catch(Exception e){
e.printStackTrace();
}
finally{
transaction.commit();
session.close();
}
return list;
}
public List<Examq> getExamqByIdOrNameOrModule(String nameOridOrMoudle) {
List<Examq> examq =new ArrayList<>();
try {
session = HibernateUtils.getSession();
transaction = session.beginTransaction();
examq = session.createQuery("from Examq e where e.id.exCruId=?").setString(0, nameOridOrMoudle).list();
} catch (Exception e) {
e.printStackTrace();
} finally {
transaction.commit();
session.close();
}
return examq;
}
public static void main(String[] args) {
ManagerDao dao=new ManagerDao();
List<Course> list= dao.getCourseByIdOrNameOrModule("数据库开发");
System.out.println(list.size());
}
}
|