linux怎么查看本机内存大小
271
2022-09-06
使用Struts2+Hibernate开发学生信息管理功能
运行结果:
总共两个表,用户表Users和学生信息表Students。
项目内容:
其中com.scx.action包中包含
所有action动作的父类SuperAction学生信息动作类StudentsAction用户动作类UsersAction
com.scx.entity包中包含
学生实体类Students用户实体类UsersStudents对象关系映射文件Students.hbm.xmlUsers对象关系映射文件Users.hbm.xml
com.scx.service包中包含
学生业务逻辑接口StudentsDao用户业务逻辑接口UsersDao
com.scx.service.impl包中包含
用户业务逻辑接口StudentsDao对应的实现类StudentsDaoImpl用户业务逻辑接口UsersDao对应的实现类UsersDaoImpl
com.scx.util包中包含
HibernateSessionFactory类,一个单例模式返回SessionFactory对象
test文件夹 主要是junit测试内容
代码展示:
Students实体类
package com.scx.entity;import java.util.Date;//学生实体类public class Students { private String sid;//学号 private String sname;//姓名 private String gender;//性别 private Date birthday;//出生日期 private String address;//地址 public Students() { } public Students(String sid, String sname, String gender, Date birthday, String address) { this.sid = sid; this.sname = sname; this.gender = gender; this.birthday = birthday; this.address = address; } public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; }}
package com.scx.entity;public class Users { private int uid;//主键id private String username;//用户名 private String password;//密码 public Users(int uid, String username, String password) { this.uid = uid; this.username = username; this.password = password; } public Users() { } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}
Students.hbm.xml 这里要注意 学生主键的生成方式为assigned
Users.hbm.xml
hibernate.cfg.xml
org.hibernate.dialect.MySQLDialect
用户业务逻辑接口
package com.scx.service;import com.scx.entity.Users;//用户业务逻辑接口public interface UsersDao { //用户登录操作 public boolean usersLogin(Users user); }
package com.scx.service;import java.util.List;import com.scx.entity.Students;//学生业务逻辑接口public interface StudentsDao { //查询所有学生信息 public List
用户业务逻辑接口实现类
package com.scx.service.impl;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.Transaction;import com.scx.entity.Students;import com.scx.entity.Users;import com.scx.service.UsersDao;import com.scx.util.HibernateSessionFactory;public class UsersDaoImpl implements UsersDao { //用户登录操作 @SuppressWarnings("unchecked") public boolean usersLogin(Users user) { Transaction transaction = null; String hql = ""; try { Session session = HibernateSessionFactory.getSessionFactory() .getCurrentSession(); transaction = session.beginTransaction(); hql = "from Users where username = ? and password = ?"; Query query = session.createQuery(hql); query.setParameter(0, user.getUsername()); query.setParameter(1, user.getPassword()); List
在添加学生时 为学生获取一个最新的学号。
package com.scx.service.impl;import java.util.ArrayList;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.Transaction;import com.scx.entity.Students;import com.scx.service.StudentsDao;import com.scx.util.HibernateSessionFactory;public class StudentsDaoImpl implements StudentsDao{ //查询所有的学生信息 @SuppressWarnings("unchecked") public List
所有action的父类SuperAction 类,为了获得常用的内置对象,采用耦合IOC方式注入属性。继承ActionSupport 并实现ServletRequestAware,ServletResponseAware,ServletContextAware 。获取相应的request,response,session,这样的话,每个继承SuperAction 的类都能够使用request,response,session
package com.scx.action;import javax.servlet.ServletContext;import javax.servlet.javax.servlet.javax.servlet.org.apache.struts2.interceptor.ServletRequestAware;import org.apache.struts2.interceptor.ServletResponseAware;import org.apache.struts2.util.ServletContextAware;import com.opensymphony.xwork2.ActionSupport;//所有action动作的父类public class SuperAction extends ActionSupport implements ServletRequestAware, ServletResponseAware, ServletContextAware { /** * */ private static final long serialVersionUID = 1L; protected HttpServletRequest request;// 请求对象 protected HttpServletResponse response;// 响应对象 protected HttpSession session;// 会话对象 protected ServletContext context;//全局对象 public void setServletContext(ServletContext context) { this.context = context; } public void setServletResponse(HttpServletResponse response) { this.response = response; } public void setServletRequest(HttpServletRequest request) { this.request = request; this.session = this.request.getSession(); }}
UsersAction类,继承SuperAction并实现ModelDriven
package com.scx.action;import org.apache.struts2.interceptor.validation.SkipValidation;import com.opensymphony.xwork2.ModelDriven;import com.scx.entity.Users;import com.scx.service.UsersDao;import com.scx.service.impl.UsersDaoImpl;/** * @author scx * 用户动作类 */public class UsersAction extends SuperAction implements ModelDriven
StudentsAction类,继承SuperAction并实现ModelDriven
package com.scx.action;import java.util.List;import com.opensymphony.xwork2.ModelDriven;import com.scx.entity.Students;import com.scx.service.StudentsDao;import com.scx.service.impl.StudentsDaoImpl;/** * @author scx * 学生信息动作类 */public class StudentsAction extends SuperAction implements ModelDriven
struct.xml
struct.xml.Flow
jsp代码就不发了。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~