bishe

	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);

你可能感兴趣的:(java,Hibernate,xml,.net)