spring+struts+hibernate 搭建项目

记录一下spring+struts+hibernate搭建的项目。

目录结构:

spring+struts+hibernate 搭建项目

pom.xml:

  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>Spring_Struts_Hibernate_maven</groupId>
  5   <artifactId>SS2HM</artifactId>
  6   <packaging>war</packaging>
  7   <version>0.0.1-SNAPSHOT</version>
  8   <name>SS2HM Maven Webapp</name>
  9   <url>http://maven.apache.org</url>
 10   <properties>
 11         <java.version>1.8</java.version>
 12         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 13         <junit.version>4.12</junit.version>
 14         <ch.qos.logback>1.1.7</ch.qos.logback>
 15         <fastjson_version>1.2.37</fastjson_version>
 16         <org.springframework.version>4.3.16.RELEASE</org.springframework.version>
 17         <org.aspectj.aspectjweaver>1.8.7</org.aspectj.aspectjweaver>
 18         <org.aspectj.aspectjrt>1.8.7</org.aspectj.aspectjrt>
 19         <com.alibaba.druid>1.0.29</com.alibaba.druid>
 20         <javax.servlet>3.1.0</javax.servlet>
 21         <org.apache.maven.plugins>2.6</org.apache.maven.plugins>
 22         <struts.version>2.3.20</struts.version>
 23     </properties>
 24   <dependencies>
 25         <!-- 单元测试 -->
 26         <dependency>
 27             <groupId>junit</groupId>
 28             <artifactId>junit</artifactId>
 29             <version>${junit.version}</version>
 30             <scope>test</scope>
 31         </dependency>
 32         <!-- 日志 -->
 33         <dependency>
 34             <groupId>ch.qos.logback</groupId>
 35             <artifactId>logback-classic</artifactId>
 36             <version>${ch.qos.logback}</version>
 37         </dependency>
 38         
 39         <!-- json -->
 40         <dependency>
 41             <groupId>com.alibaba</groupId>
 42             <artifactId>fastjson</artifactId>
 43             <version>${fastjson_version}</version>
 44         </dependency>
 45         <!-- Spring 核心依赖 -->
 46         <dependency>
 47             <groupId>org.springframework</groupId>
 48             <artifactId>spring-core</artifactId>
 49             <version>${org.springframework.version}</version>
 50         </dependency>
 51         <dependency>
 52             <groupId>org.springframework</groupId>
 53             <artifactId>spring-context</artifactId>
 54             <version>${org.springframework.version}</version>
 55         </dependency>
 56         <dependency>
 57             <groupId>org.springframework</groupId>
 58             <artifactId>spring-context-support</artifactId>
 59             <version>${org.springframework.version}</version>
 60         </dependency>
 61         <dependency>
 62             <groupId>org.springframework</groupId>
 63             <artifactId>spring-test</artifactId>
 64             <version>${org.springframework.version}</version>
 65         </dependency>
 66         <dependency>
 67             <groupId>org.springframework</groupId>
 68             <artifactId>spring-jdbc</artifactId>
 69             <version>${org.springframework.version}</version>
 70         </dependency>
 71         <dependency>
 72             <groupId>org.springframework</groupId>
 73             <artifactId>spring-web</artifactId>
 74             <version>${org.springframework.version}</version>
 75         </dependency>
 76         <dependency>
 77             <groupId>org.springframework</groupId>
 78             <artifactId>spring-orm</artifactId>
 79             <version>${org.springframework.version}</version>
 80         </dependency>
 81         <!-- Struts2和Spring整合依赖 -->
 82         <dependency>
 83             <groupId>org.apache.struts</groupId>
 84             <artifactId>struts2-spring-plugin</artifactId>
 85             <version>2.3.20</version>
 86         </dependency>
 87         <dependency>
 88             <groupId>org.apache.struts</groupId>
 89             <artifactId>struts2-json-plugin</artifactId>
 90             <version>2.3.20</version>
 91         </dependency>
 92         <dependency>
 93             <groupId>org.apache.struts</groupId>
 94             <artifactId>struts2-convention-plugin</artifactId>
 95             <version>2.3.20</version>
 96         </dependency>
 97         <dependency>
 98             <groupId>org.apache.struts</groupId>
 99             <artifactId>struts2-core</artifactId>
100             <version>2.3.20</version>
101         </dependency>
102         
103         <!-- hibernate -->
104         <dependency>
105              <groupId>org.hibernate</groupId>
106              <artifactId>hibernate-core</artifactId>
107              <version>4.2.3.Final</version>
108         </dependency>
109         <!-- 代理 -->
110         <dependency>
111             <groupId>org.aspectj</groupId>
112             <artifactId>aspectjrt</artifactId>
113             <version>${org.aspectj.aspectjrt}</version>
114         </dependency>
115         <dependency>
116             <groupId>org.aspectj</groupId>
117             <artifactId>aspectjweaver</artifactId>
118             <version>${org.aspectj.aspectjweaver}</version>
119         </dependency>
120         <!-- alibaba -->
121         <dependency>
122             <groupId>com.alibaba</groupId>
123             <artifactId>druid</artifactId>
124             <version>${com.alibaba.druid}</version>
125         </dependency>
126         <dependency>
127             <groupId>org.mariadb.jdbc</groupId>
128             <artifactId>mariadb-java-client</artifactId>
129             <version>1.5.9</version>
130         </dependency>
131         <!-- servlet -->
132         <dependency>
133             <groupId>javax.servlet</groupId>
134             <artifactId>javax.servlet-api</artifactId>
135             <version>${javax.servlet}</version>
136             <scope>provided</scope>
137         </dependency>
138         <!-- 分页插件 -->
139         <dependency>
140             <groupId>com.github.pagehelper</groupId>
141             <artifactId>pagehelper</artifactId>
142             <version>4.1.4</version>
143         </dependency>
144     </dependencies>
145     <build>
146         <finalName>SS2HM</finalName>
147         <plugins>
148             <plugin>
149                 <groupId>org.apache.maven.plugins</groupId>
150                 <artifactId>maven-compiler-plugin</artifactId>
151                 <version>3.1</version>
152                 <configuration>
153                     <source>${java.version}</source>
154                     <target>${java.version}</target>
155                     <encoding>${project.build.sourceEncoding}</encoding>
156                 </configuration>
157             </plugin>
158             <!-- 打包时跳过单元测试 -->
159             <plugin>
160                 <groupId>org.apache.maven.plugins</groupId>
161                 <artifactId>maven-surefire-plugin</artifactId>
162                 <version>2.19.1</version>
163                 <configuration>
164                     <skipTests>true</skipTests>
165                 </configuration>
166             </plugin>
167             <!-- 不同环境的配置文件选择 -->
168             <plugin>
169                 <groupId>org.apache.maven.plugins</groupId>
170                 <artifactId>maven-resources-plugin</artifactId>
171                 <version>${org.apache.maven.plugins}</version>
172                 <executions>
173                     <execution>
174                         <id>copy-resources</id>
175                         <phase>compile</phase>
176                         <goals>
177                             <goal>copy-resources</goal>
178                         </goals>
179                         <configuration>
180                             <!-- 覆盖原有文件 -->
181                             <overwrite>true</overwrite>
182                             <outputDirectory>${project.build.outputDirectory}</outputDirectory>
183                             <!-- 也可以用下面这样的方式(指定相对url的方式指定outputDirectory) <outputDirectory>target/classes</outputDirectory> -->
184                             <!-- 待处理的资源定义 -->
185                             <resources>
186                                 <resource>
187                                     <!-- 指定resources插件处理哪个目录下的资源文件 -->
188                                     <directory>src/main/resources/${package.environment}</directory>
189                                     <filtering>false</filtering>
190                                 </resource>
191                             </resources>
192                         </configuration>
193                         <inherited></inherited>
194                     </execution>
195                 </executions>
196             </plugin>
197         </plugins>
198         <resources>
199             <resource>
200                 <directory>src/main/java</directory>
201                 <includes>
202                     <include>**/*.properties</include>
203                     <include>**/*.xml</include>
204                 </includes>
205                 <filtering>true</filtering>
206             </resource>
207             <resource>
208                 <directory>src/main/resources</directory>
209                 <includes>
210                     <include>**/*.properties</include>
211                     <include>**/*.xml</include>
212                 </includes>
213                 <filtering>true</filtering>
214             </resource>
215         </resources>
216     </build>
217 </project>

RequestCode:

 1 package org.ss2hm.code;
 2 
 3 import org.apache.struts2.json.annotations.JSON;
 4 
 5 public class RequestCode {
 6 
 7     private Object data;
 8     private int code;
 9     private String msg;
10     
11     public RequestCode() {
12         super();
13     }
14     public RequestCode(Object data, int code, String msg) {
15         super();
16         this.data = data;
17         this.code = code;
18         this.msg = msg;
19     }
20     @JSON
21     public Object getData() {
22         return data;
23     }
24     
25     public void setData(Object data) {
26         this.data = data;
27     }
28     @JSON
29     public int getCode() {
30         return code;
31     }
32     public void setCode(int code) {
33         this.code = code;
34     }
35     @JSON
36     public String getMsg() {
37         return msg;
38     }
39     
40     public void setMsg(String msg) {
41         this.msg = msg;
42     }
43     
44     @Override
45     public String toString() {
46         return "RequestCode [data=" + data + ", code=" + code + ", msg=" + msg + "]";
47     }
48     
49     
50 }

CommonAction:

 1 package org.ss2hm.action;
 2 
 3 import java.util.List;
 4 import java.util.Map;
 5 
 6 import javax.annotation.Resource;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 import org.apache.struts2.ServletActionContext;
11 import org.apache.struts2.convention.annotation.Action;
12 import org.apache.struts2.convention.annotation.Namespace;
13 import org.apache.struts2.convention.annotation.ParentPackage;
14 import org.apache.struts2.convention.annotation.Result;
15 import org.springframework.context.annotation.Scope;
16 import org.springframework.stereotype.Controller;
17 import org.ss2hm.code.RequestCode;
18 import org.ss2hm.model.CityModel;
19 import org.ss2hm.service.CommonService;
20 
21 import com.alibaba.fastjson.JSON;
22 import com.opensymphony.xwork2.ActionSupport;
23 
24 @Controller
25 @Scope("prototype")
26 @ParentPackage("json-default")
27 @Namespace("/")
28 public class CommonAction extends ActionSupport{
29 
30     private static final long serialVersionUID = 1L;
31 
32     @Resource(name = "commonServiceImp")
33     private CommonService commonServiceImp;
34     
35     private RequestCode requestCode;
36     
37     private String data;
38     
39     @SuppressWarnings("unchecked")
40     @Action(value = "common",results = {@Result(name="success",type = "json",params = {"root","requestCode"})})
41     public String getAll() {
42         Map<String, Object> where = (Map<String, Object>) JSON.parse(data);
43         List<CityModel> list = commonServiceImp.getAll(where);
44         requestCode = new RequestCode();
45         requestCode.setCode(1);
46         requestCode.setData(list);
47         requestCode.setMsg(ActionSupport.SUCCESS);
48         setCrossDomain();
49         return ActionSupport.SUCCESS;
50     }
51     
52     public RequestCode getRequestCode() {
53         return requestCode;
54     }
55     
56     public void setRequestCode(RequestCode requestCode) {
57         this.requestCode = requestCode;
58     }
59 
60     public String getData() {
61         return data;
62     }
63 
64     public void setData(String data) {
65         this.data = data;
66     }
67 
68     protected HttpServletRequest getHttpRequest(){
69         return ServletActionContext.getRequest();
70     }
71     
72     protected HttpServletResponse getHttpResponse(){
73         return ServletActionContext.getResponse();
74     }
75 
76     protected void setCrossDomain() {
77         getHttpResponse().setHeader("Access-Control-Allow-Origin", getHttpRequest().getHeader("Origin"));
78         getHttpResponse().setHeader("Access-Control-Allow-Credentials", "true");
79     }
80 
81     
82 }

CommonService:

 1 package org.ss2hm.service;
 2 
 3 import java.util.List;
 4 import java.util.Map;
 5 
 6 import org.ss2hm.model.CityModel;
 7 
 8 public interface CommonService {
 9 
10     public List<CityModel> getAll(Map<String, Object> where);
11 }

CommonServiceImp:

 1 package org.ss2hm.service.imp;
 2 
 3 import java.util.List;
 4 import java.util.Map;
 5 
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Service;
 8 import org.ss2hm.dao.CommonDao;
 9 import org.ss2hm.model.CityModel;
10 import org.ss2hm.service.CommonService;
11 
12 @Service("commonServiceImp")
13 public class CommonServiceImp implements CommonService{
14 
15     @Autowired
16     private CommonDao dao;
17     
18     @Override
19     public List<CityModel> getAll(Map<String, Object> where) {
20         // TODO Auto-generated method stub
21         //业务处理
22         return dao.getAll(where);
23     }
24 
25 }

 

CommonDao:

 1 package org.ss2hm.dao;
 2 
 3 import java.util.List;
 4 import java.util.Map;
 5 
 6 import org.hibernate.Query;
 7 import org.hibernate.SessionFactory;
 8 import org.springframework.beans.factory.annotation.Autowired;
 9 import org.springframework.stereotype.Repository;
10 import org.springframework.transaction.annotation.Transactional;
11 import org.ss2hm.model.CityModel;
12 
13 @Repository
14 public class CommonDao {
15 
16     @Autowired
17     private SessionFactory sessionFactory;
18     
19     @Transactional(readOnly=true)
20     public List<CityModel> getAll(Map<String, Object> where){
21         Query query = sessionFactory.getCurrentSession().createQuery("from CityModel");
22         query.setFirstResult(0);
23         query.setMaxResults(10);
24         List<CityModel> list = query.list();
25         return list;
26     }
27 }

 

CityModel:

 1 package org.ss2hm.model;
 2 
 3 public class CityModel {
 4 
 5     private int id;
 6     private String name;
 7     private String countryCode;
 8     private String district;
 9     private int population;
10     public CityModel() {
11         super();
12     }
13     public int getId() {
14         return id;
15     }
16     public void setId(int id) {
17         this.id = id;
18     }
19     public String getName() {
20         return name;
21     }
22     public void setName(String name) {
23         this.name = name;
24     }
25     public String getCountryCode() {
26         return countryCode;
27     }
28     public void setCountryCode(String countryCode) {
29         this.countryCode = countryCode;
30     }
31     public String getDistrict() {
32         return district;
33     }
34     public void setDistrict(String district) {
35         this.district = district;
36     }
37     public int getPopulation() {
38         return population;
39     }
40     public void setPopulation(int population) {
41         this.population = population;
42     }
43     
44     
45 }

city.hbm.xml:

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 <hibernate-mapping>
 5     <class name="org.ss2hm.model.CityModel" table="city" schema="world">
 6         <id name="id" type="java.lang.Integer">
 7             <column name="ID" />
 8             <generator class="native" />
 9         </id>
10         <property name="name" type="java.lang.String">
11             <column name="name" />
12         </property>
13         <property name="countryCode" type="java.lang.String">
14             <column name="countryCode" />
15         </property>
16         <property name="district" type="java.lang.String">
17             <column name="district" />
18         </property>
19         <property name="population" type="java.lang.Integer">
20             <column name="population" />
21         </property>
22     </class>
23 </hibernate-mapping>

 

————————————————————————————————————————————————end——————————————————————————————————————————————————————

 

上一篇:【Java】Struts2配置默认Action和配置Action的默认处理类


下一篇:pandas 视频讲座 from youtube