首页 > 代码库 > struts2+hibernate+spring注解版框架搭建以及简单测试(方便脑补)

struts2+hibernate+spring注解版框架搭建以及简单测试(方便脑补)

为了之后学习的日子里加深对框架的理解和使用,这里将搭建步奏简单写一下,目的主要是方便以后自己回来脑补;

1:File--->New--->Other--->Maven--->Maven Project--->Next(之后界面如下所示:)

技术分享

--->Next(点击next之后出现如下界面:选择最后一个 maven-archetype-webapp,然后点击next)

技术分享

--->Next(点击next之后出现如下界面,然后选择好组织号,工程号,版本号即可),最后点击Finish

技术分享

2:maven项目创建好之后会报错,这里先在pom.xml中导入jar包,导入方式如下所示:这个pom配置好之后点击保存会将jar包下载到本地仓库,这个学习maven的时候都配置过,这个自己下去脑补吧;注意第304行struts2注解配置;

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  3   <modelVersion>4.0.0</modelVersion>  4   <groupId>com.bie.maven</groupId>  5   <artifactId>ssh_annotation</artifactId>  6   <packaging>war</packaging>  7   <version>0.0.1-SNAPSHOT</version>  8   <name>ssh_annotation Maven Webapp</name>  9   <url>http://maven.apache.org</url> 10    11    12    13    <!-- 配置一下jar包 --> 14    <properties> 15         <java-version>1.8</java-version> 16         <junit-version>4.8.2</junit-version> 17         <org.springframework-version>3.2.4.RELEASE</org.springframework-version> 18         <jackson-mapper-asl-version>1.9.12</jackson-mapper-asl-version> 19         <jackson-core-asl-version>1.9.12</jackson-core-asl-version> 20         <mysql-connector-java-version>5.1.8</mysql-connector-java-version> 21          22         <!--  23         <org.mybatis-version>3.2.7</org.mybatis-version> 24         <org.mybatis-spring-version>1.2.2</org.mybatis-spring-version> 25         --> 26          27         <opensymphony-version>2.4.2</opensymphony-version> 28         <freemarker-version>2.3.9</freemarker-version> 29         <c3p0-version>0.9.1.2</c3p0-version> 30         <commons-collections-version>1.0</commons-collections-version> 31         <commons-fileupload-version>1.2.2</commons-fileupload-version> 32         <org.apache.commons-version>3.1</org.apache.commons-version> 33         <commons-codec-version>1.6</commons-codec-version> 34         <dom4j-version>1.6.1</dom4j-version> 35         <javax.servlet-version>1.2</javax.servlet-version> 36         <aspectjweaver-version>1.6.6</aspectjweaver-version> 37         <slf4j-log4j12-version>1.6.6</slf4j-log4j12-version> 38         <log4j-version>1.2.16</log4j-version> 39         <javax.servlet-jsp-version>2.0</javax.servlet-jsp-version> 40         <cglib-version>2.2.2</cglib-version> 41         <slf4j-api-version>1.6.6</slf4j-api-version> 42         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 43     </properties> 44     <dependencies> 45         <!-- 配置 junit4.8.2 --> 46         <dependency> 47             <groupId>junit</groupId> 48             <artifactId>junit</artifactId> 49             <version>${junit-version}</version> 50             <type>jar</type> 51         </dependency> 52         <!-- 配置 spring --> 53         <dependency> 54             <groupId>org.springframework</groupId> 55             <artifactId>spring-core</artifactId> 56             <version>${org.springframework-version}</version> 57         </dependency> 58         <dependency> 59             <groupId>org.springframework</groupId> 60             <artifactId>spring-web</artifactId> 61             <version>${org.springframework-version}</version> 62         </dependency> 63         <dependency> 64             <groupId>org.springframework</groupId> 65             <artifactId>spring-test</artifactId> 66             <version>${org.springframework-version}</version> 67         </dependency> 68         <dependency> 69             <groupId>org.springframework</groupId> 70             <artifactId>spring-aspects</artifactId> 71             <version>${org.springframework-version}</version> 72         </dependency> 73         <dependency> 74             <groupId>org.springframework</groupId> 75             <artifactId>spring-context-support</artifactId> 76             <version>${org.springframework-version}</version> 77         </dependency> 78         <dependency> 79             <groupId>org.springframework</groupId> 80             <artifactId>spring-web</artifactId> 81             <version>${org.springframework-version}</version> 82         </dependency> 83  84         <dependency> 85             <groupId>org.springframework</groupId> 86             <artifactId>spring-webmvc</artifactId> 87             <version>${org.springframework-version}</version> 88         </dependency> 89         <dependency> 90             <groupId>org.springframework</groupId> 91             <artifactId>spring-jdbc</artifactId> 92             <version>${org.springframework-version}</version> 93         </dependency> 94         <dependency> 95             <groupId>org.springframework</groupId> 96             <artifactId>spring-tx</artifactId> 97             <version>${org.springframework-version}</version> 98         </dependency> 99         <dependency>100             <groupId>org.springframework</groupId>101             <artifactId>spring-instrument</artifactId>102             <version>${org.springframework-version}</version>103         </dependency>104         <dependency>105             <groupId>org.springframework</groupId>106             <artifactId>spring-instrument-tomcat</artifactId>107             <version>${org.springframework-version}</version>108         </dependency>109         110         <dependency>  111             <groupId>org.springframework</groupId>  112             <artifactId>spring-orm</artifactId>  113             <version>${org.springframework-version}</version>  114         </dependency>115         <!--springmvc 转换为 Json -->116         <dependency>117             <groupId>org.codehaus.jackson</groupId>118             <artifactId>jackson-mapper-asl</artifactId>119             <version>${jackson-mapper-asl-version}</version>120         </dependency>121         <dependency>122             <groupId>org.codehaus.jackson</groupId>123             <artifactId>jackson-core-asl</artifactId>124             <version>${jackson-core-asl-version}</version>125         </dependency>126         <!--配置 freemarker -->127         <dependency>128             <groupId>freemarker</groupId>129             <artifactId>freemarker</artifactId>130             <version>${freemarker-version}</version>131         </dependency>132         <!-- 配置 mybatis-spring ssh这里可以不配置mybatis-->133         <!--134         <dependency>135             <groupId>org.mybatis</groupId>136             <artifactId>mybatis</artifactId>137             <version>${org.mybatis-version}</version>138         </dependency>139         <dependency>140             <groupId>org.mybatis</groupId>141             <artifactId>mybatis-spring</artifactId>142             <version>${org.mybatis-spring-version}</version>143         </dependency>144         -->145         146         <!-- jdbc driver -->147         <dependency>148             <groupId>mysql</groupId>149             <artifactId>mysql-connector-java</artifactId>150             <version>${mysql-connector-java-version}</version>151         </dependency>152         <!-- connect pool with c3p0 -->153         <dependency>154             <groupId>c3p0</groupId>155             <artifactId>c3p0</artifactId>156             <version>${c3p0-version}</version>157         </dependency>158         <!-- apache commons jar -->159         <!-- <dependency>160             <groupId>commons-collections</groupId>161             <artifactId>commons-collections</artifactId>162             <version>${commons-collections-version}</version>163             <scope>provided</scope>164         </dependency> -->165 166         <dependency>167             <groupId>commons-fileupload</groupId>168             <artifactId>commons-fileupload</artifactId>169             <version>${commons-fileupload-version}</version>170         </dependency>171 172         <dependency>173             <groupId>org.apache.commons</groupId>174             <artifactId>commons-lang3</artifactId>175             <version>${org.apache.commons-version}</version>176         </dependency>177 178         <dependency>179             <groupId>commons-codec</groupId>180             <artifactId>commons-codec</artifactId>181             <version>${commons-codec-version}</version>182         </dependency>183         <!-- analyze xml use dom4j -->184         <dependency>185             <groupId>dom4j</groupId>186             <artifactId>dom4j</artifactId>187             <version>${dom4j-version}</version>188         </dependency>189         <!-- about servlet -->190         <dependency>191             <groupId>javax.servlet</groupId>192             <artifactId>jstl</artifactId>193             <version>${javax.servlet-version}</version>194         </dependency>195 196 197         <dependency>198             <groupId>taglibs</groupId>199             <artifactId>standard</artifactId>200             <version>1.1.2</version>201         </dependency>202         <dependency>203             <groupId>javax.servlet</groupId>204             <artifactId>javax.servlet-api</artifactId>205             <version>3.0.1</version>206             <scope>provided</scope>207         </dependency>208         <dependency>209             <groupId>org.slf4j</groupId>210             <artifactId>slf4j-log4j12</artifactId>211             <version>1.6.6</version>212             <scope>provided</scope>213         </dependency>214         <dependency>215             <groupId>log4j</groupId>216             <artifactId>log4j</artifactId>217             <version>1.2.17</version>218         </dependency>219 220         <dependency>221             <groupId>org.slf4j</groupId>222             <artifactId>slf4j-api</artifactId>223             <version>1.6.6</version>224         </dependency>225         <dependency>226             <groupId>javax.activation</groupId>227             <artifactId>activation</artifactId>228             <version>1.1.1</version>229         </dependency>230 231         <!-- memcached -->232         <dependency>233             <groupId>com.google.code.maven-play-plugin.spy</groupId>234             <artifactId>memcached</artifactId>235             <version>2.4.2</version>236         </dependency>237         <dependency>238             <groupId>cglib</groupId>239             <artifactId>cglib</artifactId>240             <version>${cglib-version}</version>241         </dependency>242         <dependency>243             <groupId>net.fckeditor</groupId>244             <artifactId>java-core</artifactId>245             <version>2.6</version>246         </dependency>247         <dependency>248             <groupId>org.json</groupId>249             <artifactId>json</artifactId>250             <version>20131018</version>251         </dependency>252         <!-- excel鎿嶄綔 -->253         <!-- <dependency> <groupId>cn.liu.jxl</groupId> <artifactId>jxl</artifactId> 254             <version>2.6.10</version> </dependency> -->255 256         <dependency>257             <groupId>com.sun.jersey</groupId>258             <artifactId>jersey-client</artifactId>259             <version>1.18.1</version>260         </dependency>261         <dependency>262             <groupId>jsptags</groupId>263             <artifactId>pager-taglib</artifactId>264             <version>2.0</version>265         </dependency>266 267         <dependency>268             <groupId>net.sourceforge.jexcelapi</groupId>269             <artifactId>jxl</artifactId>270             <version>2.6.10</version>271         </dependency>272 273         <dependency>274             <groupId>opensymphony</groupId>275             <artifactId>oscache</artifactId>276             <version>2.4.1</version>277         </dependency>278         <dependency>279             <groupId>javax.servlet</groupId>280             <artifactId>servlet-api</artifactId>281             <version>2.4</version>282             <scope>provided</scope>283         </dependency>284         285         <dependency>286             <groupId>org.apache.struts</groupId>287             <artifactId>struts2-core</artifactId>288             <version>2.3.8</version>289             <exclusions>290                 <exclusion>291                     <artifactId>javassist</artifactId>292                         <groupId>javassist</groupId>293                 </exclusion>294             </exclusions>295         </dependency>296         297         <!-- spring和struts之间的联系 -->298         <dependency>  299             <groupId>org.apache.struts</groupId>  300             <artifactId>struts2-spring-plugin</artifactId>  301             <version>2.3.8</version>  302         </dependency>303         304         <!-- struts2的注解 -->305         <dependency>306             <groupId>org.apache.struts</groupId>307             <artifactId>struts2-convention-plugin</artifactId>308         <version>2.3.8</version>309         </dependency>310 311     312     <dependency>313         <groupId>org.hibernate</groupId>314         <artifactId>hibernate-core</artifactId>315         <version>4.1.7.Final</version>316     </dependency>317     <dependency>318         <groupId>org.hibernate</groupId>319         <artifactId>hibernate-ehcache</artifactId>320         <version>4.1.7.Final</version>321     </dependency>322     <dependency>323         <groupId>displaytag</groupId>324         <artifactId>displaytag</artifactId>325         <version>1.2</version>326     </dependency>327     328   </dependencies>329   330   331 332   <build>333     <finalName>ssh_annotation</finalName>334   </build>335   336 </project>

3:web.xml里面的dtd默认是2.3的,不过2.3的不支持el表达式,所以这里修改为2.5的。配置如下所示:

1 <?xml version="1.0" encoding="UTF-8"?>2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">3   <display-name>Archetype Created Web Application</display-name>4   5 6   <display-name>Archetype Created Web Application</display-name>7   8 </web-app>

4:项目配置好上面的,接下来开始写代码,首先从web.xml的配置开始,比如struts2的核心过滤器,spring核心配置文件,监听器;

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 3   <display-name>Archetype Created Web Application</display-name> 4    5  6   <display-name>Archetype Created Web Application</display-name> 7    8   <!-- 配置spring的监听器 ,加载本地的springp配置文件--> 9   <context-param>10       <param-name>contextConfigLocation</param-name>11       <param-value>classpath:bean-*.xml</param-value>12   </context-param>13   14   <!-- 15       spring的监听器 spring-web-3.2.4.RELEASE.jar ==》org.springframework16           ==》context ==》ContextLoaderListener17   -->18   <listener>19       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>20   </listener>21   22   23   <!-- 24       struts2的核心过滤器 25           1:filter-class  ==》 struts2-core-2.3.8.jar    ==》org.apache.struts2    26               ==》 dispatcher ==》 ng ==》filter ==》StrutsPrepareAndExecuteFilter27   -->28   <filter>29       <filter-name>struts2Filter</filter-name>30       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>31   </filter>32   <filter-mapping>33       <filter-name>struts2Filter</filter-name>34       <url-pattern>/*</url-pattern>35   </filter-mapping>36   37   38   39 </web-app>

5:配置好之后就需要配置一下maven标准格式:在src/main下面创建一个java的文件夹,在src/下面创建一个test,在test下面创建java文件夹,配置如下所示:

技术分享

6:将spring的配置文件bean-common.xml配置文件放到src/main/resources下面即可(src/main/resources也是根目录):配置如下所示:

  1 <?xml version="1.0" encoding="UTF-8"?>    2 <beans     3     xmlns="http://www.springframework.org/schema/beans"     4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     5     xmlns:context="http://www.springframework.org/schema/context"    6     xmlns:tx="http://www.springframework.org/schema/tx"    7     xmlns:aop="http://www.springframework.org/schema/aop"    8     xsi:schemaLocation="    9     http://www.springframework.org/schema/beans    10     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    11     http://www.springframework.org/schema/context    12     http://www.springframework.org/schema/context/spring-context-3.0.xsd   13     http://www.springframework.org/schema/tx    14     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    15     http://www.springframework.org/schema/aop    16     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   17     ">   18      19     <!-- 使用注解annotation配置项目 --> 20     <context:annotation-config></context:annotation-config> 21      22     <!-- 使用扫描组件,扫描注解 ,自动扫描某某包,扫描所有的包--> 23     <context:component-scan base-package="com.bie"></context:component-scan>     24      25     <!-- 使用spring管理struts,使用sring管理hibernate --> 26     <!--  27         配置数据源: 28              1:spring-jdbc-3.2.4.RELEASE.jar==》org.springframework.jdbc  29                  ==》datasource  30                  ==》DriverManagerDataSource 31      --> 32     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 33         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 34         <property name="url" value="jdbc:mysql:///crm_ssh?characterEncoding=UTF-8"></property> 35         <property name="username" value="root"></property> 36         <property name="password" value="123456"></property> 37     </bean> 38      39     <!--  40         配置数据源生成hibernate的sessionFactory  41             1:spring-orm-3.2.4.RELEASE.jar ==》org.springframework.orm ==》hibernate4  42                 ==LocalSessionFactoryBean 43     --> 44     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 45         <!-- 引入数据源 ,hibernate可以操作数据库,使用某个数据源生成sessionFactory--> 46         <property name="dataSource" ref="dataSource"></property> 47          48         <!-- 配置hibernate的相关配置,方言,显示sql语句等等  --> 49         <property name="hibernateProperties"> 50             <props> 51                 <!--  52                       mysql的方言: 53                       hibernate-core-4.1.7.Final.jar      54                   --> 55                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 56                 <!-- 打印sql语句 --> 57                 <prop key="hibernate.show_sql">true</prop> 58             </props> 59         </property> 60          61          62         <!-- 之前开发使用的是映射文件,现在使用的是注解配置 --> 63         <property name="annotatedClasses"> 64             <!-- 这是一个集合,使用list集合进行赋值 --> 65             <list> 66                 <!-- 使用注解的类都需要加到这里 --> 67                 <value>com.bie.po.User</value> 68                 <value>com.bie.po.Dept</value> 69             </list> 70         </property> 71     </bean> 72      73     <!--  74         spring采用事务管理hibernate的事务 :事务管理器:transactionManager 75             1:spring-orm-3.2.4.RELEASE.jar ==》org.springframework.orm ==》 76                 hibernate4==》HibernateTransactionManager 77     --> 78     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 79         <!-- spring要管理事务,那么spring要对hibernate拥有处置权 --> 80         <property name="sessionFactory" ref="sessionFactory"></property> 81     </bean> 82      83      84     <!-- 在配置切面 之上配置事务管理的通知: 85           1:事务管理的通知要使用上面的事务管理:transactionManager 86       --> 87       <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> 88           <tx:attributes> 89               <!-- 配置事务的传播特性 --> 90               <tx:method name="find*" read-only="true"/> 91               <tx:method name="get*" read-only="true"/> 92               <tx:method name="query*" read-only="true"/> 93                94               <!-- required:本操作内如果有事务,则使用事务,如果没有,则开启新的事务 --> 95               <tx:method name="add*" propagation="REQUIRED"/> 96               <tx:method name="update*" propagation="REQUIRED"/> 97               <tx:method name="delete*" propagation="REQUIRED"/> 98           </tx:attributes> 99       </tx:advice> 100     101      102     <!-- 103       配置切面,使用切面来管理事务104           1:第一个※代表public105           2:第二个※代表类106           3:第三个※代表方法107           4:第三个※(..)里面的..代表任意参数108    -->109    <aop:config>110            <!-- 切入点 -->111            <aop:pointcut expression="execution(* com.bie.service.impl.*.*(..))" id="transactionPointcut"></aop:pointcut>112            113            <!-- 114           切入点应用到什么通知:115               1:advice-ref:应用什么通知116               2:pointcut-ref在那个切入点应用这个通知;    117           -->118         <aop:advisor advice-ref="transactionAdvice" pointcut-ref="transactionPointcut"/>119    </aop:config>120    121    122 </beans>      

7:配置好之后开始写实体类po下面的实体类;(注解方式配置实体类,配置如下所示):

 

 1 package com.bie.po; 2  3 import javax.persistence.Column; 4 import javax.persistence.Entity; 5 import javax.persistence.GeneratedValue; 6 import javax.persistence.GenerationType; 7 import javax.persistence.Id; 8 import javax.persistence.Table; 9 10 /** 11 * @author 作者:别先生12 * @version 创建时间:2017年6月8日 上午11:55:43 13 * 类说明 14 *     1:使用注解完成实体类的配置15 *     2:用户对应数据库的t_user表16 */17 @Entity18 @Table(name = "t_user")19 public class User {20 21     private Integer id;//主键编号22     private String name;//姓名23     private String account;//账号24     private String password;//密码25     26     //设置主键,然后设置主键自增27     @Id28     @GeneratedValue(strategy=GenerationType.AUTO)29     @Column(name="t_userId")30     public Integer getId() {31         return id;32     }33     public void setId(Integer id) {34         this.id = id;35     }36     37     @Column(name="t_userName")38     public String getName() {39         return name;40     }41     public void setName(String name) {42         this.name = name;43     }44     45     @Column(name="t_userAccount")46     public String getAccount() {47         return account;48     }49     public void setAccount(String account) {50         this.account = account;51     }52     53     @Column(name="t_userPwd")54     public String getPassword() {55         return password;56     }57     public void setPassword(String password) {58         this.password = password;59     }60     61 }

 

8:配置一下struts2的配置文件,配置如下所示:

 1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4     "http://struts.apache.org/dtds/struts-2.0.dtd"> 5  6 <struts> 7     <!-- struts2-core-2.3.8.jar ==》org.apache.struts2==》default.properties --> 8      9     <!-- 表示struts2的action由spring来生成 -->10     <constant name="struts.objectFactory" value="http://www.mamicode.com/spring"></constant>11   12 13 </struts>

9:配置好上面的验证一下,将项目加载到tomcat中,然后启动tomcat看看是否报错,我这里不报错,就不做多叙述了;

至此,ssh注解版框架就搭建成功,接下来就可以进行简单测试了......

10:测试的话,先写一个index.jsp页面,页面代码如下所示:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  4 <%@ taglib prefix="s" uri="/struts-tags"%>    5 <% 6     //获取绝对路径路径 :路径path 7     String path = request.getContextPath(); 8     //基本路径:http://localhost:8080/ 9     String basePath = request.getScheme() +"://" 10         + request.getServerName() 11         +":" +request.getServerPort() + path + "/";12     //保存到session中13     session.setAttribute("basePath", basePath);14 %>    15     16 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">17 <html>18 <head>19 <base href="http://www.mamicode.com/" />20 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">21 <title>测试crm</title>22 </head>23 <body>24 25 <a href="http://www.mamicode.com/${basePath }user/queryById.action?userId=1">点击查看个人信息</a>26 <h1>个人信息如下所示:</h1>27 <ul>28     <li>用户编号:${user.userId }</li>29     <li>用户姓名:${user.userName }</li>30     <li>用户账号:${user.userAccount }</li>31     <li>用户密码:${user.userPwd }</li>32     <li>用户部门:${user.dept.deptName }</li>33 </ul>34 35 36 37 </body>38 </html>

 11:页面点击超链接之后访问到struts2的action层,下面先给出action层的代码;

 1 package com.bie.action; 2  3 import org.apache.struts2.convention.annotation.Action; 4 import org.apache.struts2.convention.annotation.Namespace; 5 import org.apache.struts2.convention.annotation.ParentPackage; 6 import org.apache.struts2.convention.annotation.Result; 7 import org.springframework.beans.factory.annotation.Autowired; 8  9 import com.bie.po.User;10 import com.bie.service.UserInfoService;11 import com.opensymphony.xwork2.ActionContext;12 import com.opensymphony.xwork2.ActionSupport;13 14 /** 15 * @author 作者:别先生16 * @version 创建时间:2017年6月8日 下午4:27:30 17 * 类说明 18 */19 @ParentPackage("struts-default")20 @Namespace("/user")21 public class UserAction extends ActionSupport{22 23     private static final long serialVersionUID = 1L;24     25     //自动注入到ioc容器中26     @Autowired27     private UserInfoService userInfoService;    28     29     private int id;30     public void setId(int id) {31         this.id = id;32     }33     public int getId() {34         return id;35     }36     37     @Action(38         value = "http://www.mamicode.com/queryById",39         results = {40             @Result(name="success",location="/index.jsp")    41         }42     )43     public String queryById(){44         45         //调用service层的方法进行查询46         User user = userInfoService.queryById(id);47         48         //将查询的信息保存到session中49         ActionContext.getContext().getSession().put("user", user);50         51         return "success";52     }53     54     55 }

12:数据传输到action中,action对数据进行封装之后传到service层进行数据业务逻辑处理,这里写一下service层的代码;对service层的代码和dao层的代码先写接口,然后再写实现接口的类;

 1 package com.bie.service; 2  3 import com.bie.po.User; 4  5 /**  6 * @author 作者:别先生 7 * @version 创建时间:2017年6月8日 下午3:43:03  8 * 类说明  9 */10 11 public interface UserInfoService {12 13     /***14      * 根据用户编号查询15      * @param id16      * @return17      */18     public User queryById(int id);19 }
 1 package com.bie.service.impl; 2  3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Service; 5  6 import com.bie.dao.UserInfoDao; 7 import com.bie.po.User; 8 import com.bie.service.UserInfoService; 9 10 /** 11 * @author 作者:别先生12 * @version 创建时间:2017年6月8日 下午3:43:13 13 * 类说明 14 */15 @Service16 public class UserInfoServiceImpl implements UserInfoService{17 18     //自动注入到ioc容器中19     @Autowired20     private UserInfoDao userInfoDao;21     22     public User queryById(int id) {23         24         //直接调用dao层的方法查询之后返回到action层25         return userInfoDao.queryById(id);26     }27 28     29 }

13:数据传输到dao层之后开始和数据库里的数据表进行交互和操作;

 1 package com.bie.dao; 2  3 import com.bie.po.User; 4  5 /**  6 * @author 作者:别先生 7 * @version 创建时间:2017年6月8日 下午4:35:20  8 * 类说明  9 */10 11 public interface UserInfoDao {12 13     /**14      * 根据用户编号进行查询15      * @param userId16      * @return17      */18     public User queryById(int userId);19 }
 1 package com.bie.dao.impl; 2  3 import org.hibernate.SessionFactory; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.stereotype.Repository; 6  7 import com.bie.dao.UserInfoDao; 8 import com.bie.po.User; 9 10 /** 11 * @author 作者:别先生12 * @version 创建时间:2017年6月8日 下午4:35:46 13 * 类说明 14 */15 @Repository16 public class UserInfoDaoImpl implements UserInfoDao{17 18     //自动注入到ioc容器中19     @Autowired20     private SessionFactory sessionFactory;21     22     public User queryById(int id) {23         24         User user = (User) sessionFactory.getCurrentSession().get(User.class, id);25         26         return user;27     }28 29 }

演示效果如下所示:

技术分享

技术分享

ssh注解版框架搭建先到这吧,以后有机会再深学吧。时间过的挺快的,马上就该出去找工作了......

struts2+hibernate+spring注解版框架搭建以及简单测试(方便脑补)