s2sh整合案例-学生信息管理系统(二)_修罗debug的博客-程序员宅基地

技术标签: s2sh  s2sh整合案例  《Java Web/SSM/SSH实战》  学生信息管理系统  csdn博客  

s2sh整合案例-学生信息管理系统(二)

    继续我们的s2sh整合案例-学生信息管理系统,上一系列我们讲了项目的整体搭建需要的Jar包以及web.xml和spring、hibernate以及数据库的相关配置。这一讲主要用于介绍后端server层(包括dao和service层)的所有源代码。

    下图是后端的包目录结构:

    首先是model层的pageBean,Grade,Student,User

 

package com.steadyjack.server.model;

//分页model
public class PageBean {

	private int page; // 第几页
	private int rows; // 每页记录数
	private int start;  // 起始页
	
	
	public PageBean(int page, int rows) {
		super();
		this.page = page;
		this.rows = rows;
	}
	
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public int getStart() {
		return (page-1)*rows;
	}
}

 

package com.steadyjack.server.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

//班级model
@Entity
@Table(name="tb_grade")
public class Grade {

	private int id;
	private String gradeName;
	private String gradeDesc;
	
	public Grade() {
		super();
	}
	
	public Grade(String gradeName, String gradeDesc) {
		super();
		this.gradeName = gradeName;
		this.gradeDesc = gradeDesc;
	}

	@Id
	@GenericGenerator(name="generator",strategy="increment")
	@GeneratedValue(generator="generator")
	@Column(name="id")
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	@Column(name="gradeName")
	public String getGradeName() {
		return gradeName;
	}
	public void setGradeName(String gradeName) {
		this.gradeName = gradeName;
	}
	
	@Column(name="gradeDesc")
	public String getGradeDesc() {
		return gradeDesc;
	}
	public void setGradeDesc(String gradeDesc) {
		this.gradeDesc = gradeDesc;
	}
}

 

package com.steadyjack.server.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.GenericGenerator;

//学生model
@Entity
@Table(name="tb_student")
public class Student {

	private int stuId;
	private String stuNo;
	private String stuName;
	private String sex;
	private Date birthday;
	private int gradeId=-1;
	private String email;
	private String stuDesc;
	
	private String gradeName;
	
	public Student() {
		super();
	}
	
	public Student(String stuNo, String stuName, String sex, Date birthday,
			int gradeId, String email, String stuDesc) {
		super();
		this.stuNo = stuNo;
		this.stuName = stuName;
		this.sex = sex;
		this.birthday = birthday;
		this.gradeId = gradeId;
		this.email = email;
		this.stuDesc = stuDesc;
	}

	@Id
	@GenericGenerator(name="generator",strategy="increment")
	@GeneratedValue(generator="generator")
	@Column(name="stuId")
	public int getStuId() {
		return stuId;
	}
	public void setStuId(int stuId) {
		this.stuId = stuId;
	}
	
	@Column(name="stuNo")
	public String getStuNo() {
		return stuNo;
	}
	public void setStuNo(String stuNo) {
		this.stuNo = stuNo;
	}
	
	@Column(name="stuName")
	public String getStuName() {
		return stuName;
	}
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	
	@Column(name="sex")
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
	@Column(name="birthday")
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
	@Column(name="gradeId")
	public int getGradeId() {
		return gradeId;
	}
	public void setGradeId(int gradeId) {
		this.gradeId = gradeId;
	}
	
	@Column(name="email")
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
	@Column(name="stuDesc")
	public String getStuDesc() {
		return stuDesc;
	}
	public void setStuDesc(String stuDesc) {
		this.stuDesc = stuDesc;
	}
	
	@Transient
	public String getGradeName() {
		return gradeName;
	}
	public void setGradeName(String gradeName) {
		this.gradeName = gradeName;
	}
}

 

package com.steadyjack.server.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

//用户model
@Entity
@Table(name="tb_user")
public class User {

	private int id;
	private String userName;
	private String password;
	private Integer type; //管理员的身份类型:1,代表系统管理员  2,代表班主任  3,学生
	
	public User() {
		super();
	}

	public User(String userName, String password) {
		super();
		this.userName = userName;
		this.password = password;
	}
	
	@Id
	@GenericGenerator(name="generator",strategy="increment")
	@GeneratedValue(generator="generator")
	@Column(name="id")
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	@Column(name="userName")
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	@Column(name="password")
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	@Column(name="type")
	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}
	
}


     接下来是dao层

 

 

package com.steadyjack.server.dao;

import com.steadyjack.server.model.User;
//UserDao
public interface UserDao {
	public User login(User user) throws Exception;
}

 

package com.steadyjack.server.dao;

import java.util.List;

import com.steadyjack.server.model.Grade;
import com.steadyjack.server.model.PageBean;

//GradeDao
public interface GradeDao {

	public List<Grade> gradeList(PageBean pageBean,Grade grade)throws Exception;
	
	public long gradeCount(Grade grade)throws Exception;
	
	public int gradeDelete(String delIds)throws Exception;
	
	public int gradeSave(Grade grade)throws Exception;
	
	public Grade getGradeById(int gradeId)throws Exception;
}

 

package com.steadyjack.server.dao;

import java.util.List;

import com.steadyjack.server.model.PageBean;
import com.steadyjack.server.model.Student;

//StudentDao
public interface StudentDao {

	public List<Student> studentList(PageBean pageBean,Student student,String bbirthday,String ebirthday)throws Exception;
	
	public long studentCount(Student student,String bbirthday,String ebirthday)throws Exception;
	
	public int studentDelete(String delIds)throws Exception;
	
	public int studentSave(Student student)throws Exception;
	
	public boolean getStudentByGradeId(String gradeId)throws Exception;
}

 

package com.steadyjack.server.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 提供会话SessionFactory工厂 获取CurrentSession
 * @author zhonglinsen
 *
 */
public class BaseDao {

	private SessionFactory sessionFactory;
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public Session getSession(){
		return sessionFactory.getCurrentSession();
	}
}


    下面是dao层的impl:

 

 

package com.steadyjack.server.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.steadyjack.server.dao.BaseDao;
import com.steadyjack.server.dao.UserDao;
import com.steadyjack.server.model.User;

@Repository
public class UserDaoImpl extends BaseDao implements UserDao{
	
	@Override
	public User login(User user) throws Exception {
		Session session=this.getSession();
		Query query=session.createQuery("from User u where u.userName=? and u.password=?");
		query.setString(0, user.getUserName());
		query.setString(1, user.getPassword());
		@SuppressWarnings("unchecked")
		List<User> userList=(List<User>)query.list();
		User resultUser=null;
		if(userList.size()>0){
			resultUser=userList.get(0);
		}
		return resultUser;
	}

}

 

package com.steadyjack.server.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.steadyjack.server.dao.BaseDao;
import com.steadyjack.server.dao.GradeDao;
import com.steadyjack.server.model.Grade;
import com.steadyjack.server.model.PageBean;
import com.steadyjack.server.util.StringUtil;

@Repository
public class GradeDaoImpl extends BaseDao implements GradeDao{
	
	@SuppressWarnings("unchecked")
	public List<Grade> gradeList(PageBean pageBean,Grade grade)throws Exception{
		List<Grade> gradeList=null;
		Session session=this.getSession();
		StringBuffer sb=new StringBuffer("from Grade g");
		if(grade!=null && StringUtil.isNotEmpty(grade.getGradeName())){
			sb.append(" and g.gradeName like '%"+grade.getGradeName()+"%'");
		}
		Query query=session.createQuery(sb.toString().replaceFirst("and", "where"));
		if(pageBean!=null){
			query.setFirstResult(pageBean.getStart());
			query.setMaxResults(pageBean.getRows());
		}
		gradeList=(List<Grade>)query.list();
		return gradeList;
	}
	
	public long gradeCount(Grade grade)throws Exception{
		List<Grade> gradeList=gradeList(null, grade);
		if (gradeList!=null) {
			return gradeList.size();
		}else{
			return 0L;
		}
	}
	
	/**
	 * delete from tableName where field in (1,3,5)
	 * @param con
	 * @param delIds
	 * @return
	 * @throws Exception
	 */
	public int gradeDelete(String delIds)throws Exception{
		Session session=this.getSession();
		Query query=session.createSQLQuery("delete from tb_grade where id in("+delIds+")");
		int count=query.executeUpdate();
		return count;
	}
	
	public int gradeSave(Grade grade)throws Exception{
		Session session=this.getSession();
		session.merge(grade);
		return 1;
	}
	
	public Grade getGradeById(int gradeId)throws Exception{
		Session session=this.getSession();
		Grade grade=(Grade) session.get(Grade.class, gradeId);
		return grade;
	}

}

 

package com.steadyjack.server.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.steadyjack.server.dao.BaseDao;
import com.steadyjack.server.dao.StudentDao;
import com.steadyjack.server.model.PageBean;
import com.steadyjack.server.model.Student;
import com.steadyjack.server.util.StringUtil;

@Repository
public class StudentDaoImpl extends BaseDao implements StudentDao{
	
	@SuppressWarnings("unchecked")
	public List<Student> studentList(PageBean pageBean,Student student,String bbirthday,String ebirthday)throws Exception{
		List<Student> studentList=null;
		StringBuffer sb=new StringBuffer("from Student s");
		if(StringUtil.isNotEmpty(student.getStuNo())){
			sb.append(" and s.stuNo like '%"+student.getStuNo()+"%'");
		}
		if(StringUtil.isNotEmpty(student.getStuName())){
			sb.append(" and s.stuName like '%"+student.getStuName()+"%'");
		}
		if(StringUtil.isNotEmpty(student.getSex())){
			sb.append(" and s.sex ='"+student.getSex()+"'");
		}
		if(student.getGradeId()!=-1){
			sb.append(" and s.gradeId ='"+student.getGradeId()+"'");
		}
		if(StringUtil.isNotEmpty(bbirthday)){
			sb.append(" and TO_DAYS(s.birthday)>=TO_DAYS('"+bbirthday+"')");
		}
		if(StringUtil.isNotEmpty(ebirthday)){
			sb.append(" and TO_DAYS(s.birthday)<=TO_DAYS('"+ebirthday+"')");
		}
		Session session=this.getSession();
		Query query=session.createQuery(sb.toString().replaceFirst("and", "where"));
		if(pageBean!=null){
			query.setFirstResult(pageBean.getStart());
			query.setMaxResults(pageBean.getRows());
		}
		studentList=(List<Student>)query.list();
		return studentList;
	}
	
	public long studentCount(Student student,String bbirthday,String ebirthday)throws Exception{
		List<Student> studentList=studentList(null, student, bbirthday, ebirthday);
		if (studentList!=null) {
			return studentList.size();
		}else{
			return 0L;
		}
		
	}
	
	public int studentDelete(String delIds)throws Exception{
		Query query=getSession().createQuery("delete from Student where stuId in("+delIds+")");
		return query.executeUpdate();
	}
	
	public int studentSave(Student student)throws Exception{
		Session session=this.getSession();
		session.merge(student);
		return 1;
	}
	
	
	public boolean getStudentByGradeId(String gradeId)throws Exception{
		Query query=getSession().createQuery("from Student where gradeId=? ");
		query.setParameter(0, Integer.parseInt(gradeId));
		@SuppressWarnings("unchecked")
		List<Student> students=query.list();
		if (students!=null && students.size()>0) {
			return true;
		}else{
			return false;
		}
	}
	
}


    下面是service层:

 

 

package com.steadyjack.server.service;

import com.steadyjack.server.model.User;

public interface UserService {

	public User login(User user) throws Exception;
}

 

package com.steadyjack.server.service;

import java.util.List;



import com.steadyjack.server.model.Grade;
import com.steadyjack.server.model.PageBean;

public interface GradeService {

	public List<Grade> gradeList(PageBean pageBean,Grade grade)throws Exception;
	
	public long gradeCount(Grade grade)throws Exception;
	
	public int gradeDelete(String delIds)throws Exception;
	
	public int gradeSave(Grade grade)throws Exception;
	
	public Grade getGradeById(int gradeId)throws Exception;
}

 

package com.steadyjack.server.service;

import java.util.List;

import com.steadyjack.server.model.PageBean;
import com.steadyjack.server.model.Student;

public interface StudentService {

	public List<Student> studentList(PageBean pageBean,Student student,String bbirthday,String ebirthday)throws Exception;
	
	public long studentCount(Student student,String bbirthday,String ebirthday)throws Exception;
	
	public int studentDelete(String delIds)throws Exception;
	
	public int studentSave(Student student)throws Exception;
	
	public boolean getStudentByGradeId(String gradeId)throws Exception;
}


    下面是service层的impl:

 

 

package com.steadyjack.server.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.steadyjack.server.dao.UserDao;
import com.steadyjack.server.model.User;
import com.steadyjack.server.service.UserService;

@Service
public class UserServiceImpl implements UserService{
	
	@Resource
	private UserDao userDao;
	
	@Override
	public User login(User user) throws Exception {
		return userDao.login(user);
	}

}

 

package com.steadyjack.server.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.steadyjack.server.dao.GradeDao;
import com.steadyjack.server.model.Grade;
import com.steadyjack.server.model.PageBean;
import com.steadyjack.server.service.GradeService;

@Service
public class GradeServiceImpl implements GradeService{

	@Resource
	private GradeDao gradeDao;
	
	@Override
	public List<Grade> gradeList(PageBean pageBean, Grade grade)
			throws Exception {
		return gradeDao.gradeList(pageBean, grade);
	}

	@Override
	public long gradeCount(Grade grade) throws Exception {
		return gradeDao.gradeCount(grade);
	}

	@Override
	public int gradeDelete(String delIds) throws Exception {
		return gradeDao.gradeDelete(delIds);
	}

	@Override
	public int gradeSave(Grade grade) throws Exception {
		return gradeDao.gradeSave(grade);
	}

	@Override
	public Grade getGradeById(int gradeId) throws Exception {
		return gradeDao.getGradeById(gradeId);
	}

}

 

package com.steadyjack.server.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.steadyjack.server.dao.StudentDao;
import com.steadyjack.server.model.PageBean;
import com.steadyjack.server.model.Student;
import com.steadyjack.server.service.StudentService;

@Service
public class StudentServiceImpl implements StudentService{
	
	@Resource
	private StudentDao studentDao;
	
	@Override
	public List<Student> studentList(PageBean pageBean, Student student,
			String bbirthday, String ebirthday) throws Exception {
		return studentDao.studentList(pageBean, student, bbirthday, ebirthday);
	}

	@Override
	public long studentCount(Student student, String bbirthday, String ebirthday)
			throws Exception {
		return studentDao.studentCount(student, bbirthday, ebirthday);
	}

	@Override
	public int studentDelete(String delIds) throws Exception {
		return studentDao.studentDelete(delIds);
	}

	@Override
	public int studentSave(Student student) throws Exception {
		return studentDao.studentSave(student);
	}

	@Override
	public boolean getStudentByGradeId(String gradeId) throws Exception {
		return studentDao.getStudentByGradeId(gradeId);
	}

}


   先介绍到这里吧,明晚继续!!!个人QQ:1948831260

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u013871100/article/details/52280899

智能推荐

陈莉君老师写的一篇轻松的文章_freedom1013的博客-程序员宅基地

the full of fun about Linux(zt)Everything you always wanted to know about computers...INTRODUCTIONBIOS: Hmm... Im waking up. Linux Loader, are you there ?LILO: Hmm... Yes, Im here. How do you do ? I

苹果Mac安装软件时,为什么有时候需要输入密码?_ITACHI.鼬的博客-程序员宅基地_mac安装软件需要输入密码

“软件打不开,来自身份不明的开发者?安装软件提示损坏,您应该将它移到废纸篓?…”相信很多小伙伴在刚开始接触 Mac 电脑的时候,安装软件都遇到过这些问题。为什么有时候需要输入密码?或者出现软件安装不上的情况呢?其实并不是软件本身有问题,而是Mac系统安全机制的问题。因为从网站下载的软件资源是汉化、破解的版本,想要正常安装使用的方法就是更改Mac系统安全设置,开启【任何来源】选项。1.首先点击屏幕左上角的苹果图标,打开系统偏好设置,选择安全性与隐私。2.点击左下角...

linux下解决tomcat中文乱码问题_fox_初始化的博客-程序员宅基地

转自:https://blog.csdn.net/m0_37392489/article/details/77683876在tomcat下的conf目录文件夹下server.xml文件中添加useBodyEncodingForURI="true"能很好的解决此类问题的发生。&lt;Connector port="8080"useBodyEncodingForURI="true"red...

Linux-进程、进程组、作业、会话、控制终端详解_擒贼先擒王的博客-程序员宅基地_进程申请控制终端

From:http://www.cnblogs.com/JohnABC/p/4079669.htmlLinux进程优先级的处理--Linux进程的管理与调度(二十二):http://blog.csdn.net/gatieme/article/details/51719208进程 、进程组、会话、控制终端之间的关系:http://blog.csdn.net/yh1548503342/ar

Jmeter使用教程-----响应断言_仙女都是傲娇的啊的博客-程序员宅基地

Jmeter使用教程-----断言先在jmeter里添加一个http请求:线程组》取样器》http请求添加请求后,填写请求信息添加信息头管理,右键线程组》添加》配置元件》HTTP信息头管理器在最下面点击添加,设置信息头,名称里输入content-type,根据报文格式设置,如果是xml格式的,设置成text/xml,json的可以设置Application/json如下图所示,可...

视频回传android客户端,基于DASH的视频传输优化及在Android客户端的播放实现_weixin_39541844的博客-程序员宅基地

摘要:近来随着生活水平的不断提高人们越来越追求高品质的生活质量,对多媒体影音的观看体验就是其中之一。网络设施的逐渐完善和硬件技术的不断进步使得传输和观看高质量多媒体内容成为可能。在这种情况下,传统的流媒体传输协议与传输方式由于本身存在的诸多缺陷已经无法满足人们的需求,新一代流媒体传输协议MPEG-DASH应运而生。然而DASH协议只对媒体内容的组织形式和传输方式进行了规范,其影响用户观看体验的码率...

随便推点

Guava的Maps使用_仰望夜空一万次的博客-程序员宅基地_guava的maps

1.概述在本教程中,我们将说明可利用Guava与Java Maps结合使用的最有用的方法。让我们开始非常简单,并使用Guava创建一个没有new运算符的HashMap://com.google.common.collect.MapsMap aNewMap = Maps.newHashMap();2. ImmutableMap接下来–让我们看看如何使用Guava创建ImmutableM...

百度贴吧引流必须知道的七大技巧_普通网友的博客-程序员宅基地

各个年龄阶段的小伙伴,都喜欢玩贴吧,在里面分享自己的作品及心情,百度贴吧也越来越活跃,在前年,小编也玩百度产品,包括:百度贴吧,在贴吧里面发软文、打广告,还吸引了大量意向用户,为自己创造了好的业绩,同理,百度贴吧运用的好,就能为自己的网站带来大量流量,那么,通过百度贴吧引流的方法有哪些呢?1、文章标题很重要一个好的标题等于成功的一半,人都一样,具有极强的好奇心,标题党总能吸引大量用户点击你

MathML: 描述数学表达式_friping的博客-程序员宅基地

在Web上显示、操作、共享及其它 文档选项 打印本页 将此页作为电子邮件发送 级别: 中级车皓阳 ([email protected])中科院软件所张治 ([email protected])上海交通大学图像处理与模式识别研究所2003 年 3 月 01 日许多STM(Scientific...

VTK入门(一)--分析示例程序,对图形拉伸_悄悄咪咪的码农的博客-程序员宅基地_vtk对比度拉伸

作为一个刚刚入门VTK的菜鸟,我决定先从分析vtk的示例程序做起。vtk的程序流程通常是下面这样子的:读取或者生成数据        vtk中提供了获取数据的方法,一种是直接从文件中读取数据,另一种是生成相应的源对象(流水线的起点操作),通过vtkConeSource *cone = vtkConeSource::New();生成一个源对象(椎体)。过滤、映射

9css分离:extract-text-webpack-plugin_无忧勾勾的博客-程序员宅基地

css分离:extract-text-webpack-plugin安装该插件: for webpack 1 npm install –save-dev [email protected] for webpack 2 npm install –save-dev extract-text-webpack-plugi...

推荐文章

热门文章

相关标签