码迷,mamicode.com
首页 > 其他好文 > 详细

SSH未成熟版

时间:2014-05-06 00:00:14      阅读:333      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   java   ext   color   

本实验采用Spring中配置Action,Spring代理生成Struts的类

-->Dept.java

package cn.itcast.domain;

 

public class Dept {

   private Integer id;

 

   private String deptno;

   private String deptname;

 

   public String getDeptno() {

      return deptno;

   }

 

   public void setDeptno(String deptno) {

      this.deptno = deptno;

   }

 

   public String getDeptname() {

      return deptname;

   }

 

   public void setDeptname(String deptname) {

      this.deptname = deptname;

   }

 

   public Integer getId() {

      return id;

   }

 

   public void setId(Integer id) {

      this.id = id;

   }

}

-->Dept.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<!-- 完成实体类 和 数据表 映射 -->

<hibernate-mapping>

   <class name="cn.itcast.domain.Dept" table="dept">

      <id name="id" column="dept_id">

        <generator class="native"></generator>

      </id>

      <!-- 普通属性 -->

      <property name="deptno"></property>

      <property name="deptname"></property>

   </class>

</hibernate-mapping>

 

--> Student

package cn.itcast.domain;

 

import java.util.Date;

 

public class Student {

   private Integer id;

   private String number;// 学生学号

   private String name;// 姓名

   private String deptno;// 所在院系编号

   private String sex;// 学生性别

   private Date birthday;// 学生出生日期

 

   public Integer getId() {

      return id;

   }

 

   public void setId(Integer id) {

      this.id = id;

   }

 

   public String getName() {

      return name;

   }

 

   public void setName(String name) {

      this.name = name;

   }

 

   public String getDeptno() {

      return deptno;

   }

 

   public void setDeptno(String deptno) {

      this.deptno = deptno;

   }

 

   public String getNumber() {

      return number;

   }

 

   public void setNumber(String number) {

      this.number = number;

   }

 

   public String getSex() {

      return sex;

   }

 

   public void setSex(String sex) {

      this.sex = sex;

   }

 

   public Date getBirthday() {

      return birthday;

   }

 

   public void setBirthday(Date birthday) {

      this.birthday = birthday;

   }

 

   @Override

   public String toString() {

      return "Student [id=" + id + ", number=" + number + ", name=" + name

           + ", deptno=" + deptno + ", sex=" + sex + ", birthday="

           + birthday + "]";

   }

  

}

--> Student.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<!-- 完成实体类 和 数据表 映射 -->

<hibernate-mapping>

   <class name="cn.itcast.domain.Student" table="Student">

      <id name="id" column="student_id">

        <generator class="native"></generator>

      </id>

      <!-- 普通属性 -->

      <property name="number" length="32"/>

      <property name="name" length="32"/>

      <property name="deptno" length="32"/>

      <property name="sex" length="2"/>

      <property name="birthday"/>

   </class>

</hibernate-mapping>

-->StudentDao.java

package cn.itcast.dao;

 

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import org.hibernate.Criteria;

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.criterion.Restrictions;

 

import cn.itcast.domain.Student;

import cn.itcast.util.HibernateUtil;

 

public class StudentDao {

   private int pageSize;

   private int page;

 

   public int getPageSize() {

      return pageSize;

   }

 

   public void setPageSize(int pageSize) {

      this.pageSize = pageSize;

   }

 

   public int getPage() {

      return page;

   }

 

   public void setPage(int page) {

      this.page = page;

   }

 

   public List<Student> selectStudentByXing(String name){

      Session session = HibernateUtil.getSession();

      Criteria  criteria =session.createCriteria(Student.class);

        criteria.add(Restrictions.like("name",name+"%"));

      return criteria.list();

   }

   public List<Student> selectStudent(Student student) {

      Session session = HibernateUtil.getSession();

      Query query = session

            .createQuery("from Student as student where student.name = :name");

      query.setParameter("name", student.getName());

 

      List s = query.list();

      return s;

   }

 

   public List<Student> selectStudentIterate() {

      Session session = HibernateUtil.getSession();

      Query query = session.createQuery("from Student");

 

      query.setFirstResult((page - 1) * pageSize);

      query.setMaxResults(pageSize);

 

      List<Student> list = new ArrayList<Student>();

      Iterator students = query.iterate();

      while (students.hasNext()) {

         Student student = (Student) students.next();

        list.add(student);

      }

      return list;

   }

 

   public List<Student> selectStudentList() {

      Session session = HibernateUtil.getSession();

      Query query = session.createQuery("from Student");

 

      query.setFirstResult((page - 1) * pageSize);

      query.setMaxResults(pageSize);

 

      return query.list();

   }

}

--> StudentDeptDao.java

package cn.itcast.dao;

 

/*

 * 可加pagesize

 * pageCount控制每页显示的多少

 */

import java.util.List;

import org.hibernate.Query;

import org.hibernate.Session;

import cn.itcast.util.HibernateUtil;

 

public class StudentDeptDao {

   public List selectStudentDept() {

      Session session = HibernateUtil.getSession();

 

      Query q = session

           .createQuery("from Student s , Dept d where s.deptno = d.deptno");

      List l = q.list();

      return l;

   }

}

-->StudentService

package cn.itcast.service;

 

import java.util.List;

import cn.itcast.dao.StudentDao;

import cn.itcast.domain.Student;

 

public class StudentService {

   private StudentDao studentDao;

 

   public List<Student> selectStudent(Student student) {

      studentDao=new StudentDao();

      return studentDao.selectStudent(student);

   }

 

   public List<Student> selectStudentIterate() {

      studentDao=new StudentDao();

      return studentDao.selectStudentIterate();

   }

 

   public List<Student> selectStudentList() {

      studentDao=new StudentDao();

      return studentDao.selectStudentList();

   }

   public List<Student> selectStudentListByXing(String name) {

      studentDao=new StudentDao();

      return studentDao.selectStudentByXing(name);

   }

 

   public StudentDao getStudentDao() {

      return studentDao;

   }

 

   public void setStudentDao(StudentDao studentDao) {

      this.studentDao = studentDao;

   }

  

}

-->StudentDeptService

package cn.itcast.service;

 

import java.util.List;

import cn.itcast.dao.StudentDeptDao;

 

public class StudentDeptService {

   private StudentDeptDao studentDeptDao;

  

   public StudentDeptDao getStudentDeptDao() {

      return studentDeptDao;

   }

 

   public void setStudentDeptDao(StudentDeptDao studentDeptDao) {

      this.studentDeptDao = studentDeptDao;

   }

   public List selectStudentDept() {

      return studentDeptDao.selectStudentDept();

   }

}

 -->StudentAction

package cn.itcast.action;

àStudentAction

import java.util.List;

import cn.itcast.domain.Student;

import cn.itcast.service.StudentService;

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;

 

public class StudentAction extends ActionSupport {

   private static final long serialVersionUID = 1L;

   private StudentService studentService;

   private String name;

 

   public StudentService getStudentService() {

      return studentService;

   }

 

   public void setStudentService(StudentService studentService) {

      this.studentService = studentService;

   }

 

   public String getName() {

      return name;

   }

 

   public void setName(String name) {

      this.name = name;

   }

 

   @Override

   public String execute() throws Exception {

      System.out.println(name);

      List<Student> sdlistxing = studentService.selectStudentListByXing(name);

      if (sdlistxing.size() > 0) {

        ActionContext.getContext().put("sdlistxing", sdlistxing);

        return SUCCESS;

      } else {

        return ERROR;

      }

   }

}

 

-->StudentDeptAction

package cn.itcast.action;

 

import java.util.List;

import cn.itcast.service.StudentDeptService;

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;

 

public class StudentDeptAction extends ActionSupport {

   private static final long serialVersionUID = 1L;

   private StudentDeptService studentDeptService;

 

   public StudentDeptService getStudentDeptService() {

      return studentDeptService;

   }

 

   public void setStudentDeptService(StudentDeptService studentDeptService) {

      this.studentDeptService = studentDeptService;

   }

 

   @Override

   public String execute() throws Exception {

      List sdlist = studentDeptService.selectStudentDept();

      if (sdlist.size() > 0) {

        ActionContext.getContext().put("sdlist", sdlist);

        return SUCCESS;

      } else {

        return ERROR;

      }

   }

 

}

 

-->showAllStudentDept.jsp

<%@ page language="java" import="java.util.*,cn.itcast.domain.*"

   pageEncoding="utf-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>

<%

   String path = request.getContextPath();

   String basePath = request.getScheme() + "://"

        + request.getServerName() + ":" + request.getServerPort()

        + path + "/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>show all student</title>

</head>

<body>

   <form action="" method="post">

      <table border="1" bordercolor="green" cellspacing="0" width="500px">

        <tr>

           <td colspan="6" align="center"><b>所有学生信息</b></td>

        </tr>

        <tr>

           <th>学 号</th>

           <th>姓 名</th>

           <th>性 别</th>

           <th>生 日</th>

           <th>系编号</th>

           <th>系名称</th>

        </tr>

        <s:iterator value="sdlist" var="sd">

           <tr>

        <!--输出List<Object[]> -->

              <td><s:property value="#sd[0].number" /></td>

              <td><s:property value="#sd[0].name" /></td>

              <td><s:property value="#sd[0].sex" /></td>

              <td><s:property value="#sd[0].birthday" /></td>

              <td><s:property value="#sd[1].deptno" /></td>

              <td><s:property value="#sd[1].deptname" /></td>

              <td>

           </tr>

        </s:iterator>

      </table>

   </form>

   <a href="<%=basePath%>/welcome.jsp">返回主界面</a>

</body>

</html>

-->showAllStudentByXing.jsp

<%@ page language="java" import="java.util.*,cn.itcast.domain.*"

   pageEncoding="utf-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>

<%

   String path = request.getContextPath();

   String basePath = request.getScheme() + "://"

        + request.getServerName() + ":" + request.getServerPort()

        + path + "/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>show all student</title>

</head>

<body>

   <form action="" method="post">

      <table border="1" bordercolor="green" cellspacing="0" width="500px">

        <tr>

           <td colspan="6" align="center"><b>所有学生信息</b></td>

        </tr>

        <tr>

           <th>id</th>

           <th>姓 名</th>

           <th>学 号</th>

           <th>性 别</th>

           <th>生日</th>

        </tr>

        <s:iterator value="sdlistxing" var="sdx">

           <tr>

              <td><s:property value="#sdx.id" /></td>

              <td><s:property value="#sdx.number" /></td>

              <td><s:property value="#sdx.name" /></td>

              <td><s:property value="#sdx.deptno" /></td>

              <td><s:property value="#sdx.sex" /></td>

              <td><s:property value="#sdx.birthday" /></td>

              <td>

           </tr>

        </s:iterator>

      </table>

   </form>

   <a href="<%=basePath%>/welcome.jsp">返回主界面</a>

</body>

</html>

 

--> hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC

   "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

   "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

   <session-factory>

      <!-- Database connection settings -->

      <!-- 配置驱动 -->

      <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

      <property name="connection.url">jdbc:mysql://127.0.0.1:3306/hibe1</property>

      <property name="connection.username">root</property>

      <property name="connection.password">root</property>

      <!-- 是否根据hbm文件生成数据库 -->

      <property name="hibernate.hbm2ddl.auto">update</property>

      <!-- 是否打印sql语句 -->

      <property name="show_sql">true</property>

      <!-- C3P0连接池设定 -->

      <!-- 使用c3po连接池 配置连接池提供的供应商 -->

      <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider                                                                                                                                                     </property>

      <!-- 配置方言. -->

      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

      <mapping resource="cn/itcast/domain/Dept.hbm.xml" />

      <mapping resource="cn/itcast/domain/Student.hbm.xml" />

   </session-factory>

</hibernate-configuration>

 

-->struts.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

   "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

   "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

   <constant name="struts.devMode" value="false" />

   <constant name="struts.i18n.encoding" value="utf-8" />

   <constant name="struts.objectFactory" value="spring" />

   <package name="test" namespace="/" extends="struts-default">

      <action name="sddall" class="cn.itcast.action.StudentDeptAction">

        <result>/showAllStudentDept.jsp</result>

        <result name="error">/showAllStudentDeptError.jsp</result>

      </action>

   </package>

   <package name="test2" namespace="/" extends="struts-default">

      <action name="showbyxing" class="cn.itcast.action.StudentAction">

        <result>/showAllStudentByXing.jsp</result>

        <result name="error">/showAllStudentByXingError.jsp</result>

      </action>

  

à applicationContext.xml

<?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:p="http://www.springframework.org/schema/p"

   xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"

   xmlns:context="http://www.springframework.org/schema/context"

   xsi:schemaLocation="http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

   http://www.springframework.org/schema/context

   http://www.springframework.org/schema/context/spring-context-4.0.xsd

   http://www.springframework.org/schema/tx

   http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

   http://www.springframework.org/schema/aop

   http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

   <!-- 配置Action -->

   <bean id="StudentDeptAction" class="cn.itcast.action.StudentDeptAction"

      scope="prototype">

      <property name="studentDeptService" ref="studentDeptService" />

   </bean>

   <bean id="studentDeptService" class="cn.itcast.service.StudentDeptService">

      <property name="studentDeptDao" ref="studentDeptDao"></property>

   </bean>

   <bean id="studentDeptDao" class="cn.itcast.dao.StudentDeptDao"></bean>

 

   <!-- 配置Action -->

   <bean id="StudentAction" class="cn.itcast.action.StudentAction"

      scope="prototype">

      <property name="studentService" ref="studentService" />

   </bean>

   <bean id="studentService" class="cn.itcast.service.StudentService">

      <property name="studentDao" ref="studentDao"></property>

   </bean>

   <bean id="studentDao" class="cn.itcast.dao.StudentDao"></bean>

</beans>

 

数据库脚本:

后台数据库:

数据库脚本:

-- --------------------------------------------------------

-- 主机:                           127.0.0.1

-- 服务器版本:                        5.5.36-MariaDB - mariadb.org binary distribution

-- 服务器操作系统:                      Win32

-- HeidiSQL 版本:                  8.0.0.4396

-- --------------------------------------------------------

 

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;

/*!40101 SET NAMES utf8 */;

/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;

/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE=‘NO_AUTO_VALUE_ON_ZERO‘ */;

 

-- 导出 hibe1 的数据库结构

CREATE DATABASE IF NOT EXISTS `hibe1` /*!40100 DEFAULT CHARACTER SET utf8 */;

USE `hibe1`;

 

 

-- 导出  表 hibe1.dept 结构

CREATE TABLE IF NOT EXISTS `dept` (

  `dept_id` int(11) NOT NULL AUTO_INCREMENT,

  `deptno` varchar(255) DEFAULT NULL,

  `deptname` varchar(255) DEFAULT NULL,

  PRIMARY KEY (`dept_id`)

) ENGINE=InnoDB AUTO_INCREMENT=65 DEFAULT CHARSET=utf8;

 

-- 正在导出表  hibe1.dept 的数据:~2 rows (大约)

DELETE FROM `dept`;

/*!40000 ALTER TABLE `dept` DISABLE KEYS */;

INSERT INTO `dept` (`dept_id`, `deptno`, `deptname`) VALUES

       (1, ‘d01‘, ‘计算机‘),

       (2, ‘d02‘, ‘数学‘);

/*!40000 ALTER TABLE `dept` ENABLE KEYS */;

 

 

-- 导出  表 hibe1.student 结构

CREATE TABLE IF NOT EXISTS `student` (

  `student_id` int(11) NOT NULL AUTO_INCREMENT,

  `number` varchar(32) DEFAULT NULL,

  `name` varchar(32) DEFAULT NULL,

  `deptno` varchar(32) DEFAULT NULL,

  `sex` varchar(2) DEFAULT NULL,

  `birthday` datetime DEFAULT NULL,

  PRIMARY KEY (`student_id`)

) ENGINE=InnoDB AUTO_INCREMENT=1134110143 DEFAULT CHARSET=utf8;

 

-- 正在导出表  hibe1.student 的数据:~3 rows (大约)

DELETE FROM `student`;

/*!40000 ALTER TABLE `student` DISABLE KEYS */;

INSERT INTO `student` (`student_id`, `number`, `name`, `deptno`, `sex`, `birthday`) VALUES

       (1134110140, ‘s001‘, ‘陈新卫‘, ‘d01‘, ‘男‘, ‘2014-04-23 15:20:02‘),

       (1134110141, ‘s002‘, ‘张三‘, ‘d02‘, ‘男‘, ‘1992-03-13 15:19:41‘),

       (1134110142, ‘s003‘, ‘张三‘, ‘d02‘, ‘男‘, ‘1992-03-13 15:19:41‘);

/*!40000 ALTER TABLE `student` ENABLE KEYS */;

/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, ‘‘) */;

/*!40014 SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS IS NULL, 1, @OLD_FOREIGN_KEY_CHECKS) */;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;

 

bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣

 

 

bubuko.com,布布扣

 

 

 

 

 

SSH未成熟版,布布扣,bubuko.com

SSH未成熟版

标签:style   blog   class   java   ext   color   

原文地址:http://www.cnblogs.com/jianfengyun/p/3704477.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!