您的位置:首页 > 编程语言 > Java开发

springMVC+Hibernate简单的Demo

2015-02-14 11:25 477 查看
<pre class="java" name="code"><pre class="html" name="code">
最近在学习springMVC,在网上找了一些例子看,最后自己整理出来一个简单的例子,大家一起交流学习下。<img src="https://img-blog.csdn.net/20150214113047962?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcGFzc2lvbl96aGFuRw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />



 整体结构就是这样了 需要的jar包我这里面的比较乱,就不贴出来了,一般都是spring的和hibernate的jar包,还有数据库的。

下面贴代码:

Student.Java

package bean;

public class Student{

private String id;
private String name;
private String sex;
private String school_id;
private String edu;
private String tel;
private String addr;
private String sno;

public String getSchool_id() {
return school_id;
}
public void setSchool_id(String school_id) {
this.school_id = school_id;
}

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getEdu() {
return edu;
}
public void setEdu(String edu) {
this.edu = edu;
}

public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
}

Student.hbm.xml 这里用的是配置文件的方式,不是注解。当然注解也可以,当时用注解的时候总是不行,后来改用的配置文件。有朋友使用注解方式搞好了可以教教我。

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="bean.Student" table="student">
<id name="id" column="id" type="string">
<generator class="uuid" />
</id>
<property name="name" column="name" type="string" />
<property name="addr" column="addr" type="string" />
<property name="sex" column="sex" type="string" />
<property name="school_id" column="school_id" type="string" />
<property name="edu" column="edu" type="string" />
<property name="tel" column="tel" type="string" />
<property name="sno" column="sno" type="string" />
</class>
</hibernate-mapping>


StudentController.java 在这个Controller里面写了一个check方法,就是根据现有学生表中的姓名和学号来进行登录,做的时候就是简单添加一下登录验证,需要完善修饰的地方还有很多。

package controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import service.StudentService;
import bean.Student;

@Controller
@RequestMapping(value = "/student")
public class StudentController {

@Resource(name = "studentService")
private StudentService studentService;

@RequestMapping(value = "/login")
public String list(HttpServletRequest req, String school) {
List<Student> students = new ArrayList<Student>();

if (school != null && !school.equals("")) {
students = studentService.search(school);
} else {
students = studentService.findAllStudent();
}
req.setAttribute("students", students);
req.setAttribute("school", school);
return ("studentList");
}

//	@RequestMapping(value = "/findStudentById")
//	public String findStudentById(String id) {
//		Student stu = studentService.findStudentById(id);
//		System.out.println(stu);
//		return "showStudent";
//	}
@RequestMapping(value = "check")
public String check(HttpServletRequest req,HttpServletResponse resp){
String name = req.getParameter("name");
String sno = req.getParameter("sno");
Student stu = studentService.findStudentByName(name, sno);
if (stu.getName() != null && stu.getSno() != null) {
return "redirect:/student/findAllStudent";
}else{

return "redirect:/index.jsp";
}
}

@RequestMapping(value = "/showStudent")
public String showStudent(HttpServletRequest req, String id) {
Student stu = studentService.findStudentById(id);
req.setAttribute("stu", stu);
return "showStudent";
}

@RequestMapping(value = "/addStudentUI")
public String addStudentUI() {
return "addStudent";
}

@RequestMapping(value = "/addStudent")
public String addStudent(Student stu) {
studentService.addStudent(stu);
System.out.println(stu);
// 重定向
return "redirect:/student/findAllStudent";
}

@RequestMapping(value = "/deleteStudentById")
public String deleteStudentById(String id) {
studentService.deleteStudentById(id);

// 重定向
return "redirect:/student/findAllStudent";
}

// 批量删除
@RequestMapping(value = "/deleteStudentByIds")
public String deleteStudentByIds(String ids) {
ids = ids.substring(0, ids.length() - 1);
String[] idss = ids.split(",");
for (String id : idss) {
studentService.deleteStudentById(id);
}
// 重定向
return "redirect:/student/findAllStudent";
}

@RequestMapping(value = "/findAllStudent")
public String findAllStudent(HttpServletRequest req) {
List<Student> students = studentService.findAllStudent();
req.setAttribute("students", students);
return "studentList";
}

@RequestMapping(value = "/updateStudentUI")
public String updateStudentUI(HttpServletRequest req, String id) {
Student stu = studentService.findStudentById(id);
req.setAttribute("stu", stu);
return "updateStudent";
}

@RequestMapping(value = "/updateStudent")
public String updateStudent(Student stu) {
studentService.updateStudent(stu);
// 重定向
return "redirect:/student/findAllStudent";
}

}


StudentService.java

package service;

import java.util.List;

import bean.Student;

public interface StudentService {

public Student findStudentById(String id);

public Student findStudentByName(String name,String sno);

public List<Student> findAllStudent();

public List<Student> search(String school);

public void addStudent(Student stu);

public void deleteStudentById(String id);

public void updateStudent(Student stu);

}


 

StudentServiceImpl.java

package service;

import java.util.List;

import javax.annotation.Resource;

import dao.StudentDao;

import bean.Student;

@org.springframework.stereotype.Service(value = "studentService")
public class StudentServiceImpl implements StudentService {

@Resource(name = "studentDao")
private StudentDao studentDao;

@Override
public List<Student> findAllStudent() {

return studentDao.findAllStudent();
}

@Override
public void addStudent(Student stu) {
studentDao.addStudent(stu);

}

@Override
public void updateStudent(Student stu) {
studentDao.updateStudent(stu);

}

@Override
public Student findStudentById(String id) {

return studentDao.findStudentById(id);
}

@Override
public Student findStudentByName(String name,String sno) {

return studentDao.findStudentByName(name,sno);
}

@Override
public void deleteStudentById(String id) {
studentDao.deleteStudentById(id);

}

@Override
public List<Student> search(String school) {

return studentDao.search(school);
}

}
StudentDao.java
<pre class="java" name="code">package dao;

import java.util.List;

import bean.Student;

public interface StudentDao {

public Student findStudentById(String id);

public Student findStudentByName(String name,String sno);

public List<Student> findAllStudent();

public List<Student> search(String school);

public void addStudent(Student stu);

p
4000
ublic void updateStudent(Student stu);

public void deleteStudentById(String id);
}


StudentDaoImpl.java
<pre class="java" name="code">package dao;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import bean.Student;

@Repository(value="studentDao")
public class StudentDaoImpl implements StudentDao{

@Resource(name="sessionFactory")
private SessionFactory sf;

@Override
public Student findStudentById(String id) {
return (Student) sf.getCurrentSession().createQuery("from Student where id = '" + id + "'").list().get(0);
}

@SuppressWarnings("unchecked")
@Override
public Student findStudentByName(String name, String sno) {
List<Student> list = sf
.getCurrentSession()
.createQuery(
"from Student where name = '" + name + "' and sno = '"
+ sno + "' ").list();
if (!list.isEmpty()) {
return (Student) sf
.getCurrentSession()
.createQuery(
"from Student where name = '" + name
+ "' and sno = '" + sno + "' ").list().get(0);
} else {
list = new ArrayList<Student>();
Student stu = new Student();
stu.setName(null);
stu.setSno(null);
list.add(stu);
return (Student) list.get(0);
}
}
@SuppressWarnings("unchecked")
@Override
public List<Student> findAllStudent() {
return sf.getCurrentSession().createQuery("from Student").list();
}

@SuppressWarnings("unchecked")
public List<Student> search(String school) {
return sf.getCurrentSession().createQuery("from Student where school_id = '" + school + "'").list();
}

@Override
public void addStudent(Student stu) {
sf.getCurrentSession().save(stu);
}

@Override
public void deleteStudentById(String id) {
Student stu = new Student();
stu.setId(id);
sf.getCurrentSession().delete(stu);
}

@Override
public void updateStudent(Student stu) {
sf.getCurrentSession().update(stu);
}

}


applicationContext.xml
<pre class="html" name="code"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<!-- 注解驱动 -->
<mvc:annotation-driven />

<!-- 组件扫描 -->
<context:component-scan base-package="controller"></context:component-scan>
<context:component-scan base-package="bean"></context:component-scan>
<context:component-scan base-package="dao"></context:component-scan>
<context:component-scan base-package="service"></context:component-scan>
<!-- 定义数据源 -->

<bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
<property name="initialPoolSize" value="10"></property>
<property name="maxPoolSize" value="50"></property>
<property name="minPoolSize" value="10"></property>
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="ds"></property>
<!-- hibernate映射文件的位置 -->
<property name="packagesToScan">
<list>
<value>bean</value>
</list>
</property>
<property name="mappingDirectoryLocations">
<value>classpath:bean/</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.Dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>

<!-- 配置事物管理器 -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- 配置事物的传播特性 (事物通知) -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" read-only="true" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>

<aop:config>
<aop:advisor pointcut="execution(* *..*ServiceImpl.*(..))"
advice-ref="txAdvice" />
<!-- <aop:advisor advice-ref="txAdvice" pointcut="execution(* *..*ServiceImpl.*(..))"/> -->
</aop:config>

<mvc:resources location="/js/" mapping="/js/**" />
<!-- 配置内部资源视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>


web.xml
<pre class="html" name="code"><?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/classes/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>


studentList.jsp
<pre class="html" name="code"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<html>
<head>
<title>studentList.jsp</title>
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
function reset(){
$('input[name=school]').val('');
}
function selectOrUnslect(){
var ids = document.getElementsByName('ids');
if(document.getElementById('topId').checked == true){
for(var i=0;i<ids.length;i++){
ids[i].checked = true;
}
}else{
for(var i=0;i<ids.length;i++){
ids[i].checked = false;
}
}
}

function deleteSomeStudent(){
var ids = document.getElementsByName('ids');
var strIds = '';
for(var i=0;i<ids.length;i++){
if(ids[i].checked == true){
strIds += ids[i].value + ',';
}
}
if(strIds==''){
alert("请选择要删除的记录!");
}else{
window.location = '<%=path%>/student/deleteStudentByIds?ids=' + strIds;}
}
</script>
</head>
<body>
<h3>学生列表页面</h3>
<form action="<%=path %>/student/login" method="post">
学校:<input type="text" name="school" value="${school}">
<%--   学历:<select name="filter_edu" style="width:150px">
<option value="小学" <c:if test="${stu.edu eq '小学'}">selected</c:if>>小学</option>
<option value="初中" <c:if test="${stu.edu eq '初中'}">selected</c:if>>初中</option>
<option value="高中" <c:if test="${stu.edu eq '高中'}">selected</c:if>>高中</option>
<option value="大专" <c:if test="${stu.edu eq '大专'}">selected</c:if>>大专</option>
<option value="本科" <c:if test="${stu.edu eq '本科'}">selected</c:if>>本科</option>
<option value="硕士" <c:if test="${stu.edu eq '硕士'}">selected</c:if>>硕士</option>
<option value="博士" <c:if test="${stu.edu eq '博士'}">selected</c:if>>博士</option>
</select> --%>
<input name="input" value="查询" type="submit" />
<input name="resetSrchForm" type="button" onclick="reset()" value="重置" />
<input type="button" value="批量删除" onclick="deleteSomeStudent();">
</form>
<table width="100%" border="1">
<tr>
<td style="text-align: center"><input type="checkbox" id="topId"
onclick="selectOrUnslect();"></td>
<td style="text-align: center">姓名</td>
<td style="text-align: center">学号</td>
<td style="text-align: center">性别</td>
<td style="text-align: center">学校</td>
<td style="text-align: center">学历</td>
<td style="text-align: center">电话</td>
<td style="text-align: center">住址</td>
<td style="text-align: center">操作</td>
</tr>
<c:forEach items="${students}" var="stu">
<tr>
<td style="text-align: center"><input type="checkbox"
name="ids" value="${stu.id }"></td>
<td style="text-align: center">${stu.name }</td>
<td style="text-align: center">${stu.sno }</td>
<td style="text-align: center">${stu.sex}</td>
<td style="text-align: center">${stu.school_id}</td>
<td style="text-align: center">${stu.edu}</td>
<td style="text-align: center">${stu.tel}</td>
<td style="text-align: center">${stu.addr}</td>
<td style="text-align: center">
<a href="<%=path %>/student/addStudentUI">添加</a>  
<a href="<%=path %>/student/deleteStudentById?id=${stu.id}">删除</a> 
<a href="<%=path %>/student/updateStudentUI?id=${stu.id}">更新</a> 
<a href="<%=path %>/student/showStudent?id=${stu.id}">查看</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>


addStudent.jsp
<pre class="html" name="code"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<html>
<head>
<title>新增学生信息页面</title>
</head>
<body>
<h3>添加页面</h3>
<form action="<%=path%>/student/addStudent" method="post">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>学号:</td>
<td><input type="text" name="sno"></td>
</tr>
<tr>
<td>住址:</td>
<td><input type="text" name="addr"></td>
</tr>
<tr>
<td>性别:</td>
<td><select name="sex">
<option value="男">男</option>
<option value="女">女</option>
</select></td>
</tr>
<tr>
<td>学校:</td>
<td><input type="text" name="school_id"></td>
</tr>
<tr>
<td>学历:</td>
<td><select name="edu">
<option value="小学">小学</option>
<option value="初中">初中</option>
<option value="高中">高中</option>
<option value="大专">大专</option>
<option value="本科">本科</option>
<option value="硕士">硕士</option>
<option value="博士">博士</option>
</select></td>
</tr>
<tr>
<td>电话:</td>
<td><input type="text" name="tel"></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
<td><input type="reset" value="重置"></td>
</tr>
</table>
</form>
</body>
</html>


showStudent.jsp
<pre class="html" name="code"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<html>
<head>
<title>saveStudent.jsp</title>
</head>
<body>
<h3>学生信息查看页面</h3>
<form action="<%=path%>/student/findAllStudent" method="post">
<input type="hidden" name="id" value="${stu.id}">
<table>
<tr>
<td>姓名:</td>
<td>${stu.name }</td>
</tr>
<tr>
<td>学号:</td>
<td>${stu.sno }</td>
</tr>
<tr>
<td>地址:</td>
<td>${stu.addr }</td>
</tr>
<tr>
<td>性别:</td>
<td>${stu.sex }</td>
</tr>
<tr>
<td>学校:</td>
<td>${stu.school_id }</td>
</tr>
<tr>
<td>学历:</td>
<td>${stu.edu }</td>
</tr>
<tr>
<td>电话:</td
9ef4
>
<td>${stu.tel }</td>
</tr>
<tr>
<td><input type="submit" value="返回"></td>
</tr>
</table>
</form>
</body>
</html>


updateStudent.jsp
<pre class="html" name="code"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<html>
<head>
<title>updateStudent.jsp</title>
</head>
<body>
<h3>更新页面</h3>
<form action="<%=path%>/student/updateStudent" method="post">
<input type="hidden" name="id" value="${stu.id}">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="name" value="${stu.name }"></td>
</tr>
<tr>
<td>学号:</td>
<td><input type="text" name="sno" value="${stu.sno }"></td>
</tr>
<tr>
<td>地址:</td>
<td><input type="text" name="addr" value="${stu.addr }"></td>
</tr>
<tr>
<td>性别:</td>
<td><select name="sex">
<option value="男" <c:if test="${stu.sex eq '男'}">selected</c:if>>男</option>
<option value="女" <c:if test="${stu.sex eq '女'}">selected</c:if>>女</option>
</select></td>
</tr>
<tr>
<td>学校:</td>
<td><input type="text" name="school_id"
value="${stu.school_id }"></td>
</tr>
<tr>
<td>学历:</td>
<td><select name="edu">
<option value="小学" <c:if test="${stu.edu eq '小学'}">selected</c:if>>小学</option>
<option value="初中" <c:if test="${stu.edu eq '初中'}">selected</c:if>>初中</option>
<option value="高中" <c:if test="${stu.edu eq '高中'}">selected</c:if>>高中</option>
<option value="大专" <c:if test="${stu.edu eq '大专'}">selected</c:if>>大专</option>
<option value="本科" <c:if test="${stu.edu eq '本科'}">selected</c:if>>本科</option>
<option value="硕士" <c:if test="${stu.edu eq '硕士'}">selected</c:if>>硕士</option>
<option value="博士" <c:if test="${stu.edu eq '博士'}">selected</c:if>>博士</option>
</select></td>
</tr>
<tr>
<td>电话:</td>
<td><input type="text" name="tel" value="${stu.tel }"></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
<td><input type="reset" value="重置"></td>
</tr>
</table>
</form>
</body>
</html>


index.jsp
<pre class="html" name="code"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>
</head>
<body>
<center>
<br /> <br />
<h5 style="color: blue">学生登录</h5>
<form action="student/check" method="post">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>学号:</td>
<td><input type="text" name="sno" /></td>
</tr>
<tr>
<td />
<td><input type="submit" value=" 登录  " />
<input type="reset" value=" 重 置 " />
<a href="<%=path %>/student/addStudentUI">注册</a>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>


本人是初学阶段,上述代码可能有不妥当或是不正确的地方,希望大家多多交流 互相学习。

package dao;

import java.util.List;

import bean.Student;

public interface StudentDao {

public Student findStudentById(String id);

public Student findStudentByName(String name,String sno);

public List<Student> findAllStudent();

public List<Student> search(String school);

public void addStudent(Student stu);

public void updateStudent(Student stu);

public void deleteStudentById(String id);
}


 

package service;

import java.util.List;

import javax.annotation.Resource;

import dao.StudentDao;

import bean.Student;

@org.springframework.stereotype.Service(value = "studentService")
public class StudentServiceImpl implements StudentService {

@Resource(name = "studentDao")
private StudentDao studentDao;

@Override
public List<Student> findAllStudent() {

return studentDao.findAllStudent();
}

@Override
public void addStudent(Student stu) {
studentDao.addStudent(stu);

}

@Override
public void updateStudent(Student stu) {
studentDao.updateStudent(stu);

}

@Override
public Student findStudentById(String id) {

return studentDao.findStudentById(id);
}

@Override
public Student findStudentByName(String name,String sno) {

return studentDao.findStudentByName(name,sno);
}

@Override
public void deleteStudentById(String id) {
studentDao.deleteStudentById(id);

}

@Override
public List<Student> search(String school) {

return studentDao.search(school);
}

}


 

 

<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="bean.Student" table="student"> <id name="id" column="id" type="string"> <generator class="uuid" /> </id> <property name="name" column="name" type="string" /> <property name="addr" column="addr" type="string" /> <property name="sex" column="sex" type="string" /> <property name="school_id" column="school_id" type="string" /> <property name="edu" column="edu" type="string" /> <property name="tel" column="tel" type="string" /> <property name="sno" column="sno" type="string" /> </class> </hibernate-mapping>

 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: