服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - SSM框架搭建图文教程(推荐)

SSM框架搭建图文教程(推荐)

2021-01-11 14:09冰叔博客 Java教程

下面小编就为大家带来一篇SSM框架搭建图文教程(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

好久就想着好好搭建一个ssm框架,自己以后用也方便吧,但是最近的事真的是很多,很多事情都没有去干,有时候自己会怀疑一下人生自己该不该去做程序员,我是一个没毕业的学生,找工作真的是四处碰壁,感觉自己好难混,一个暑假换了3个公司。我其实就想找一个公司能干自己想干的技术,能学到自己想学到的东西,可是我发现太难了,几乎没有这样的公司。所以说在你不是很牛逼的时候,人得适应公司,而不是公司适应你。第一个公司的一个老程序员和我去公司必须能吃上饭,能吃上饭再想其他的东西,而我是个90后,我明白我的性格,一遇到难题就想逃避,可是逃避是最不好的道路,也是不能走的道路。希望刚毕业的同学能找到一个适合自己的工作。

一、我用的是idea

二、首先创建一个maven项目,结构如下:

SSM框架搭建图文教程(推荐)

三、开始写配置文件

SSM框架搭建图文教程(推荐)

pom文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
   xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelversion>4.0.0</modelversion>
 <groupid>com.xxx.demo</groupid>
 <artifactid>ssm</artifactid>
 <packaging>war</packaging>
 <version>1.0-snapshot</version>
 <name>ssm maven webapp</name>
 <url>http://maven.apache.org</url>
 
 <properties>
  <project.build.sourceencoding>utf-8</project.build.sourceencoding>
  <project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
  <!-- spring版本号 -->
  <spring.version>4.2.6.release</spring.version>
 
  <!-- mybatis版本号 -->
  <mybatis.version>3.2.8</mybatis.version>
 
  <!-- mysql驱动版本号 -->
  <mysql-driver.version>5.1.29</mysql-driver.version>
 
  <!-- log4j日志包版本号 -->
  <slf4j.version>1.7.18</slf4j.version>
  <log4j.version>1.2.17</log4j.version>
 </properties>
 
 
 <dependencies>
  <!-- 添加jstl依赖 -->
  <dependency>
   <groupid>jstl</groupid>
   <artifactid>jstl</artifactid>
   <version>1.2</version>
  </dependency>
 
  <dependency>
   <groupid>javax</groupid>
   <artifactid>javaee-api</artifactid>
   <version>7.0</version>
  </dependency>
 
  <!-- 添加junit4依赖 -->
  <dependency>
   <groupid>junit</groupid>
   <artifactid>junit</artifactid>
   <version>4.11</version>
   <!-- 指定范围,在测试时才会加载 -->
   <scope>test</scope>
  </dependency>
 
  <!-- 添加spring核心依赖 -->
  <dependency>
   <groupid>org.springframework</groupid>
   <artifactid>spring-core</artifactid>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupid>org.springframework</groupid>
   <artifactid>spring-web</artifactid>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupid>org.springframework</groupid>
   <artifactid>spring-oxm</artifactid>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupid>org.springframework</groupid>
   <artifactid>spring-tx</artifactid>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupid>org.springframework</groupid>
   <artifactid>spring-jdbc</artifactid>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupid>org.springframework</groupid>
   <artifactid>spring-webmvc</artifactid>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupid>org.springframework</groupid>
   <artifactid>spring-context</artifactid>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupid>org.springframework</groupid>
   <artifactid>spring-aspects</artifactid>
   <version>4.3.8.release</version>
  </dependency>
  <dependency>
   <groupid>org.springframework</groupid>
   <artifactid>spring-context-support</artifactid>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupid>org.springframework</groupid>
   <artifactid>spring-aop</artifactid>
   <version>${spring.version}</version>
  </dependency>
 
  <dependency>
   <groupid>org.springframework</groupid>
   <artifactid>spring-test</artifactid>
   <version>${spring.version}</version>
  </dependency>
 
  <!-- 添加mybatis依赖 -->
  <dependency>
   <groupid>org.mybatis</groupid>
   <artifactid>mybatis</artifactid>
   <version>${mybatis.version}</version>
  </dependency>
 
  <!-- 添加mybatis/spring整合包依赖 -->
  <dependency>
   <groupid>org.mybatis</groupid>
   <artifactid>mybatis-spring</artifactid>
   <version>1.2.2</version>
  </dependency>
 
  <!-- 添加mysql驱动依赖 -->
  <dependency>
   <groupid>mysql</groupid>
   <artifactid>mysql-connector-java</artifactid>
   <version>${mysql-driver.version}</version>
  </dependency>
  <!-- 添加数据库连接池依赖 -->
  <dependency>
   <groupid>com.alibaba</groupid>
   <artifactid>druid</artifactid>
   <version>1.1.3</version>
  </dependency>
 
  <!-- 添加fastjson -->
  <dependency>
   <groupid>com.alibaba</groupid>
   <artifactid>fastjson</artifactid>
   <version>1.1.41</version>
  </dependency>
 
  <!-- 添加日志相关jar包 -->
  <dependency>
   <groupid>log4j</groupid>
   <artifactid>log4j</artifactid>
   <version>${log4j.version}</version>
  </dependency>
  <dependency>
   <groupid>org.slf4j</groupid>
   <artifactid>slf4j-api</artifactid>
   <version>${slf4j.version}</version>
  </dependency>
  <dependency>
   <groupid>org.slf4j</groupid>
   <artifactid>slf4j-log4j12</artifactid>
   <version>${slf4j.version}</version>
  </dependency>
 
  <!-- log end -->
  <!-- 映入json -->
  <dependency>
   <groupid>org.codehaus.jackson</groupid>
   <artifactid>jackson-mapper-asl</artifactid>
   <version>1.9.13</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
  <dependency>
   <groupid>com.fasterxml.jackson.core</groupid>
   <artifactid>jackson-core</artifactid>
   <version>2.8.0</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
  <dependency>
   <groupid>com.fasterxml.jackson.core</groupid>
   <artifactid>jackson-databind</artifactid>
   <version>2.8.0</version>
  </dependency>
 
  <dependency>
   <groupid>commons-fileupload</groupid>
   <artifactid>commons-fileupload</artifactid>
   <version>1.3.1</version>
  </dependency>
 
  <dependency>
   <groupid>commons-io</groupid>
   <artifactid>commons-io</artifactid>
   <version>2.4</version>
  </dependency>
 
  <dependency>
   <groupid>commons-codec</groupid>
   <artifactid>commons-codec</artifactid>
   <version>1.9</version>
  </dependency>
  <dependency>
   <groupid>org.aspectj</groupid>
   <artifactid>aspectjweaver</artifactid>
   <version>1.8.10</version>
  </dependency>
 </dependencies>
 <build>
  <finalname>ssm</finalname>
 </build>
</project>

db.properties

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
db.driver=com.mysql.jdbc.driver
db.url=jdbc:mysql://localhost:3306/myuser?useunicode=true&characterencoding=utf-8&zerodatetimebehavior=converttonull
db.username=root
db.password=xxxxxxxxxxxxx
 
#定义初始连接数
db.initialsize=1
#定义最大连接数
db.maxactive=20
#定义最大空闲
db.maxidle=20
#定义最小空闲
db.minidle=1
#定义最长等待时间
db.maxwait=60000
#使用非公平锁
db.useunfairlock=true
#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
db.timebetweenevictionrunsmillis=60000
#配置一个连接在池中最小生存的时间,单位是毫秒
db.minevictableidletimemillis=300000
 
db.validationquery=select 'x'
#申请连接的时候检测,如果空闲时间大于timebetweenevictionrunsmillis,执行validationquery检测连接是否有效。
db.testwhileidle=true
#申请连接时执行validationquery检测连接是否有效
db.testonborrow=false
#归还连接时执行validationquery检测连接是否有效
db.testonreturn=false
#打开pscache,并且指定每个连接上pscache的大小
db.poolpreparedstatements=true
db.maxopenpreparedstatements=20
#配置监控统计拦截的filters,去掉后监控界面sql无法统计
db.filters=start

log4j.properties

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
log4j.rootlogger=debug,console,fileout
log4j.addivity.org.apache=true
 
# console 打印到控制台
log4j.appender.console=org.apache.log4j.consoleappender
log4j.appender.threshold=debug
log4j.appender.console.target=system.out
log4j.appender.console.layout=org.apache.log4j.patternlayout
log4j.appender.console.layout.conversionpattern=[%-5p] %d{yyyy-mm-dd hh\:mm\:ss} \:%m%n
 
#
# fileout 打印到 <span style="font-family: arial, helvetica, sans-serif;">d\:\\report.log</span>
log4j.appender.fileout=org.apache.log4j.rollingfileappender
log4j.appender.fileout.file=d\:\\report.log
log4j.appender.fileout.layout=org.apache.log4j.patternlayout
log4j.appender.fileout.maxfilesize=100000kb
 
log4j.appender.fileout.append=true
#log4j.appender.console.layout.conversionpattern=[framework] %d \u2013 %c -%-4r [%t] %-5p %c %x \u2013 %m%n
log4j.appender.fileout.layout.conversionpattern=[%-5p]_%d{yyyy-mm-dd hh\:mm\:ss} \:%m%n

usermapper.xml配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.ssm.dao.userdao">
 
 <resultmap type="com.ssm.pojo.user" id="user">
  <id column="id" property="id"/>
  <result column="username" property="username" />
  <result column="password" property="password"/>
 </resultmap>
 <select id="seleceuserbyid" parametertype="int" resultmap="user">
  select * from t_user where id=#{id}
 </select>
</mapper>

sp-mybatis配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?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"
  xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 
 <!--扫描数据库配置文件-->
 <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
 
 <!-- 第二种方式:加载多个properties文件
 <bean id="configproperties" class="org.springframework.beans.factory.config.propertiesfactorybean">
  <property name="locations">
   <list>
    <value>classpath:jdbc.properties</value>
    <value>classpath:common.properties</value>
   </list>
  </property>
  <property name="fileencoding" value="utf-8"/>
 </bean>
 <bean id="propertyconfigurer" class="org.springframework.beans.factory.config.preferencesplaceholderconfigurer">
  <property name="properties" ref="configproperties"/>
 </bean>
 -->
 
 <!--druid配置数据源-->
 <bean id="datasource" class="com.alibaba.druid.pool.druiddatasource">
  <property name="driverclassname" value="${db.driver}"></property>
  <property name="url" value="${db.url}"></property>
  <property name="username" value="${db.username}"></property>
  <property name="password" value="${db.password}"></property>
 
  <property name="initialsize" value="${db.initialsize}"/><!-- 配置初始化连接池数量-->
  <property name="minidle" value="${db.minidle}"/><!-- 配置最小连接池数量-->
  <property name="maxactive" value="${db.maxactive}"/><!-- 配置最大连接池数量-->
  <property name="maxwait" value="${db.maxwait}"/><!-- 配置获取连接等待超时的时间 单位毫秒-->
  <property name="useunfairlock" value="${db.useunfairlock}"/><!--使用非公平锁-->
  <property name="timebetweenevictionrunsmillis"
     value="${db.timebetweenevictionrunsmillis}"/><!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
  <property name="minevictableidletimemillis"
     value="${db.minevictableidletimemillis}"/><!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
  <property name="validationquery" value="${db.validationquery}"/><!--用来检测连接是否有效的sql,要求是一个查询语句。-->
  <property name="testwhileidle"
     value="${db.testwhileidle}"/><!--申请连接的时候检测,如果空闲时间大于timebetweenevictionrunsmillis,执行validationquery检测连接是否有效。-->
  <property name="testonborrow" value="${db.testonborrow}"/><!--申请连接时执行validationquery检测连接是否有效,-->
  <property name="testonreturn" value="${db.testonreturn}"/><!--归还连接时执行validationquery检测连接是否有效,-->
  <property name="poolpreparedstatements"
     value="${db.poolpreparedstatements}"/><!-- 打开pscache,并且指定每个连接上pscache的大小 -->
  <property name="maxopenpreparedstatements" value="${db.maxopenpreparedstatements}"/>
  <property name="filters" value="${db.filters}"/><!-- 配置监控统计拦截的filters,去掉后监控界面sql无法统计 -->
 </bean>
 
 <!-- mybatis和spring完美整合,不需要mybatis的配置映射文件 -->
 <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
  <property name="datasource" ref="datasource"></property>
  <property name="mapperlocations" value="classpath:mapper/*.xml"></property>
 </bean>
 
 <!-- dao接口所在包名,spring会自动查找其下的类 -->
 <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer">
  <property name="basepackage" value="com.ssm.dao"/>
  <property name="sqlsessionfactorybeanname" value="sqlsessionfactory"></property>
 </bean>
 
 
</beans>

sp-service.xml

?
1
2
3
4
5
6
7
8
9
10
11
<?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"
  xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 
 
 <context:component-scan base-package="com.ssm.service">
 </context:component-scan>
 
</beans>

sp-trans.xml配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?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"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
 
 <!-- 配置事务管理器 -->
 <bean id="transactionmanager"
   class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
  <property name="datasource" ref="datasource" />
 </bean>
 
 
 <!-- 注解方式配置事务 -->
 <tx:annotation-driven transaction-manager="transactionmanager" />
 
 <!-- 拦截器方式配置事物 -->
 <tx:advice id="transactionadvice" transaction-manager="transactionmanager">
  <tx:attributes>
   <!--required支持当前存在的事务,如果没有的话句创建一个新的事务
    propagation_required:支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
    supports:支持当前事务,如果当前没有事务,就以非事务方式执行。
    mandatory:支持当前事务,如果当前没有事务,就抛出异常。
    requires_new:新建事务,如果当前存在事务,把当前事务挂起。
    not_supported:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
    never:以非事务方式执行,如果当前存在事务,则抛出异常。
    nested:支持当前事务,如果当前事务存在,则执行一个嵌套事务(还不是太明白),如果当前没有事务,就新建一个事务。
    -->
   <tx:method name="add*" propagation="required" />
   <tx:method name="append*" propagation="required" />
   <tx:method name="insert*" propagation="required" />
   <tx:method name="save*" propagation="required" />
   <tx:method name="update*" propagation="required" />
   <tx:method name="modify*" propagation="required" />
   <tx:method name="edit*" propagation="required" />
   <tx:method name="delete*" propagation="required" />
   <tx:method name="remove*" propagation="required" />
   <tx:method name="repair" propagation="required" />
   <tx:method name="delandrepair" propagation="required" />
 
   <tx:method name="get*" propagation="supports" />
   <tx:method name="find*" propagation="supports" />
   <tx:method name="load*" propagation="supports" />
   <tx:method name="search*" propagation="supports" />
   <tx:method name="datagrid*" propagation="supports" />
 
   <tx:method name="*" propagation="supports" />
  </tx:attributes>
 </tx:advice>
 <aop:config>
  <aop:pointcut id="transactionpointcut" expression="execution(* com.ssm.service..*.*(..))" />
  <aop:advisor pointcut-ref="transactionpointcut"
      advice-ref="transactionadvice" />
 </aop:config>
 
 
 <!-- 配置druid监控spring jdbc -->
 <bean id="druid-stat-interceptor"
   class="com.alibaba.druid.support.spring.stat.druidstatinterceptor">
 </bean>
 <bean id="druid-stat-pointcut" class="org.springframework.aop.support.jdkregexpmethodpointcut"
   scope="prototype">
  <property name="patterns">
   <list>
    <value>com.ssm.service.*</value>
   </list>
  </property>
 </bean>
 <aop:config>
  <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />
 </aop:config>
 
</beans>

springmvc配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
 
 <!--扫描controller-->
 <context:component-scan base-package="com.ssm.controller"></context:component-scan>
 
 <!--mvc驱动-->
 <mvc:annotation-driven></mvc:annotation-driven>
 
 <!--视图解析器-->
 <bean class="org.springframework.web.servlet.view.internalresourceviewresolver">
  <property name="prefix" value="/web-inf/jsps"></property>
  <property name="suffix" value=".jsp"></property>
 </bean>
 
 <!-- 文件上传配置 -->
 <bean id="multipartresolver" class="org.springframework.web.multipart.commons.commonsmultipartresolver">
  <!-- 默认编码 -->
  <property name="defaultencoding" value="utf-8"/>
  <!-- 上传文件大小限制为31m,31*1024*1024 -->
  <property name="maxuploadsize" value="32505856"/>
  <!-- 内存中的最大值 -->
  <property name="maxinmemorysize" value="4096"/>
 </bean>
 
 
</beans>

web.xml配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!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>
 
 
 <context-param>
  <param-name>log4jconfiglocation</param-name>
  <param-value>classpath:log4j.properties</param-value>
 </context-param>
 
 <context-param>
  <param-name>contextconfiglocation</param-name>
  <param-value>classpath:spring/sp-*.xml</param-value>
 </context-param>
 
 <!-- 编码过滤器 -->
 <filter>
  <filter-name>encodingfilter</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>
 </filter>
 <filter-mapping>
  <filter-name>encodingfilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 
 <!--spring溢出监听器
 <listener>
  <listener-class>org.springframework.web.util.introspectorcleanuplistener</listener-class>
 </listener>-->
 
 <!--spring监听器-->
 <listener>
  <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>
 </listener>
 
 
 <!--配置servlet-->
 <servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>
  <init-param>
   <param-name>contextconfiglocation</param-name>
   <param-value>classpath:spring/springmvc.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 
 <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
 
 <!--session配置-->
 <session-config>
  <session-timeout>15</session-timeout>
 </session-config>
</web-app>

然后基本框架已经配置好了,剩下的就是写java代码了

controller

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.ssm.controller;
 
import com.ssm.pojo.user;
import com.ssm.service.userservice;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.responsebody;
 
import javax.annotation.resource;
 
/**
 * @author: hsj
 * @date: 2017/9/15
 * @description :
 */
@controller
@requestmapping(value = "/user")
public class usercontroller {
 
 @resource
 private userservice userservice;
 
 @requestmapping(value = "user")
 @responsebody
 public user getuser(int userid){
  return userservice.finduserbyid(userid);
 }
}

service

?
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.ssm.service;
 
import com.ssm.pojo.user;
import org.springframework.stereotype.service;
 
/**
 * @author: hsj
 * @date: 2017/9/15
 * @description :
 */
public interface userservice {
 user finduserbyid(int userid);
}

serviceimpl

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.ssm.service.impl;
 
import com.ssm.dao.userdao;
import com.ssm.pojo.user;
import com.ssm.service.userservice;
import org.springframework.stereotype.service;
 
import javax.annotation.resource;
 
/**
 * @author: hsj
 * @date: 2017/9/15
 * @description :
 */
@service
public class userserviceimpl implements userservice {
 
 @resource
 private userdao userdao;
 
 public user finduserbyid(int userid) {
  return userdao.seleceuserbyid(userid);
 }
}

pojo

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.ssm.pojo;
 
/**
 * @author: hsj
 * @date: 2017/9/18
 * @description :这是一个user实体类
 */
public class user {
 private int id;
 private string username;
 private string password;
 
 public int getid() {
  return id;
 }
 
 public void setid(int id) {
  this.id = id;
 }
 
 public string getusername() {
  return username;
 }
 
 public void setusername(string username) {
  this.username = username;
 }
 
 public string getpassword() {
  return password;
 }
 
 public void setpassword(string password) {
  this.password = password;
 }
 
 @override
 public string tostring() {
  return "user{" +
    "id=" + id +
    ", username='" + username + '\'' +
    ", password='" + password + '\'' +
    '}';
 }
}

dao

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.ssm.dao;
 
import com.ssm.pojo.user;
import org.springframework.stereotype.repository;
 
/**
 * @author: hsj
 * @date: 2017/9/15
 * @description :
 */
 
@repository
public interface userdao {
 user seleceuserbyid(int userid);
}

以上这篇ssm框架搭建图文教程(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/bingshu/archive/2017/09/26/7599629.html

延伸 · 阅读

精彩推荐