public PageModel listStudent(int pageNo, int pageSize, String clientIdOrName) {
StringBuffer sbSql = new StringBuffer();
sbSql.append("from Student ");
if(clientIdOrName != null && clientIdOrName.trim().length() != 0) {
sbSql.append("where id like '%" + clientIdOrName + "%' or time like '%" + clientIdOrName + "%'");
}
Session session = null;
Transaction tx = null;
PageModel pageModel = null;
List studentes = new ArrayList();
try {
session = HibernateSessionFactory.getSession();
tx = session.beginTransaction();
Query query = session.createQuery(sbSql.toString());
query.setFirstResult((pageNo -1) * pageSize);
query.setMaxResults(pageSize);
List temp = query.list();
for(Iterator ite = temp.iterator(); ite.hasNext();) {
studentes.add((Student)ite.next());
}
pageModel = new PageModel();
pageModel.setPageNo(pageNo);
pageModel.setPageSize(pageSize);
pageModel.setList(studentes);
pageModel.setTotalRecords(getTotalRecords(clientIdOrName,"Student"));
tx.commit();
}catch(Exception e) {
e.printStackTrace();
tx.rollback();
}finally {
HibernateSessionFactory.closeSession();
}
return pageModel;
}
public ActionForward list(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
StudentActionForm saf = (StudentActionForm)form;
PageModel pageModel = ServiceFactory.getInstance()
.getAdminManager()
.listStudent(saf.getPageNo(), saf.getPageSize(), saf.getClientIdOrName());
request.setAttribute("pagemodel", pageModel);
return mapping.findForward("list_success");
}
<action path="/admin/student"
type="edu.struts.actions.StudentAction"
name="studentForm"
scope="request"
parameter="command"
>
public class AdminBaseAction extends DispatchAction {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
if(request.getSession().getAttribute("adminId") == null) {
return mapping.findForward("login");
}
return super.execute(mapping, form, request, response);
}
}
public class LoginAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginActionForm lf = (LoginActionForm)form;
if("student".equals(lf.getType())) {
if(ServiceFactory.getInstance().getStudentManager().login(lf.getId(), lf.getPassword(), lf.getType())) {
request.getSession().setAttribute("studentId", lf.getId());
ActionForward af = new ActionForward("student/main.do",true);
return af;
}
}else if("teacher".equals(lf.getType())) {
if(ServiceFactory.getInstance().getTeacherManager().login(lf.getId(), lf.getPassword(), lf.getType())) {
request.getSession().setAttribute("teacherId", lf.getId());
ActionForward af = new ActionForward("teacher/main.do",true);
return af;
}
}else if("admin".equals(lf.getType())) {
if(ServiceFactory.getInstance().getAdminManager().login(lf.getId(), lf.getPassword(), lf.getType())) {
request.getSession().setAttribute("adminId", lf.getId());
ActionForward af = new ActionForward("admin/main.do",true);
return af;
}
}
return mapping.findForward("login");
}
}
<action path="/login"
type="edu.struts.actions.LoginAction"
scope="request"
name="loginForm"
>
<?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">
<hibernate-mapping>
<class name="edu.model.Student">
<!-- 描述ID生成策略 -->
<id name="id">
<generator class="assigned"/>
</id>
<!-- 普通属性 -->
<property name="name"/>
<property name="password"/>
<!-- 描述Student与Contact的一对一映射 -->
<many-to-one name="contact" column="contact_id" unique="true" not-null="true" cascade="all" lazy="false"/>
<!-- 描述Student与History的一对多映射 -->
<set name="histories" table="history" lazy="true" cascade="all" inverse="true" order-by="id asc">
<key column="sid"/>
<one-to-many class="edu.model.History"/>
</set>
<!-- 描述Student与Classes的多对多映射 -->
<set name="classes" table="student_class_link" lazy="true" inverse="false" cascade="save-update">
<key column="student_id"/>
<many-to-many class="edu.model.Classes" column="class_id"/>
</set>
</class>
</hibernate-mapping>
package edu.util;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class ExportDB {
public static void main(String[] args) {
//读取xml配置文件
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
//根据读取的xml文件导出数据表
export.create(true, true);
}
}
<?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">
<hibernate-mapping>
<class name="edu.model.History">
<id name="id">
<generator class="native"/>
</id>
<property name="mark"/>
<many-to-one name="student" column="sid"/>
<many-to-one name="clazz" column="cid" lazy="false"/>
</class>
</hibernate-mapping>
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>edu.util.CharsetEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Student student = (Student)HibernateSessionFactory.getSession().get(Student.class, id);
Session session = null;
Transaction tx = null;
Student student = null;
try {
//通过HibernateSessionFactory工具类获得Session对象
session = HibernateSessionFactory.getSession();
//开启事务
tx = session.beginTransaction();
//业务逻辑
student = (Student)session.get(Student.class, id);
//提交事务
tx.commit();
}catch(Exception e) {
e.printStackTrace();
//当业务逻辑发生异常是回滚事务
tx.rollback();
}finally {
//最后关闭Session
HibernateSessionFactory.closeSession();
}
return student;
Student student = (Student)HibernateSessionFactory.getSession().get(Student.class,id);
分享到:
相关推荐
bishe跳频通信系统的仿真
bishe (1).zip
用于毕设的实验室考勤管理系统,采用cs架构,服务器采用MongoDB数据库在本地搭建简易数据库,刷卡端采用RFID与stm32F107芯片,学生在进入实验室时打卡,数据库实时记录,并在pc端显示该卡学生的照片,年级,姓名等...
加班信息,包括加班开始时间、结束时间、统计加班总时间)--(学生宿舍管理系统c#winform窗体程序,含有数据库)--(bishe_实验室考勤管理系统(C#语言,MongoDB数据库,刷卡端采用Stm32F10。。。)
bishe_基于VUE_vue.zip
BiShe Project 毕业设计 测井数据采集系统
BiShe Project 毕业设计 测井数据采集系统
毕业设计 BiShe Project 测井数据采集系统.zip
bishe有限冲激响应滤波器的设计说明.doc
bishe_基于VUE_vue_源码.zip
bishe公交车自动报站系统的设计说明.doc
bishe.sql
BiShe
对输入的两个信号进行异或,并对异或后的信号与其中一个原信号进行计数
用户,管理员身份的登入,注册,菜谱的添加,搜索,个人主页的显示已经个人菜谱的详情查看,菜谱的分类等
基于vue+vue-cli+vue-router实现的校园二手交易平台,
仿真频谱利用率为3bit/s/Hz的两根、三根、四根发射天线的误码率以及 运用8psk调制仿真两根天线的误码率