RESTful风格的SSM框架搭建

时间:2021-10-29 16:36:09

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

 

2 进行项目配置

2.1 pom文件依赖

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

 

2.2 数据库配置 spring-db.xml

RESTful风格的SSM框架搭建RESTful风格的SSM框架搭建
<?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: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">

<!--数据源配置-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///day11?useSSL=true" />
<property name="user" value="${mysql.user}" />
<property name="password" value="${mysql.password}" />
<property name="acquireIncrement" value="2" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="1000" />
</bean>

<!--SqlSessionFactoryBean 配置-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>


<!-- SqlSessionTemplate 配置 -->
<bean id="sessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>

<!-- MapperScannerConfigurer 配置 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.huitong"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

</beans>
View Code

 

2.3 springMVC配置文件,springMVC.xml

RESTful风格的SSM框架搭建RESTful风格的SSM框架搭建
<?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: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">

<!-- 注解驱动 -->
<mvc:annotation-driven >
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>

</mvc:message-converters>
</mvc:annotation-driven>
<!-- 扫描包 -->
<context:component-scan base-package="com.huitong" />
<!-- 对静态资源文件的访问 不支持访问WEB-INF目录 -->
<mvc:default-servlet-handler />

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
</bean>

</beans>
View Code

 

2.4 mybatis配置文件

RESTful风格的SSM框架搭建RESTful风格的SSM框架搭建
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--任意环境,数据源和 MyBatis 的事务管理器都会被 忽略 。-->
<!--通常这会是 <settings> 或 <typeAliases> 的部分。-->

<settings>
<!-- Globally enables or disables any caches configured in any mapper under this configuration -->
<setting name="cacheEnabled" value="false"/>
<!--<setting name="logImpl" value="STDOUT_LOGGING" />-->
</settings>
</configuration>
View Code

 

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

RESTful风格的SSM框架搭建RESTful风格的SSM框架搭建
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--任意环境,数据源和 MyBatis 的事务管理器都会被 忽略 。-->
<!--通常这会是 <settings> 或 <typeAliases> 的部分。-->

<settings>
<!-- Globally enables or disables any caches configured in any mapper under this configuration -->
<setting name="cacheEnabled" value="false"/>
<!--<setting name="logImpl" value="STDOUT_LOGGING" />-->
</settings>
</configuration>
View Code

 

2.6 web.xml配置文件

RESTful风格的SSM框架搭建RESTful风格的SSM框架搭建
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 指定 spring 的配置文件,不然会报错找不到applicationContext.xml 文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>


<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>

<!--解决表单put请求-->
<filter>
<filter-name>httpPutFormContentFilter</filter-name>
<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<servlet-name>dispatcherServlet</servlet-name>
</filter-mapping>

<filter-mapping>
<filter-name>httpPutFormContentFilter</filter-name>
<servlet-name>dispatcherServlet</servlet-name>
</filter-mapping>


<!-- 方便获取ApplicationContext -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>



<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

</web-app>
View Code

 

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

RESTful风格的SSM框架搭建

 

3 关键类

3.1 实体类Student

RESTful风格的SSM框架搭建RESTful风格的SSM框架搭建
 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接口

RESTful风格的SSM框架搭建RESTful风格的SSM框架搭建
 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 映射配置

RESTful风格的SSM框架搭建RESTful风格的SSM框架搭建
 1 <?xml version="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 <mapper namespace="com.huitong.adaptor.persistence.mybatis.StudentDao">
6
7 <insert id="insert" parameterType="com.huitong.entity.Student">
8 INSERT INTO student(id, name, age, gender, birthday)
9 VALUES(#{id},#{name},#{age},#{gender},#{birthday})
10 </insert>
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 </mapper>
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文件,
在<build>子项中添加
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
</includes>
</resource>

<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>*
*/*.properties</include>
</includes>
</resource>

<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>

<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>*
*/*.java</exclude>
</excludes>
</resource>

</resources>

 

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

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

<mvc:annotation-driven >
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>

</mvc:message-converters>
</mvc:annotation-driven>

 

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

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

 
<mvc:annotation-driven >
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss"/>
</bean>
</property>
</bean>
</property>
</bean>

</mvc:message-converters>
</mvc:annotation-driven>