博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java之spring之整合ssh
阅读量:5037 次
发布时间:2019-06-12

本文共 7737 字,大约阅读时间需要 25 分钟。

 

这篇主要讲解spring + struts2 + hibernate :

目录结构如下:

t_role

t_user

 

1.新建 web项目 :spring_ssh

2.在 WebRoot/WEB-INF/lib 下 导入jar包

antlr-2.7.7.jaraopalliance.jarasm-3.3.jarasm-commons-3.3.jarasm-tree-3.3.jaraspectjweaver.jarcommons-fileupload-1.2.2.jarcommons-io-2.0.1.jarcommons-lang3-3.1.jarcommons-logging.jardom4j-1.6.1.jarfreemarker-2.3.19.jarhibernate-commons-annotations-4.0.5.Final.jarhibernate-core-4.3.10.Final.jarhibernate-jpa-2.1-api-1.0.0.Final.jarjandex-1.1.0.Final.jarjavassist-3.18.1-GA.jarjboss-logging-3.1.3.GA.jarjboss-logging-annotations-1.2.0.Beta1.jarjboss-transaction-api_1.2_spec-1.0.0.Final.jarjstl-1.2.jarmysql-connector-java-5.1.20-bin.jarognl-3.0.5.jarspring-aop-4.1.6.RELEASE.jarspring-aspects-4.1.6.RELEASE.jarspring-beans-4.1.6.RELEASE.jarspring-context-4.1.6.RELEASE.jarspring-core-4.1.6.RELEASE.jarspring-expression-4.1.6.RELEASE.jarspring-jdbc-4.1.6.RELEASE.jarspring-orm-4.1.6.RELEASE.jarspring-tx-4.1.6.RELEASE.jarspring-web-4.1.6.RELEASE.jarstruts2-core-2.3.4.jarstruts2-spring-plugin-2.3.4.jarxwork-core-2.3.4.jar

3.编写vo类 :Role.java , User.java

Role.java

package cn.vincent.vo;import java.io.Serializable;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name="t_role")public class Role implements Serializable {    @Id    @GeneratedValue(strategy=GenerationType.AUTO)    private int id;    private String name;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    }
View Code

User.java

package cn.vincent.vo;import java.io.Serializable;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.ManyToOne;import javax.persistence.Table;@Entity@Table(name="t_user")public class User implements Serializable {    @Id    @GeneratedValue(strategy=GenerationType.AUTO)    private int id;    private String name;    private int age;    @ManyToOne    @JoinColumn(name="roleId")    private Role role;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public Role getRole() {        return role;    }    public void setRole(Role role) {        this.role = role;    }    }
View Code

4.编写 dao 

UserDao.java

package cn.vincent.dao;import java.util.List;import cn.vincent.vo.User;public interface UserDao {    public List
findAll();}
View Code

UserDaoImpl.java

package cn.vincent.dao.impl;import java.util.List;import org.hibernate.SessionFactory;import cn.vincent.dao.UserDao;import cn.vincent.vo.User;public class UserDaoImpl implements UserDao{    private SessionFactory sessionFactory;        @Override    public List
findAll() { return sessionFactory.getCurrentSession() .createQuery("from User").list(); } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } }
View Code

5.编写 service

UserService.java

package cn.vincent.service;import java.util.List;import cn.vincent.vo.User;public interface UserService {    public List
findAll();}
View Code

UserServiceImpl.java

package cn.vincent.service.impl;import java.util.List;import cn.vincent.dao.UserDao;import cn.vincent.service.UserService;import cn.vincent.vo.User;public class UserServiceImpl implements UserService {    private UserDao userDao;    @Override    public List
findAll() { return userDao.findAll(); } public void setUserDao(UserDao userDao) { this.userDao = userDao; }}
View Code

6.编写 action

package cn.vincent.action;import java.util.List;import com.opensymphony.xwork2.Action;import cn.vincent.service.UserService;import cn.vincent.vo.User;public class UserAction {    private List
list; private UserService userService; public String list(){ list = userService.findAll(); return Action.SUCCESS; } public List
getList() { return list; } public void setList(List
list) { this.list = list; } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } }
View Code

7.编写 spring 配置文件

applicationContext-dao.xml

org.hibernate.dialect.MySQLDialect
true
true
cn.vincent.vo.User
cn.vincent.vo.Role

applicationContext-asd.xml

8.编写 struts2 配置文件

struts.xml

/list.jsp

9.编写 web.xml 

contextConfigLocation
classpath:applicationContext-*.xml
org.springframework.web.context.ContextLoaderListener
osiv
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
sessionFactoryBeanName
sessionFactory
osiv
*.action
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*

10.编写 jsp

<%@ page language="java" contentType="text/html; charset=utf-8"    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+"/";%>
用户管理
编号 姓名 年龄 角色
${bean.id } ${bean.name } ${bean.age } ${bean.role.name }

11. 运行web项目

首先部署项目

 

 访问地址如下:

至于访问地址为什么是这个,可以查看   

 github地址:

转载于:https://www.cnblogs.com/Vincent-yuan/p/11259469.html

你可能感兴趣的文章
DP入门——01背包 & 完全背包
查看>>
智能电视系列(4)-高通,天才与极限
查看>>
让entityframework.extend库同时支持mysql,sqlsever
查看>>
hdoj:2061
查看>>
Oarcle 入门之like关键字
查看>>
Entity Framework Code First (七)空间数据类型 Spatial Data Types
查看>>
2012年度读写Excel文件的最佳PHP类库收集
查看>>
读过的书
查看>>
ll 详解
查看>>
form表单中的label标签
查看>>
eclipse下解决明明有jar包,却找不到的问题
查看>>
Entity Framework 学习初级篇1--EF基本概况(入门)
查看>>
C Looooops
查看>>
bzoj 2226 LCMSum 欧拉函数
查看>>
JavaSript模块规范 - AMD规范与CMD规范介绍
查看>>
都市环游
查看>>
【工具】【截图工具】FScapture,支持滚动
查看>>
jQuery延迟加载(懒加载)插件 – jquery.lazyload.js
查看>>
人脸检测(1)——HOG特征
查看>>
react native 示例代码
查看>>