ssm框架搭建并演示获取json字符串

为方便起见,使用oracle自带的scott账户中的emp表作为数据源。

预期效果:

1.输入URL:http://localhost:8888/ssm/getEmp?empno=7369

2.返回结果如下:

实现过程:

1.引入所需jar包:

  1 <dependencies>
  2     <dependency>  
  3            <groupId>jstl</groupId>  
  4            <artifactId>jstl</artifactId>  
  5            <version>1.1.2</version>  
  6        </dependency>
  7        <!-- https://mvnrepository.com/artifact/taglibs/standard -->
  8 <dependency>
  9     <groupId>taglibs</groupId>
 10     <artifactId>standard</artifactId>
 11     <version>1.1.2</version>
 12 </dependency>
 13 <dependency>  
 14             <groupId>com.fasterxml.jackson.core</groupId>  
 15             <artifactId>jackson-core</artifactId>  
 16             <version>2.8.9</version>
 17         </dependency>  
 18           <dependency>
 19             <groupId>com.fasterxml.jackson.core</groupId>  
 20             <artifactId>jackson-databind</artifactId>  
 21             <version>2.8.9</version>  
 22         </dependency>
 23         <dependency>  
 24             <groupId>com.fasterxml.jackson.core</groupId>  
 25             <artifactId>jackson-annotations</artifactId>  
 26             <version>2.8.9</version>  
 27         </dependency>  
 28     <dependency>
 29       <groupId>junit</groupId>
 30       <artifactId>junit</artifactId>
 31       <version>3.8.1</version>
 32       <scope>test</scope>
 33     </dependency>
 34       <!-- mybatis-generator-core核心jar包 -->  
 35 <dependency>  
 36     <groupId>org.mybatis.generator</groupId>  
 37     <artifactId>mybatis-generator-core</artifactId>  
 38     <version>1.3.2</version>  
 39 </dependency> 
 40 <!-- c3p0连接池 --><!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
 41 <dependency>
 42     <groupId>c3p0</groupId>
 43     <artifactId>c3p0</artifactId>
 44     <version>0.9.1.2</version>
 45 </dependency>
 46 
 47 <!-- 添加连接Oracle的jar包 -->
 48 <!-- https://mvnrepository.com/artifact/ojdbc/ojdbc -->
 49 <dependency>
 50     <groupId>ojdbc</groupId>
 51     <artifactId>ojdbc</artifactId>
 52     <version>14-10gR3</version>
 53 </dependency>
 54     <!-- Spring4.3.3 -->
 55    <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
 56 <dependency>
 57     <groupId>org.springframework</groupId>
 58     <artifactId>spring-web</artifactId>
 59     <version>4.3.3.RELEASE</version>
 60 </dependency>
 61 <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
 62 <dependency>
 63     <groupId>org.springframework</groupId>
 64     <artifactId>spring-webmvc</artifactId>
 65     <version>4.3.3.RELEASE</version>
 66 </dependency>
 67 <!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib-ext-spring 
 68 <dependency>
 69     <groupId>net.sf.json-lib</groupId>
 70     <artifactId>json-lib-ext-spring</artifactId>
 71     <version>1.0.2</version>
 72 </dependency>-->
 73 <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl -->
 74 <dependency>
 75     <groupId>org.codehaus.jackson</groupId>
 76     <artifactId>jackson-mapper-asl</artifactId>
 77     <version>1.9.13</version>
 78 </dependency>
 79 <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-lgpl -->
 80 <dependency>
 81     <groupId>org.codehaus.jackson</groupId>
 82     <artifactId>jackson-mapper-lgpl</artifactId>
 83     <version>1.9.13</version>
 84 </dependency>
 85 <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl -->
 86 <dependency>
 87     <groupId>org.codehaus.jackson</groupId>
 88     <artifactId>jackson-core-asl</artifactId>
 89     <version>1.9.13</version>
 90 </dependency>
 91 <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-lgpl -->
 92 <dependency>
 93     <groupId>org.codehaus.jackson</groupId>
 94     <artifactId>jackson-core-lgpl</artifactId>
 95     <version>1.9.13</version>
 96 </dependency>
 97 <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
 98 <dependency>
 99     <groupId>org.springframework</groupId>
100     <artifactId>spring-jdbc</artifactId>
101     <version>4.3.3.RELEASE</version>
102 </dependency>
103 <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
104 <dependency>
105     <groupId>org.springframework</groupId>
106     <artifactId>spring-context</artifactId>
107     <version>4.3.3.RELEASE</version>
108 </dependency>
109 <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
110 <dependency>
111     <groupId>org.springframework</groupId>
112     <artifactId>spring-core</artifactId>
113     <version>4.3.3.RELEASE</version>
114 </dependency>
115 <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
116 <dependency>
117     <groupId>org.springframework</groupId>
118     <artifactId>spring-aop</artifactId>
119     <version>4.3.3.RELEASE</version>
120 </dependency>
121 <!-- 与spring-aop相关的jar包 -->
122 <!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
123 <dependency>
124     <groupId>aopalliance</groupId>
125     <artifactId>aopalliance</artifactId>
126     <version>1.0</version>
127 </dependency>
128 <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
129 <dependency>
130     <groupId>org.aspectj</groupId>
131     <artifactId>aspectjrt</artifactId>
132     <version>1.8.9</version>
133 </dependency>
134 <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
135 <dependency>
136     <groupId>org.aspectj</groupId>
137     <artifactId>aspectjweaver</artifactId>
138     <version>1.8.9</version>
139 </dependency>
140 <!-- Junit -->
141 <!-- https://mvnrepository.com/artifact/junit/junit -->
142 <dependency>
143     <groupId>junit</groupId>
144     <artifactId>junit</artifactId>
145     <version>4.10</version>
146 </dependency>
147 <!-- JSR303验证 -->
148 <!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
149 <dependency>
150     <groupId>javax.validation</groupId>
151     <artifactId>validation-api</artifactId>
152     <version>1.1.0.Final</version>
153 </dependency>
154 <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
155 <dependency>
156     <groupId>org.hibernate</groupId>
157     <artifactId>hibernate-validator</artifactId>
158     <version>5.2.4.Final</version>
159 </dependency>
160 
161 <!-- 重写equals和hashCode方法的包 -->
162 <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
163 <dependency>
164     <groupId>commons-lang</groupId>
165     <artifactId>commons-lang</artifactId>
166     <version>2.6</version>
167 </dependency>
168 <!-- tiles页面布局 -->
169     <!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-jsp -->
170 <dependency>
171     <groupId>org.apache.tiles</groupId>
172     <artifactId>tiles-jsp</artifactId>
173     <version>3.0.7</version>
174 </dependency>
175 <!-- log4j -->
176 <!-- https://mvnrepository.com/artifact/log4j/log4j -->
177 <dependency>
178     <groupId>log4j</groupId>
179     <artifactId>log4j</artifactId>
180     <version>1.2.17</version>
181 </dependency>
182 <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-ext -->
183 <dependency>
184     <groupId>org.slf4j</groupId>
185     <artifactId>slf4j-ext</artifactId>
186     <version>1.5.10</version>
187 </dependency>
188 <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
189 <dependency>
190     <groupId>org.slf4j</groupId>
191     <artifactId>slf4j-api</artifactId>
192     <version>1.5.10</version>
193 </dependency>
194 <!-- https://mvnrepository.com/artifact/org.slf4j/jcl-over-slf4j -->
195 <dependency>
196     <groupId>org.slf4j</groupId>
197     <artifactId>jcl-over-slf4j</artifactId>
198     <version>1.5.10</version>
199 </dependency>
200 <!-- slf4j -->
201 <dependency>
202     <groupId>org.slf4j</groupId>
203     <artifactId>slf4j-log4j12</artifactId>
204     <version>1.5.10</version>
205 </dependency>
206 <!-- AspectJ -->
207 <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
208 <dependency>
209     <groupId>org.aspectj</groupId>
210     <artifactId>aspectjrt</artifactId>
211     <version>1.8.9</version>
212 </dependency>
213 <!-- SpringMVC文件上传 -->
214 <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
215 <dependency>
216     <groupId>commons-io</groupId>
217     <artifactId>commons-io</artifactId>
218     <version>2.4</version>
219 </dependency>
220 <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
221 <dependency>
222     <groupId>commons-fileupload</groupId>
223     <artifactId>commons-fileupload</artifactId>
224     <version>1.3.1</version>
225 </dependency>
226   <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
227 <dependency>
228     <groupId>org.mybatis</groupId>
229     <artifactId>mybatis</artifactId>
230     <version>3.4.1</version>
231 </dependency>
232   <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
233 <dependency>
234     <groupId>org.mybatis</groupId>
235     <artifactId>mybatis-spring</artifactId>
236     <version>1.3.0</version>
237 </dependency>
238 
239 </dependencies>
pom.xml

2.创建所需目录:

3.使用MyBatis-generator生成POJO和mapper

  3.1为pojo添加@Component注解

  3.2为mapper接口添加@repository注解

4.在resources目录中放置log4j配置文件

 1 log4j.rootLogger=DEBUG,Console  
 2   
 3 #Console  
 4 log4j.appender.Console=org.apache.log4j.ConsoleAppender  
 5 log4j.appender.Console.layout=org.apache.log4j.PatternLayout  
 6 log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n  
 7   
 8 #sql show
 9 log4j.logger.com.ibatis=DEBUG
10 log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
11 log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
12 log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
13 log4j.logger.java.sql.Connection=DEBUG
14 log4j.logger.java.sql.Statement=DEBUG
15 log4j.logger.java.sql.PreparedStatement=DEBUG
16 
17 # MyBatis logging configuration...
18 #log4j.logger.com.yan.ssm.dao.mapper=TRACE
log4j.properties

 5.在resources目录中放置数据库配置文件

 1 #jdbc.driverClassName=com.mysql.jdbc.Driver
 2 #jdbc.url=jdbc:mysql://localhost:3306:mysql
 3 jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
 4 jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
 5 jdbc.username=scott
 6 jdbc.password=orcl
 7 jdbc.initialPoolSize=20 
 8 jdbc.maxPoolSize=100  
 9 jdbc.minPoolSize=10  
10 jdbc.maxIdleTime=600  
11 jdbc.acquireIncrement=5  
12 jdbc.maxStatements=5  
13 jdbc.idleConnectionTestPeriod=60
dataSource.properties

6.配置MyBatis配置文件

  6.1配置log4j

1 <settings>
2     <!-- 使用Log4J输出日志 -->
3     <setting name="logImpl" value="LOG4J"/>
4     <!-- 配置全局的映射器启用或禁用缓存 -->
5     <setting name="cacheEnabled" value="false"/>
6     <setting name="lazyLoadingEnabled" value="true"/>
7     <setting name="defaultStatementTimeout" value="30000"/>
8 </settings>

  6.2定义别名

1 <!-- 定义别名 -->
2 <typeAliases>
3     <package name="com.yan.issm.pojo"/>
4 </typeAliases>

  6.3定义mapper(和此例中的7.4+7.5起同样作用,此处其实可以不配置,统一交由Spring管理)

1 <mappers>
2 <!-- mapper和package两种方式二选一
3 <mapper resource="com/yan/issm/dao/mapper/EmpMapper.xml"/>
4 <mapper resource="com/yan/issm/dao/mapper/DeptMapper.xml"/> -->
5 <package name="com.yan.issm.dao.mapper"/>
6 </mappers>

6.4MyBatis配置全文

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6 <settings>
 7     <!-- 使用Log4J输出日志 -->
 8     <setting name="logImpl" value="LOG4J"/>
 9     <!-- 配置全局的映射器启用或禁用缓存 -->
10     <setting name="cacheEnabled" value="false"/>
11     <setting name="lazyLoadingEnabled" value="true"/>
12     <setting name="defaultStatementTimeout" value="30000"/>
13 </settings>
14 <!-- 定义别名 -->
15 <typeAliases>
16     <package name="com.yan.issm.pojo"/>
17 </typeAliases>
18 
19 <mappers>
20 <mapper resource="com/yan/issm/dao/mapper/EmpMapper.xml"/>
21 <mapper resource="com/yan/issm/dao/mapper/DeptMapper.xml"/>
22 </mappers>
23 </configuration>
sqlMapConfig.xml

7.配置Spring上下文

  7.1配置beans属性

 1 <beans
 2     xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:c="http://www.springframework.org/schema/c"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xmlns:p="http://www.springframework.org/schema/p"
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xmlns:jdbc="http://www.springframework.org/schema/jdbc"
 8     xmlns:jee="http://www.springframework.org/schema/jee"
 9     xmlns:aop="http://www.springframework.org/schema/aop"
10     xmlns:context="http://www.springframework.org/schema/context"
11     xmlns:mvc="http://www.springframework.org/schema/mvc"
12     xmlns:conditional="http://www.springframework.org/schema/context"
13     xsi:schemaLocation="http://www.springframework.org/schema/beans 
14     http://www.springframework.org/schema/beans/spring-beans.xsd
15     http://www.springframework.org/schema/context 
16     http://www.springframework.org/schema/context/spring-context.xsd
17     http://www.springframework.org/schema/jdbc 
18     http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
19     http://www.springframework.org/schema/aop
20     http://www.springframework.org/schema/aop/spring-aop.xsd
21     http://www.springframework.org/schema/mvc
22     http://www.springframework.org/schema/mvc/spring-mvc.xsd
23     http://www.springframework.org/schema/jee 
24     http://www.springframework.org/schema/jee/spring-jee.xsd
25     http://www.springframework.org/schema/tx 
26     http://www.springframework.org/schema/tx/spring-tx.xsd
27     http://www.springframework.org/schema/conditional 
28     http://www.springframework.org/schema/conditional/spring-conditional.xsd">
29         ...
30 </beans>

  7.2配置自动扫描的包

1 <context:component-scan base-package="com.yan.issm"></context:component-scan>

  7.3导入数据库配置文件并读取

 1 <context:property-placeholder location="classpath:dataSource.properties"/>
 2 
 3     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
 4         <!-- 指定连接数据库的驱动-->
 5         <property name="driverClass" value="${jdbc.driverClassName}"/>  
 6         <!-- 指定连接数据库的URL-->  
 7         <property name="jdbcUrl" value="${jdbc.url}"/>  
 8         <!-- 指定连接数据库的用户名-->  
 9         <property name="user" value="${jdbc.username}"/>  
10         <!-- 指定连接数据库的密码-->  
11         <property name="password" value="${jdbc.password}"/>  
12         <!-- 指定连接池中保留的最大连接数. Default:15-->  
13         <property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>  
14         <!-- 指定连接池中保留的最小连接数-->  
15         <property name="minPoolSize" value="${jdbc.minPoolSize}"/>  
16         <!-- 指定连接池的初始化连接数  取值应在minPoolSize 与 maxPoolSize 之间.Default:3-->  
17         <property name="initialPoolSize" value="${jdbc.initialPoolSize}"/>  
18         <!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。 Default:0-->  
19         <property name="maxIdleTime" value="${jdbc.maxIdleTime}"/>  
20         <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数. Default:3-->  
21         <property name="acquireIncrement" value="${jdbc.acquireIncrement}"/>  
22         <!-- JDBC的标准,用以控制数据源内加载的PreparedStatements数量。  
23         但由于预缓存的statements属于单个connection而不是整个连接池所以设置这个参数需要考虑到多方面的因数.如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:0-->  
24         <property name="maxStatements" value="${jdbc.maxStatements}"/>  
25         <!-- 每60秒检查所有连接池中的空闲连接.Default:0 -->  
26         <property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}"/>  
27     </bean>

  7.3配置MyBatis的SqlSessionFactory

1     <!-- 配置MyBatis的SqlSessionFactory,并且定义其scope为prototype-多例模式 -->
2     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" scope="prototype">
3         <property name="dataSource" ref="dataSource"></property>
4         <property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
5     </bean>

  7.4配置SQLSessionTemplate

1     <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
2         <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
3     </bean>

  7.5配置自动扫描mapper

1     <!-- 采用自动扫描方式创建mapper bean -->
2     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
3         <property name="basePackage" value="com.yan.issm.dao.mapper"/>
4         <property name="sqlSessionTemplateBeanName" value="sqlSessionTemplate"/>
5         <property name="annotationClass" value="org.springframework.stereotype.Repository"/>
6     </bean>

  7.6applicationContext配置全文

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans
 3     xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:c="http://www.springframework.org/schema/c"
 5     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6     xmlns:p="http://www.springframework.org/schema/p"
 7     xmlns:tx="http://www.springframework.org/schema/tx"
 8     xmlns:jdbc="http://www.springframework.org/schema/jdbc"
 9     xmlns:jee="http://www.springframework.org/schema/jee"
10     xmlns:aop="http://www.springframework.org/schema/aop"
11     xmlns:context="http://www.springframework.org/schema/context"
12     xmlns:mvc="http://www.springframework.org/schema/mvc"
13     xmlns:conditional="http://www.springframework.org/schema/context"
14     xsi:schemaLocation="http://www.springframework.org/schema/beans 
15     http://www.springframework.org/schema/beans/spring-beans.xsd
16     http://www.springframework.org/schema/context 
17     http://www.springframework.org/schema/context/spring-context.xsd
18     http://www.springframework.org/schema/jdbc 
19     http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
20     http://www.springframework.org/schema/aop
21     http://www.springframework.org/schema/aop/spring-aop.xsd
22     http://www.springframework.org/schema/mvc
23     http://www.springframework.org/schema/mvc/spring-mvc.xsd
24     http://www.springframework.org/schema/jee 
25     http://www.springframework.org/schema/jee/spring-jee.xsd
26     http://www.springframework.org/schema/tx 
27     http://www.springframework.org/schema/tx/spring-tx.xsd
28     http://www.springframework.org/schema/conditional 
29     http://www.springframework.org/schema/conditional/spring-conditional.xsd">
30     <!-- 配置自动扫描的包 -->
31     <context:component-scan base-package="com.yan.issm"></context:component-scan>
32     <!-- 导入配置文件 -->
33     <context:property-placeholder location="classpath:dataSource.properties"/>
34     <!-- <aop:aspectj-autoproxy proxy-target-class="true"/> -->
35     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
36         <!-- 指定连接数据库的驱动-->
37         <property name="driverClass" value="${jdbc.driverClassName}"/>  
38         <!-- 指定连接数据库的URL-->  
39         <property name="jdbcUrl" value="${jdbc.url}"/>  
40         <!-- 指定连接数据库的用户名-->  
41         <property name="user" value="${jdbc.username}"/>  
42         <!-- 指定连接数据库的密码-->  
43         <property name="password" value="${jdbc.password}"/>  
44         <!-- 指定连接池中保留的最大连接数. Default:15-->  
45         <property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>  
46         <!-- 指定连接池中保留的最小连接数-->  
47         <property name="minPoolSize" value="${jdbc.minPoolSize}"/>  
48         <!-- 指定连接池的初始化连接数  取值应在minPoolSize 与 maxPoolSize 之间.Default:3-->  
49         <property name="initialPoolSize" value="${jdbc.initialPoolSize}"/>  
50         <!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。 Default:0-->  
51         <property name="maxIdleTime" value="${jdbc.maxIdleTime}"/>  
52         <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数. Default:3-->  
53         <property name="acquireIncrement" value="${jdbc.acquireIncrement}"/>  
54         <!-- JDBC的标准,用以控制数据源内加载的PreparedStatements数量。  
55         但由于预缓存的statements属于单个connection而不是整个连接池所以设置这个参数需要考虑到多方面的因数.如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:0-->  
56         <property name="maxStatements" value="${jdbc.maxStatements}"/>  
57         <!-- 每60秒检查所有连接池中的空闲连接.Default:0 -->  
58         <property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}"/>  
59     </bean> 
60     <!-- 配置MyBatis的SqlSessionFactory,并且定义其scope为prototype-多例模式 -->
61     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" scope="prototype">
62         <property name="dataSource" ref="dataSource"></property>
63         <property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
64     </bean>
65     <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
66         <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
67     </bean>
68     <!-- 采用自动扫描方式创建mapper bean -->
69     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
70         <property name="basePackage" value="com.yan.issm.dao.mapper"/>
71         <property name="sqlSessionTemplateBeanName" value="sqlSessionTemplate"/>
72         <property name="annotationClass" value="org.springframework.stereotype.Repository"/>
73     </bean>
74 </beans>
applicationContext

8.配置SpringMVC

  8.1配置自动扫描

1 <mvc:annotation-driven/>

  8.2配置自动扫描的包名(必须配,与上下文中的有区别)

1 <context:component-scan base-package="com.yan.ssm.controller"/>

  8.3配置JSON转换器

1     <!-- 配置json转换器 -->
2     <bean id="jsonMessageConverters" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
3         <property name="supportedMediaTypes">
4             <list>
5                 <value>application/json;charset=UTF-8</value>
6             </list>
7         </property>
8     </bean>

  8.4配置JSON视图拦截器

1     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
2         <property name="messageConverters">
3             <list>
4                 <!-- JSON视图拦截器,碰到ResponseBody会触发 -->
5                 <ref bean="jsonMessageConverters"/>
6             </list>
7         </property>
8     </bean> 

  8.5配置视图和静态资源访问(本例不涉及

1         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
2             <property name="prefix" value="/WEB-INF/views/"></property>
3             <property name="suffix" value=".jsp"></property>
4             <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
5         </bean>
6     <!--添加访问静态资源的功能 -->
7  <mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources>

  8.6spring-servlet配置全文

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:mvc="http://www.springframework.org/schema/mvc"
 5     xmlns:p="http://www.springframework.org/schema/p"
 6     xmlns:context="http://www.springframework.org/schema/context"
 7     xsi:schemaLocation="
 8         http://www.springframework.org/schema/beans
 9         http://www.springframework.org/schema/beans/spring-beans.xsd
10         http://www.springframework.org/schema/context
11         http://www.springframework.org/schema/context/spring-context.xsd
12         http://www.springframework.org/schema/mvc
13         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
14     <mvc:annotation-driven/>
15     <context:component-scan base-package="com.yan.issm.controller"/>
16     <!-- 配置json转换器 -->
17     <bean id="jsonMessageConverters" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
18         <property name="supportedMediaTypes">
19             <list>
20                 <value>application/json;charset=UTF-8</value>
21             </list>
22         </property>
23     </bean>
24     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
25         <property name="messageConverters">
26             <list>
27                 <!-- JSON视图拦截器,碰到ResponseBody会触发 -->
28                 <ref bean="jsonMessageConverters"/>
29             </list>
30         </property>
31     </bean> 
32     
33         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
34             <property name="prefix" value="/WEB-INF/views/"></property>
35             <property name="suffix" value=".jsp"></property>
36             <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
37         </bean>
38     <!--添加访问静态资源的功能 -->
39  <mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources>
40 </beans>
Spring-servlet.xml

9.配置web.xml

  9.1配置log4j上下文

1   <context-param>   
2    <param-name>log4jConfigLocation</param-name>   
3    <param-value>classpath:log4j.properties</param-value>   
4 </context-param>

    9.2配置spring-log4j监听器

1 <!-- 定义LOG4J监听器 -->
2 <listener>   
3    <listener-class>   
4         org.springframework.web.util.Log4jConfigListener   
5    </listener-class>   
6 </listener> 

  9.3配置加载指定位置的上下文配置文件

1 <!-- 加载指定位置的上下文配置文件 -->
2   <context-param>
3       <param-name>contextConfigLocation</param-name>
4       <param-value>classpath:applicationContext.xml</param-value>
5   </context-param>

  9.4配置DispatcherServlet

 1 <servlet>
 2       <servlet-name>Spring</servlet-name>
 3       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 4       <!-- 表示启动容器时初始化该servlet -->
 5       <init-param>
 6           <param-name>contextConfigLocation</param-name>
 7           <param-value>classpath:Spring-servlet.xml</param-value>
 8       </init-param>
 9       <load-on-startup>1</load-on-startup>
10       </servlet>

  9.5配置DispatcherServlet映射规则

1       <servlet-mapping>
2           <servlet-name>Spring</servlet-name>
3           <!-- 表示哪些请求需要交给Spring Web MVC处理,/是用来定义默认servlet映射的。也可以如“*.html”表示拦截所有以html为扩展名的请求 -->
4           <url-pattern>/*</url-pattern>
5       </servlet-mapping>

  9.6配置字符集过滤器(纯英文,本例不涉及)

 1     <!-- 使用spring解决中文乱码 -->
 2         <filter>
 3         <filter-name>CharacterEncodingFilter</filter-name>
 4         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
 5         <init-param>
 6             <param-name>encoding</param-name>
 7             <param-value>utf-8</param-value>
 8         </init-param>
 9       </filter>
10       
11       <filter-mapping>
12         <filter-name>CharacterEncodingFilter</filter-name>
13         <url-pattern>/*</url-pattern>  
14       </filter-mapping>

  9.7web.xml配置全文

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="3.0" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 7   <display-name>ssm</display-name>    
 8   <welcome-file-list>
 9     <welcome-file>index.jsp</welcome-file>
10   </welcome-file-list>
11   <context-param>   
12    <param-name>log4jConfigLocation</param-name>   
13    <param-value>classpath:log4j.properties</param-value>   
14 </context-param>
15   <listener>   
16      <listener-class>   
17           org.springframework.web.context.ContextLoaderListener   
18      </listener-class>   
19 </listener>
20 <!-- 定义LOG4J监听器 -->
21 <listener>   
22    <listener-class>   
23         org.springframework.web.util.Log4jConfigListener   
24    </listener-class>   
25 </listener> 
26 <!-- 加载指定位置的上下文配置文件 -->
27   <context-param>
28       <param-name>contextConfigLocation</param-name>
29       <param-value>classpath:applicationContext.xml</param-value>
30   </context-param>
31 <servlet>
32       <servlet-name>Spring</servlet-name>
33       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
34       <!-- 表示启动容器时初始化该servlet -->
35       <init-param>
36           <param-name>contextConfigLocation</param-name>
37           <param-value>classpath:Spring-servlet.xml</param-value>
38       </init-param>
39       <load-on-startup>1</load-on-startup>
40       </servlet>
41       <servlet-mapping>
42           <servlet-name>Spring</servlet-name>
43           <!-- 表示哪些请求需要交给Spring Web MVC处理,/是用来定义默认servlet映射的。也可以如“*.html”表示拦截所有以html为扩展名的请求 -->
44           <url-pattern>/*</url-pattern>
45       </servlet-mapping>
46     <!-- 使用spring解决中文乱码 -->
47         <filter>
48         <filter-name>CharacterEncodingFilter</filter-name>
49         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
50         <init-param>
51             <param-name>encoding</param-name>
52             <param-value>utf-8</param-value>
53         </init-param>
54       </filter>
55       
56       <filter-mapping>
57         <filter-name>CharacterEncodingFilter</filter-name>
58         <url-pattern>/*</url-pattern>  
59       </filter-mapping>
60 </web-app>
web.xml

10.完成service层

  10.1创建EmpService接口

1 Emp selectByPrimaryKey(Short empno);

  10.2实现接口EmpServiceImpl

 1 @Service
 2 public class EmpServiceImpl implements EmpService {
 3     @Autowired
 4     EmpMapper empMapper;
 5 
 6     @Override
 7     public Emp selectByPrimaryKey(Short empno) {
 8         return empMapper.selectByPrimaryKey(empno);
 9     }
10 }

11.完成Controller层

 1 @Controller
 2 public class EmpController {
 3     @Autowired
 4     EmpService empService;
 5     
 6     @RequestMapping(value="/getEmp",method=RequestMethod.GET)
 7     @ResponseBody
 8     public Emp getEmp(short empno)
 9     {
10         return empService.selectByPrimaryKey(empno);
11     }
12 }

 

posted @ 2017-07-04 01:44  舒山  阅读(2749)  评论(0编辑  收藏  举报