关于springMVC的配置,可以参考这篇文章的第一个小节:
【java】intellij idea SpringMVC 配置FreeMarker模板引擎
- 第一步打开intellij idea创建maven项目
- 第二步 配置maven
- 第三步 通过maven添加SSM所需框架和其他库
- 第四步 添加各框架的配置文件
- Spring与MyBatis的整合
- 1web-front 的resources下新建jdbcproperties文件编码修改为utf-8如果不存在这个目录则自己手动创建一个directory然后右建make directory as 选择resources root具体配置根据本机情况调整
- 2配置log4j方便日志的输出
- 3创建Spring-myBatis此文件就是专门用来整合Spring和mybatis的
- 4使用MyBatis Generator自动创建代码
- 5编写测试用例
- SpringMVC整合
- 1配置Spring-mvcxml
- 2 配置webxml
- 3 测试
- Spring与MyBatis的整合
- 总结
第一步:打开intellij idea,创建maven项目
既然是从0开始,那么我们就从打开intellij idea 开始!
点击intellij idea左上方的project->
出现如下界面,选择maven,然后选择项目Java的SDK版本,当然如果项目需要其他的SDK版本你也可以自己选择
这里说明一下,默认的maven项目会提供一些模板给开发者使用,
然后点击下一步
进入到填写GroupId和ArtifactId的界面
这两个id不是随便写的,后面的模块与模块之间的关系都是依赖于GroupId和ArtifactId来完成的,这里暂不叙述,后面再聊
接着再点next,进入填写项目名称的阶段
这里有一个有趣的细节,那就是上一步你的artifactId填写的是什么,下一步中默认的projectName 就会是什么,同时注意下面的Module也会是这个名称。
这里换个名字吧~
点击Finish 完成第一步!
此时的项目结构是这样的
在开始下一步之前,再来深究一下GroupId和ArtifactId的作用
假设我们的项目包含3个模块,分别是web-mobie,web-pc,web-service
那么如果他们的GroupId和ArtifactId设置如下
那么我们新建module
然后又出现了我们新建项目时的那个界面了!
操作不赘述,重点是这个界面
然后以此类推添加module
第二步 配置maven
setting.xml镜像配置:
- <mirror>
- <id>alimaven</id>
- <name>aliyun maven</name>
- <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
- <mirrorOf>central</mirrorOf>
- </mirror>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
第三步 通过maven添加SSM所需框架和其他库
在项目(注意不是module)的pom.xml文件中添加相应代码:
注意,这里所有的依赖都是可以根据项目的需求自行改变的
- <properties>
- <project.build.sourceEncoding>
- UTF-8
- </project.build.sourceEncoding>
- <!-- spring版本号 -->
- <spring.version>
- 4.1.4.RELEASE
- </spring.version>
- <!-- mybatis版本号 -->
- <mybatis.version>
- 3.2.8
- </mybatis.version>
- <!-- log4j日志文件管理包版本 -->
- <slf4j.version>
- 1.7.7
- </slf4j.version>
- <log4j.version>
- 1.2.17
- </log4j.version>
- <!-- jackson包版本 -->
- <jackson.version>
- 2.5.0
- </jackson.version>
- </properties>
- <dependencies>
- <!-- 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>
- <!--spring单元测试依赖 -->
- <dependency>
- <groupId>
- org.springframework
- </groupId>
- <artifactId>
- spring-test
- </artifactId>
- <version>
- ${spring.version}
- </version>
- <scope>
- test
- </scope>
- </dependency>
- <!-- springMVC核心包 -->
- <dependency>
- <groupId>
- org.springframework
- </groupId>
- <artifactId>
- spring-webmvc
- </artifactId>
- <version>
- ${spring.version}
- </version>
- </dependency>
- <!-- spring核心包 -->
- <dependency>
- <groupId>
- org.springframework
- </groupId>
- <artifactId>
- spring-core
- </artifactId>
- <version>
- ${spring.version}
- </version>
- </dependency>
- <dependency>
- <groupId>
- org.springframework
- </groupId>
- <artifactId>
- spring-beans
- </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-context-support
- </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-web
- </artifactId>
- <version>
- ${spring.version}
- </version>
- </dependency>
- <dependency>
- <groupId>
- org.springframework
- </groupId>
- <artifactId>
- spring-jdbc
- </artifactId>
- <version>
- ${spring.version}
- </version>
- </dependency>
- <dependency>
- <groupId>
- javax.servlet
- </groupId>
- <artifactId>
- javax.servlet-api
- </artifactId>
- <version>
- 3.0.1
- </version>
- <scope>
- provided
- </scope>
- </dependency>
- <dependency>
- <groupId>
- javax.servlet.jsp
- </groupId>
- <artifactId>
- jsp-api
- </artifactId>
- <version>
- 2.2
- </version>
- <scope>
- provided
- </scope>
- </dependency>
- <!-- 导入Mysql数据库链接jar包 -->
- <dependency>
- <groupId>
- mysql
- </groupId>
- <artifactId>
- mysql-connector-java
- </artifactId>
- <version>
- 5.1.30
- </version>
- </dependency>
- <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
- <dependency>
- <groupId>
- commons-dbcp
- </groupId>
- <artifactId>
- commons-dbcp
- </artifactId>
- <version>
- 1.2.2
- </version>
- </dependency>
- <!-- JSTL标签类 -->
- <dependency>
- <groupId>
- jstl
- </groupId>
- <artifactId>
- jstl
- </artifactId>
- <version>
- 1.2
- </version>
- </dependency>
- <!-- 日志文件管理包 -->
- <!-- log start -->
- <dependency>
- <groupId>
- log4j
- </groupId>
- <artifactId>
- log4j
- </artifactId>
- <version>
- ${log4j.version}
- </version>
- </dependency>
- <!-- 格式化对象,方便输出日志 -->
- <dependency>
- <groupId>
- com.alibaba
- </groupId>
- <artifactId>
- fastjson
- </artifactId>
- <version>
- 1.1.41
- </version>
- </dependency>
- <!-- 映入JSON -->
- <dependency>
- <groupId>
- org.codehaus.jackson
- </groupId>
- <artifactId>
- jackson-mapper-asl
- </artifactId>
- <version>
- 1.9.13
- </version>
- </dependency>
- <dependency>
- <groupId>
- com.fasterxml.jackson.core
- </groupId>
- <artifactId>
- jackson-databind
- </artifactId>
- <version>
- 2.1.0
- </version>
- </dependency>
- <dependency>
- <groupId>
- com.fasterxml.jackson.core
- </groupId>
- <artifactId>
- jackson-annotations
- </artifactId>
- <version>
- 2.1.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>
- <!--Spring test相关依赖-->
- <dependency>
- <groupId>
- org.springframework
- </groupId>
- <artifactId>
- spring-test
- </artifactId>
- <version>
- 4.1.7.RELEASE
- </version>
- </dependency>
- <dependency>
- <groupId>
- junit
- </groupId>
- <artifactId>
- junit
- </artifactId>
- <version>
- RELEASE
- </version>
- </dependency>
- </dependencies>
- 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
第四步 添加各框架的配置文件
Spring与MyBatis的整合
为了方便演示,我们在新建 一个moudle,这次选择web app的模板
结构关系如下,这个moudle是创建在LearnBuildSSM的Project下的。
配置maven
最后点击完成
可以看到项目结构如下:
观察发现并没有java这个目录,这就需要我们自己手动创建了
把创建的java目录变为source root
然后同理创建test目录及java下的相关目录,最后的结构图如下:
中间可能出现一些配置,按照IDE的提示去完成就好了
(1)web-front 的resources下新建jdbc.properties(文件编码修改为utf-8),如果不存在这个目录,则自己手动创建一个directory,然后右建make directory as 选择resources root,具体配置根据本机情况调整。
- driver=com.mysql.jdbc.Driver
- url=jdbc:mysql://localhost:3306/project?useUnicode=true&characterEncoding=utf-8
- username=root
- password=123456
- #定义初始连接数
- initialSize=0
- #定义最大连接数
- maxActive=20
- #定义最大空闲
- maxIdle=20
- #定义最小空闲
- minIdle=1
- #定义最长等待时间
- maxWait=60000
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
这里需要注意,图中高亮的地方代表的是一个空格,需要删除
(2)配置log4j,方便日志的输出
参考Log4j配置详解
同样在刚才的resources目录下新建log4j.properties
- #定义LOG输出级别
- log4j.rootLogger=INFO,Console,File
- #定义日志输出目的地为控制台
- log4j.appender.Console=org.apache.log4j.ConsoleAppender
- log4j.appender.Console.Target=System.out
- #可以灵活地指定日志输出格式,下面一行是指定具体的格式
- log4j.appender.Console.layout = org.apache.log4j.PatternLayout
- log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
- #文件大小到达指定尺寸的时候产生一个新的文件
- log4j.appender.File = org.apache.log4j.RollingFileAppender
- #指定输出目录
- log4j.appender.File.File = logs/ssm.log
- #定义文件最大大小
- log4j.appender.File.MaxFileSize = 10MB
- # 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
- log4j.appender.File.Threshold = ALL
- log4j.appender.File.layout = org.apache.log4j.PatternLayout
- log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
(3)创建Spring-myBatis,此文件就是专门用来整合Spring和mybatis的
- <?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:p="http://www.springframework.org/schema/p"
- 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-3.1.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.1.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
- <!-- 自动扫描 -->
- <context:component-scan base-package="com.springmvc" />
- <!-- 引入配置文件 -->
- <bean id="propertyConfigurer"
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="location" value="classpath:jdbc.properties" />
- </bean>
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="${driver}" />
- <property name="url" value="${url}" />
- <property name="username" value="${username}" />
- <property name="password" value="${password}" />
- <!-- 初始化连接大小 -->
- <property name="initialSize" value="${initialSize}"></property>
- <!-- 连接池最大数量 -->
- <property name="maxActive" value="${maxActive}"></property>
- <!-- 连接池最大空闲 -->
- <property name="maxIdle" value="${maxIdle}"></property>
- <!-- 连接池最小空闲 -->
- <property name="minIdle" value="${minIdle}"></property>
- <!-- 获取连接最大等待时间 -->
- <property name="maxWait" value="${maxWait}"></property>
- </bean>
- <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="configLocation" value="classpath:/mybatis-config.xml"></property>
- <!-- 自动扫描mapping.xml文件 -->
- <property name="mapperLocations" value="classpath:com/springmvc/mapping/*.xml"></property>
- </bean>
- <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.springmvc.dao" />
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
- </bean>
- <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
- <bean id="transactionManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource" />
- </bean>
- </beans>
- 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
这里需要注意一点
- <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="configLocation" value="classpath:/mybatis-config.xml"></property>
- <!-- 自动扫描mapping.xml文件 -->
- <property name="mapperLocations" value="classpath:com/springmvc/mapping/*.xml"></property>
- </bean>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
这段代码是IDE自动加载map下的.xml文件,由于maven无法对xml文件进行编译打包,须在pom.xml文件中添加如下配置:
- <build>
- <finalName>API</finalName>
- <resources>
- ······
- <resource>
- <directory>src/main/java</directory>
- <includes>
- <include>**/*.xml</include>
- </includes>
- <filtering>true</filtering>
- </resource>
- ······
- </resources>
- </build>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
参考 maven项目无法打包编辑mapper下的xml文件
若果不添加,就会报找不到.xml文件(FlieNoFound)的错误
(4)使用MyBatis Generator自动创建代码
参考: SSM框架——使用MyBatis Generator自动创建代码
最后将生成的文件放到对应的目录当中:
(5)编写测试用例
- /**
- * Created by hugo on 2017/3/20.
- */
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})
- public class TestMybatis {
- private static Logger logger = Logger.getLogger(TestMybatis.class);
- @Resource
- private MessageService messageService=null;
- @Test
- public void test1(){
- Message message=messageService.getMessageById(1);
- logger.info("值:"+message.getCommand());
- }
- }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
数据库的message表也非常的简单
测试结果:
SpringMVC整合
最后我们将进行SpringMVC的整合,其配置文件单独存放,然后在web.xml当中配置整合
(1)配置Spring-mvc.xml
- <?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:p="http://www.springframework.org/schema/p"
- 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-3.1.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.1.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
- <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
- <context:component-scan base-package="com.springmvc.controller" />
- <!--避免IE执行AJAX时,返回JSON出现下载文件 -->
- <bean id="mappingJacksonHttpMessageConverter"
- class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
- <property name="supportedMediaTypes">
- <list>
- <value>text/html;charset=UTF-8</value>
- </list>
- </property>
- </bean>
- <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
- <bean
- class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
- <property name="messageConverters">
- <list>
- <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->
- </list>
- </property>
- </bean>
- <!-- 定义跳转的文件的前后缀 ,视图模式配置-->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
- <property name="prefix" value="/WEB-INF/page/" />
- <property name="suffix" value=".jsp" />
- </bean>
- <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
- <bean id="multipartResolver"
- class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- <!-- 默认编码 -->
- <property name="defaultEncoding" value="utf-8" />
- <!-- 文件大小最大值 -->
- <property name="maxUploadSize" value="10485760000" />
- <!-- 内存中的最大值 -->
- <property name="maxInMemorySize" value="40960" />
- </bean>
- </beans>
- 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
(2) 配置web.xml
- <!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 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
- version="3.0">
- <display-name>Archetype Created Web Application</display-name>
- <!-- Spring和mybatis的配置文件 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring-mybatis.xml</param-value>
- </context-param>
- <!--Log4j配置-->
- <context-param>
- <param-name>log4jRefreshInterval</param-name>
- <param-value>60000</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
- </listener>
- <!-- 编码过滤器 -->
- <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监听器 ApplicationContext 载入 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- 防止Spring内存溢出监听器 -->
- <listener>
- <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
- </listener>
- <!-- Spring MVC 核心配置开始 -->
- <servlet>
- <servlet-name>mvc-dispatcher</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <!-- 可以通过contextConfigLocation来自定义SpringMVC配置文件的位置,如不指定,则默认在WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,
- 此时文件名必须为[<servlet-name>]-servlet.xml,否则会出错-->
- <!--以下init-param是自定义SpringMVC的配置文件的位置 -->
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring-mvc.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <!-- 拦截设置 -->
- <servlet-mapping>
- <servlet-name>mvc-dispatcher</servlet-name>
- <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- <!-- Spring MVC 核心配置结束 -->
- <!-- 配置SESSION超时,单位是分钟 -->
- <session-config>
- <session-timeout>15</session-timeout>
- </session-config>
- </web-app>
- 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
具体说明详见注释
(3) 测试
我们在controller中新建一个MessageController
- @Controller
- @RequestMapping("/message")
- public class MessageController {
- @Resource
- private MessageService messageService;
- @RequestMapping(value = "/show",method = RequestMethod.GET)
- public @ResponseBody
- Message helloMvc(HttpServletRequest request, Model model){
- int id = Integer.parseInt(request.getParameter("id"));
- Message mes=this.messageService.getMessageById(id);
- model.addAttribute("message",mes);
- return mes;
- }
- }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
然后讲web-front项目添加进tomcat当中
运行项目,在浏览器输入url,得到如下结果:
至此,SSM框架就全部集成完毕了~
总结
最后,再来简单概括一下通过intellij idea 来实现SSM框架集成的步骤
1、新建Maven项目。这里需要通过groupId和ArtifactId来处理好module与moudle之间的关系,同时需要修改intellij idea中的maven配置,重点是要修改setting.xml当中的镜像地址,以方便我们可以快速地下载第三方的类库
2、添加类库。当项目新建完毕后,接下来就是在项目的pom.xml中添加相关类库的依赖,具体这些依赖是添加在工程下的pom.xml里,还是添加在某一个moudle当中的pom.xml需要视整体系统的设计而定
3、配置MyBatis、SpringMVC、log4j和项目的web.xml来完成最终的框架整合。
【参考】
SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
使用maven创建web项目
intellij idea下使用Maven搭建SpringMVC
IDEA搭建SpringMVC并用maven配置的小例子(非常详细)
关于Java Servlet的中文乱码
百度搜索“就爱阅读”,专业资料,生活学习,尽在就爱阅读网92to.com,您的在线图书馆!