RESTful风格的SSM框架搭建

1 使用idea编辑工具,maven项目构建工具搭建RESTful风格的java项目

 

2 进行项目配置

2.1 pom文件依赖

  1 "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   4.0.0
  4   com.huitong
  5   friend
  6   war
  7   1.0-SNAPSHOT
  8   friend Maven Webapp
  9   http://maven.apache.org
 10 
 11 
 12   
 13     UTF-8
 14     UTF-8
 15     
 16     4.3.0.RELEASE
 17 
 18     
 19     3.4.4
 20 
 21     
 22     5.1.40
 23 
 24     
 25     2.7
 26     1.2
 27 
 28     
 29     2.8.5
 30 
 31     
 32     4.2.3.RELEASE
 33 
 34     root
 35     123456
 36 
 37 
 38   
 39   
 40     
 41       junit
 42       junit
 43       4.11
 44       test
 45     
 46 
 47 
 48     
 49     
 50       jstl
 51       jstl
 52       1.2
 53     
 54 
 55     
 56     
 57     
 58       javax
 59       javaee-api
 60       7.0
 61     
 62 
 63 
 64     
 65     
 66     
 67       org.springframework
 68       spring-web
 69       ${spring.version}
 70     
 71 
 72     
 73     
 74       org.springframework
 75       spring-webmvc
 76       ${spring.version}
 77     
 78     
 79 
 80     
 81     
 82     
 83       org.springframework
 84       spring-beans
 85       ${spring.version}
 86     
 87 
 88     
 89     
 90       org.springframework
 91       spring-context
 92       ${spring.version}
 93     
 94 
 95     
 96     
 97       org.springframework
 98       spring-core
 99       ${spring.version}
100     
101     
102 
103     
104     
105     
106       org.ow2.asm
107       asm
108       5.2
109     
110 
111     
112     
113       cglib
114       cglib
115       3.2.5
116     
117 
118     
119     
120       org.mybatis
121       mybatis
122       ${mybatis.version}
123     
124     
125 
126     
127     
128     
129       org.mybatis
130       mybatis-spring
131       1.3.1
132     
133     
134 
135 
136     
137     
138     
139       mysql
140       mysql-connector-java
141       ${mysql-driver.version}
142     
143 
144     
145     
146       com.mchange
147       c3p0
148       0.9.5.2
149     
150     
151 
152     
153     
154     
155       org.springframework
156       spring-jdbc
157       ${spring.version}
158     
159 
160     
161     
162       org.springframework
163       spring-tx
164       ${spring.version}
165     
166     
167 
168     
169     
170     
171       aopalliance
172       aopalliance
173       1.0
174     
175 
176     
177     
178       org.aspectj
179       aspectjrt
180       1.8.10
181     
182 
183     
184     
185       org.aspectj
186       aspectjweaver
187       1.8.10
188     
189 
190     
191     
192       org.springframework
193       spring-aop
194       ${spring.version}
195     
196     
197 
198 
199     
200     
201       com.fasterxml.jackson.core
202       jackson-core
203       ${jackson.version}
204     
205     
206       com.fasterxml.jackson.core
207       jackson-databind
208       ${jackson.version}
209     
210     
211       com.fasterxml.jackson.core
212       jackson-annotations
213       ${jackson.version}
214     
215     
216 
217 
218     
219       commons-fileupload
220       commons-fileupload
221       1.3.3
222     
223 
224 
225     
226     
227       commons-logging
228       commons-logging
229       ${commons-logging.version}
230     
231 
232     
233     
234       org.apache.logging.log4j
235       log4j-core
236       ${log4j.version}
237     
238 
239     
240       org.springframework.security
241       spring-security-web
242       4.2.3.RELEASE
243     
244 
245   
246 
247 
248   
249     friend
250     
251       
252         src/main/resources
253         true
254         
255           **/*.xml
256         
257       
258 
259       
260         src/main/resources
261         true
262         
263           **/*.properties
264         
265       
266 
267       
268         src/main/resources
269         
270           **/*.properties
271         
272       
273 
274       
275         src/main/java
276         
277           **/*.java
278         
279       
280 
281 
282     
283   
284 
View Code

 

2.2 数据库配置 spring-db.xml

"1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    
    "dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        "driverClass" value="com.mysql.jdbc.Driver"/>
        "jdbcUrl" value="jdbc:mysql:///day11?useSSL=true" />
        "user" value="${mysql.user}" />
        "password" value="${mysql.password}" />
        "acquireIncrement" value="2" />
        "maxPoolSize" value="20" />
        "maxIdleTime" value="1000" />
    

    
    "sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        "dataSource" ref="dataSource"/>
        "configLocation" value="classpath:mybatis-config.xml">
    


    
    "sessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        "0" ref="sqlSessionFactory"/>
    

    
    "mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        "basePackage" value="com.huitong"/>
        "sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    

    "transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        "dataSource" ref="dataSource"/>
    

    "transactionManager" proxy-target-class="true"/>

View Code

 

2.3 springMVC配置文件,springMVC.xml

"1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

    
    
        
            class="org.springframework.http.converter.StringHttpMessageConverter">
                "UTF-8"/>
            

        
    
    
    base-package="com.huitong" />
    
    default-servlet-handler />

    "multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        "defaultEncoding" value="utf-8"/>
    

View Code

 

2.4 mybatis配置文件

"1.0" encoding="UTF-8"?>
DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

    
    

    
        
        "cacheEnabled" value="false"/>
        
    
View Code

 

2.5 spring的配置,将所有配置进行整合

"1.0" encoding="UTF-8"?>
DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

    
    

    
        
        "cacheEnabled" value="false"/>
        
    
View Code

 

2.6 web.xml配置文件

app PUBLIC
                "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
                "http://java.sun.com/dtd/web-app_2_3.dtd" >

Archetype Created Web Application
  

  contextConfigLocation
  classpath:spring.xml




  characterEncodingFilter
  class>org.springframework.web.filter.CharacterEncodingFilterclass>
  
    encoding
    UTF-8
  
  
    forceEncoding
    true
  




  httpPutFormContentFilter
  class>org.springframework.web.filter.HttpPutFormContentFilterclass>



  characterEncodingFilter
  dispatcherServlet



  httpPutFormContentFilter
  dispatcherServlet





  class>org.springframework.web.context.ContextLoaderListenerclass>





  dispatcherServlet
  class>org.springframework.web.servlet.DispatcherServletclass>

  
    contextConfigLocation
    classpath:spring.xml
  
  1



  dispatcherServlet
  /*


View Code

 

至此,所有配置文件已经配置完毕。我的项目结构如下图

RESTful风格的SSM框架搭建_第1张图片

 

3 关键类

3.1 实体类Student

 1 public class Student {
 2 
 3     private String id;
 4     private String name;
 5     private Integer age;
 6     private String gender;
 7     private Date birthday;
 8 
 9     public Student() {
10     }
11 
12 
13     public Student(
14             String id,
15             String name,
16             Integer age,
17             String gender,
18             Date birthday) {
19         this.id = id;
20         this.name = name;
21         this.age = age;
22         this.gender = gender;
23         this.birthday = birthday;
24     }
25 
26     public String getId() {
27         return id;
28     }
29 
30     public void setId(String id) {
31         this.id = id;
32     }
33 
34     public String getName() {
35         return name;
36     }
37 
38     public void setName(String name) {
39         this.name = name;
40     }
41 
42     public Integer getAge() {
43         return age;
44     }
45 
46     public void setAge(Integer age) {
47         this.age = age;
48     }
49 
50     public String getGender() {
51         return gender;
52     }
53 
54     public void setGender(String gender) {
55         this.gender = gender;
56     }
57 
58     public Date getBirthday() {
59         return birthday;
60     }
61 
62     public void setBirthday(Date birthday) {
63         this.birthday = birthday;
64     }
65 }
View Code

 

3.2  StudentDao接口

 1 @Repository
 2 public interface StudentDao {
 3     /**
 4      * 添加学生
 5      * @param student 学生信息
 6      */
 7     void insert(Student student);
 8 
 9     /**
10      * 查询学生信息
11      * @param id 学生ID
12      * @return 查询到的学生信息
13      */
14     Student queryStudentById(@Param("id") String id);
15 
16     String getNameById(@Param("id") String id);
17 }
View Code

 

3.3 StudentDao.xml 映射配置

 1 "1.0" encoding="UTF-8"?>
 2 DOCTYPE mapper
 3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 namespace="com.huitong.adaptor.persistence.mybatis.StudentDao">
 6 
 7     "insert" parameterType="com.huitong.entity.Student">
 8         INSERT INTO student(id, name, age, gender, birthday)
 9         VALUES(#{id},#{name},#{age},#{gender},#{birthday})
10     
11 
12     <select id="queryStudentById" parameterType="string" resultType="com.huitong.entity.Student">
13         SELECT *
14         FROM student
15         WHERE id=#{id}
16     select>
17 
18     <select id="getNameById" resultType="string">
19         SELECT NAME
20         FROM student
21         WHERE id=#{id}
22     select>
23 
24 
View Code

 

 4 问题小结

4.1 报错找不到applicationContext.xml 配置文件

 IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]
解决方案:查看博文 关于“Could not open ServletContext resource [/WEB-INF/applicationContext.xml]”解决方案

4.2 报错org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
解决方案
一般的原因是Mapper interface和xml文件的定义对应不上,需要检查包名,namespace,函数名称等能否对应上,需要比较细致的对比,

按以下步骤一一执行:

1:检查xml文件所在的package名称是否和interface对应的package名称一一对应

2:检查xml文件的namespace是否和xml文件的package名称一一对应

3:检查函数名称能否对应

但是博主在尝试以上方案之后仍然报错,在我查看了发布之后的源代码之后,发现*Mapper.xml文件并没有发布到目标文件中。我是使用maven 进行项目构建。修改pom.xmml文件,
子项中添加

      
        src/main/resources
        true
        
          **/*.xml
        
      

      
        src/main/resources
        true
        
          **/*.properties
        
      

      
        src/main/resources
        
          **/*.properties
        
      

      
        src/main/java
        
          **/*.java
        
      

    

 

4.3 在请求中,获取string字符串中文乱码问题

解决方案,修改 springMVC.xml 配置文件,参考博客 SpringMVC源码总结(四)由StringHttpMessageConverter引出的客户端服务器端之间的乱码过程分析


        
            class="org.springframework.http.converter.StringHttpMessageConverter">
                "UTF-8"/>
            

        
    

 

4.4 处理返回类型是日期类型 Date

解决方案,修改 springMVC.xml 配置文件



        
            class="org.springframework.http.converter.StringHttpMessageConverter">
                "UTF-8"/>
            
            class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                "objectMapper">
                    class="com.fasterxml.jackson.databind.ObjectMapper">
                        "dateFormat">
                            class="java.text.SimpleDateFormat">
                                "java.lang.String" value="yyyy-MM-dd HH:mm:ss"/>
                            
                        
                    
                
            

        
    

 

转载于:https://www.cnblogs.com/zhaopengcheng/p/7224001.html

你可能感兴趣的:(RESTful风格的SSM框架搭建)