目前对于这三大框架整合开发有很多人有这不同的看法,我在这提供一个自己比较喜欢的设计方式:
下面以搭建一个用户管理实验做为说明:
第一步:创建数据库(只需要建立数据库名字就可以了)
第二步:在myeclipse中切换到myeclipse database explorer 然后建立数据库连接(datebase driver)如图:我这里取名为:usermanager   完成待用
第三步:切换回myeclipse java enterprise界面;建立自己的项目:如我这里叫usermanager
第四步:建立对应的包:分别有action  service  serviceimpl  dao  daoimpl pojo  vo   tools用mvc设计模式    分三层一般建立这几个包就够了。
第五步:建立实体类并写好注解:如我这里就建立了:
package com.cn.nnny.ssh.pojo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
//用户实体
@Entity
@Table(name="users")
public class Users {
 private int  id;// 编号
 private String  name;//姓名
 private int age;//年龄
 private String  sex;//性别    男  女
 private Department department;//部门
 private String  telphone;//电话
 
 
 public Users(){
  
 }
@Id
@GeneratedValue
 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 String getSex() {
  return sex;
 }

 public void setSex(String sex) {
  this.sex = sex;
 }
@ManyToOne
 public Department getDepartment() {
  return department;
 }

 public void setDepartment(Department department) {
  this.department = department;
 }

 public String getTelphone() {
  return telphone;
 }

 public void setTelphone(String telphone) {
  this.telphone = telphone;
 }
 
 
  
}
*********************************************************8
package com.cn.nnny.ssh.pojo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
//部门实体
@Entity
public class Department {
    private int depid;//编号
    private String depname;//名称
    private String address;//地址
   
    public Department(){
     
    }
@Id
@GeneratedValue
 public int getDepid() {
  return depid;
 }
public void setDepid(int depid) {
  this.depid = depid;
 }
public String getDepname() {
  return depname;
 }
public void setDepname(String depname) {
  this.depname = depname;
 }
public String getAddress() {
  return address;
 }
public void setAddress(String address) {
  this.address = address;
 }
}
************************************************
第六步:加入三大框架的jar包:注意使用myeclipse给你添加包:特别是添加hibernate的支持的时候要选中之前创建的usermanager驱动(就是前面创建备用的那个)
有这几个基本够用:
第七步:完成spring配置文件的编写(其中不括事物等的控制)如:
******************************************************
<?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:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

 <context:annotation-config /><!-- 注解 -->
 <context:component-scan base-package="com.cn.nnny.ssh*.*" /><!-- 扫描包 -->
 <aop:aspectj-autoproxy /><!-- 代理 -->
 
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
 
  <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
  </property>
  <property name="url"
   value="jdbc:sqlserver://localhost:1433;databaseName=hibernate">
  </property>
  <property name="username" value="sa"></property>
  <property name="password" value="sa"></property>
 </bean>
   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan">   
<list>
        <value>com.cn.nnny.usermanager.pojo</value> 
      
      </list>
    </property>
     <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.SQLServerDialect
    </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:annotation-driven transaction-manager="txManager"/> 申明annotation 加载事务驱动 -->
 
 
 <tx:advice id="txAdvice" transaction-manager="txManager">
  <tx:attributes>
   <tx:method name="list*" read-only="true" /><!-- 加事务的方法 -->
   <tx:method name="save*" propagation="REQUIRED"/><!-- 事务的生成特性 -->
   <tx:method name="delete*" propagation="REQUIRED"/><!-- 事务的生成特性 -->
   <tx:method name="update*" propagation="REQUIRED"/><!-- 事务的生成特性 -->
  </tx:attributes>
 </tx:advice>
<aop:config>
  <aop:pointcut id="bussinessService" expression="execution(public * com.cn.nnny.ssh.service..*.*(..))" />
  <aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />
 </aop:config>

</beans>
****************************************************
第八步:部署项目到应用服务器上(我用tomcat)然后启动这样它就会给你自己创建数据库表以及他们之间的关系了.
*********************web。xml文件配置***********************888888
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
 </context-param>
 
 
  <filter>
   <filter-name>struts2</filter-name>
   <filter-class>
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
   </filter-class>
  </filter>
  <filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>*.action</url-pattern>
  </filter-mapping></web-app>
****************************************************
第九步:方向工程(注意我们这里需要把生成的dao放到pojo这个包下  选择dao是选spring那个   至于为什么试试就知道了)对于不会反向工程的自己查查很简单的附个图:
对了:有的时候myeclipse不给你选择
那么你需要修改spring配置文件中
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      
改成:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
改后下面会报错 删除就可以了  等反向工程做完在改回来就行了
然后完成反向工程:
最后一步:
回到myeclipse中提取接口:然后把相应的类移到相应的包中就ok
(不要说不会用myeclipse自动提取接口哦!!)
************************************************************
最后附上action、service 、dao层的注解:
哦忘了;在dao实现类中腰加这么一句:
@Autowired
 public void setSessionFactoryOverride(SessionFactory sessionFactory) {
  super.setSessionFactory(sessionFactory);
 }
***********************action中**********************************
package com.cn.nnny.ssh.action;
import java.util.List;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.cn.nnny.ssh.pojo.Department;
import com.cn.nnny.ssh.service.IDepartmentService;
import com.cn.nnny.ssh.vo.DepartmentVo;
import com.opensymphony.xwork2.ActionSupport;
@Controller
@Scope(value = "prototype")
@Namespace(value = "/department")
public class DepartmenAction extends ActionSupport {
 
 @Autowired
 private IDepartmentService service;//拿到服务
 @Autowired
 private DepartmentVo departmentVo;
 
 private List<Department> departmentlist;
 
   
 @Action(value = "index", results = {
   @Result(name = "success", location = "/WEB-INF/jsp/department/index.jsp"),
   @Result(name = "errors", location = "/WEB-INF/jsp/department/index.jsp") })
 public String execute() throws Exception {
  return super.execute();
 }
 
 
 @Action(value = "save", results = {
   @Result(name = "success", location = "/WEB-INF/jsp/department/index.jsp"),
   @Result(name = "errors", location = "/WEB-INF/jsp/department/index.jsp") })
 public String save() throws Exception {
  System.out.println("wwwwwwwwwwwwwwwwwwwwwwwwww");
  service.save(departmentVo);
  return super.execute();
 }
 
 
 @Action(value = "list", results = {
   @Result(name = "success", location = "/WEB-INF/jsp/department/index.jsp"),
   @Result(name = "errors", location = "/WEB-INF/jsp/department/index.jsp") })
 public String list() throws Exception {
  
  departmentlist=service.list();
  return super.execute();
 }
 
 

 public DepartmentVo getDepartmentVo() {//必须有set方法  才能收集值
  return departmentVo;
 }
public void setDepartmentVo(DepartmentVo departmentVo) {
  this.departmentVo = departmentVo;
 }

 public List<Department> getDepartmentlist() {
  return departmentlist;
 }

 public void setDepartmentlist(List<Department> departmentlist) {
  this.departmentlist = departmentlist;
 }

 
 
}
****************dao中**************只附关键的*********88
@Repository
public class DepartmentDAO extends HibernateDaoSupport implements IDepartmentDAO {
 
 private static final Logger log = LoggerFactory.getLogger(DepartmentDAO.class);
 protected void initDao() {
  // do nothing
 }
/* (non-Javadoc)
  * @see com.cn.nnny.ssh.dao.IDepartmentDAO#save(com.cn.nnny.ssh.pojo.Department)
  */
 @Autowired
 public void setSessionFactoryOverride(SessionFactory sessionFactory) {
  super.setSessionFactory(sessionFactory);
 }
 
 public void save(Department transientInstance) {
  log.debug("saving Department instance");
  try {
   getHibernateTemplate().save(transientInstance);
   log.debug("save successful");
  } catch (RuntimeException re) {
   log.error("save failed", re);
   throw re;
  }
 }
*********************service中************************************
package com.cn.nnny.ssh.service.impl;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cn.nnny.ssh.dao.IDepartmentDAO;
import com.cn.nnny.ssh.pojo.Department;
import com.cn.nnny.ssh.service.IDepartmentService;
import com.cn.nnny.ssh.vo.DepartmentVo;
import com.sun.org.apache.commons.beanutils.BeanUtils;
@Service
public class DepartmentService implements IDepartmentService {
 
 @Autowired
 private IDepartmentDAO departmentDAO;

 public boolean save(DepartmentVo departmentVo){
  boolean ok=true;
  Department department=new Department();
  try {
   BeanUtils.copyProperties(department, departmentVo);//值复制对应名字的
  } catch (IllegalAccessException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (InvocationTargetException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  departmentDAO.save(department);
  System.out.println(departmentVo.getDepname());
  return ok;
  
 }
 
 public List<Department> list(){
  
  List<Department> departmentlist=departmentDAO.findAll();
  
    return departmentlist;
  
 }
}
************************ok结束**********************************

转载于:https://blog.51cto.com/talentb/507068

struts2+hibernate3.3+spring3.0 实现零配置相关推荐

  1. ppst技术视频——spring3.0 注解定时任务配置

    ppst 视频-- spring3.0 注解定时任务配置:请访问ppst 技术视频分享平台 , www.ppst.cc,上面有最新的技术视频 1.在spring的配置文件中加入配置 <conte ...

  2. Java Web笔记之Struts2.1 +Hibernate3.3 +Spring3.0

    2019独角兽企业重金招聘Python工程师标准>>> 1.Struts2 1.1.了解Struts2 Struts2是基于MVC设计模式的Java Web框架技术之一,按照MVC设 ...

  3. 基于james3.0 的邮件系统(struts2.3.2 +spring3.0.1+jpa(hibernate3.6.5)实现)b/s模式--java邮件系统...

    2019独角兽企业重金招聘Python工程师标准>>> 米林(ljg)邮箱使用说明 java邮件系统 下载地址一 https://code.google.com/p/java-mai ...

  4. struts2.1.8+hibernate3.2+Spring3.0+ExtJs4.2+MySql+WebSocket 更新

    软件更名MyDesktop 增加在线和隐身两个用户状态 增加屏蔽群消息功能 增加修改密码功能 增加主界面加载进度条: 增加解散群恢复群功能: 增加了禁用和启用用户: 增加聊天窗口,管理中心,页签的右键 ...

  5. 简述Struts2 Convention零配置

    从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该 ...

  6. SSH (Struts2+Spring3.0+Hibernate3)框架(二) 框架的配置

    一.准备工作: 1. JDK -> jdk1.6.0_17 安装(环境变量配置): JAVA_HOME = C:\ jdk1.6.0_17; PATH = %JAVA_HOME%\bin; %J ...

  7. Myeclipse10下搭建SSH框架(图解)Struts2.1+Spring3.0+Hibernate3.3

    一.建立一个Web Project  ①点击File→New→Web Project 新建一个Web工程.     ②在Project Name中输入项目名ssh,在J2EE Specificatio ...

  8. Spring3, Hibernate3.6与Proxool连接池配置

    为什么80%的码农都做不了架构师?>>>    Proxool连接池Spring3.1Hibernate3.6 鉴于Spring3.0不采用Servlet启动,改用listener, ...

  9. struts2 零配置

    一.新建一个web项目,命名为:struts2 二.导入strut2所需的jar包 所需jar下载:http://pan.baidu.com/s/1dDxP4Z3 三.配置struts2的启动文件,在 ...

最新文章

  1. CodeGen编写自定义表达式标记
  2. 推荐10个百度,阿里,美团系公众号,第2个化学专业,半路出家!
  3. 钱趣多风控新举措:源头选择与物理隔离
  4. 从技术角度讨论微服务
  5. Manacher 算法模板
  6. 腾讯云安全专家 Killer 告诉你,企业上云怎么做更安全
  7. 高效的企业测试-工作流和代码质量(4/6)
  8. 带你用Python玩转PPT
  9. python django开发新闻聚合检索推荐_Django查询以检索符合相关选项卡上聚合条件的行...
  10. 23 android多线程
  11. vmware之VMware Remote Console (VMRC) SDK(一)
  12. DB9串口定义及含义(全)
  13. 2G维码器-以及4G维码器--基于合宙air202 air720sl luat
  14. 20170627——【肿瘤】肿瘤的异质性
  15. JDK1.8帮助文档 chm格式中英文
  16. 4.前端注册表单验证 表单回填
  17. 那些曾经被央视拿来跟韩寒作对比的三好学生们,十年之后,再看看(转)
  18. matlab中的spline,在MATLAB中与spline(x,y,xi)插值效果相同的命令是( )
  19. ChatGPT教程之 01 什么是ChatGPT革命性的对话生成新工具
  20. 【Python_PyQtGraph 学习笔记(五)】基于PyQtGraph和GraphicsLayoutWidget动态绘图并实现窗口模式,且保留全部绘图信息

热门文章

  1. MATLAB xlswrite 写数据 到 Excel文件
  2. mysql的远程服务开启_mysql开启远程服务
  3. java撕裂_屏幕撕裂与卡顿分析
  4. 河北机电职业技术学院计算机分数线,河北机电职业技术学院历年分数线 2021河北机电职业技术学院录取分数线...
  5. 传说之下音乐计算机版,传说之下同人音乐
  6. Shape Correspondence and Functional Maps
  7. 文本数据标注工具doccano【介绍最详细的一遍文章】
  8. 安利几个优质nlp开源项目
  9. python中输出某段文字_Python如何输出字符串中字符出现的个数
  10. Typecho安装后后台界面和文章链接均为404错误的解决方法