1、前言
在工作中,自己使用的持久化框架并不是MyBatis,而是hibernate+Spring data JPA,但是觉得MyBatis还是挺不错的,故准备自己搭建一个SSM框架学习备用,本文只介绍最基本的SSM整合。
<!--more-->
分享,让你我共同进步!本文章还是比较长的,请耐心看。我的个人博客,欢迎常来哟
2、框架介绍
Spring:Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。 简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
Spring MVC:Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。
MyBatis:MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。MyBatis是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO)MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
3、准备工作
3.1开发环境
本人使用的是jdk1.8+eclipse+maven+tomcat7.0.23,具体如何配置,本文就不细说了。
3.2创建maven工程
本项目使用的是maven,maven会自动下载依赖jar包非常方便,而我使用的是eclipse来创建maven项目。
1、File->new->other
2、找到maven,选择maven project,next
3、把create simple project勾上,next
4、填写组织名和项目名,packaging类型选择war,因为要运行在tomcat上,最后点击finish完成。
5、创建完的项目可以看到是有错误的,出现这个错误的原因是Maven不支持缺少web.xml的web项目。
解决方法如下:对项目右键->Java EE Tools->Generate Deployment Descriptor Stub,这样就在src\main\webapp下面生成了WEB-INF文件夹和web.xml,问题解决
3.3整合所需要引入的JAR包
pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xyz.heykin</groupId>
<artifactId>SSM</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!-- Spring、MyBatis版本号及log4j日志管理包版本 -->
<properties>
<spring.version>4.1.4.RELEASE</spring.version>
<mybatis.version>3.2.6</mybatis.version>
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<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-aop</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-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>
<!-- 导入java ee jar 包 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</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>
<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>
<!-- 上传组件包 -->
<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>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.7.5</version>
</dependency>
</dependencies>
</project>
3.4使用MyBatis Generator自动创建代码
由于MyBatis属于一种半自动的ORM框架,所以主要的工作就是配置Mapping映射文件,但是由于手写映射文件很容易出错,所以可利用MyBatis生成器自动生成实体类、DAO接口和Mapping映射文件。这样可以省去很多的功夫,将生成的代码copy到项目工程中即可。
而在本项目中在测试Spring、MyBatis的整合是否成功,也将使用到此工具,可暂时跳过此步,等下回来看。
具体的使用请参照以下链接:https://heykin.github.io/2018/01/24/MyBatis自动生成代码/
下载工具包链接
4、整合Spring、MyBatis
本人整理了一份思维导图,按着导图顺序,基本可以将项目整合完毕!下面的代码我也没加上注释,因为导图上基本有解析了,我也是按自己的图搭出来的
项目的基本配置文件和包架构
4.1建立连接池参数配置文件
db.properties
如果想输入中文注释,需要右键文件>Properties>选为utf-8格式
#连接池参数配置
username=root
password=123456
url=jdbc:MySQL://localhost:3306/test1
driverClassName=com.mysql.jdbc.Driver
initSize=10
maxActive=100
#maxldle=30
#minldle=5
#maxWait=5000
#removeAbandoned=true
#removeAbandonedTimeout=3000
#logAbandoned=false
4.2建立spring-mybaits.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<util:properties id="dbConfig" location="classpath:config/db.properties"></util:properties>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="#{dbConfig.username}" />
<property name="password" value="#{dbConfig.password}" />
<property name="url" value="#{dbConfig.url}" />
<property name="driverClassName" value="#{dbConfig.driverClassName}" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:xyz/heykin/*/mapping/*.xml"/>
</bean>
<bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="xyz.heykin.*.repository" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<aop:config>
<aop:pointcut id="pointCut" expression="execution(* xyz.heykin.*.service.impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="upd*" propagation="REQUIRED" />
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="xyz.heykin.*.service.impl">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
4.3建立log4j配置文件
为了方便调试,一般都会使用日志来输出信息,Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件,甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等;我们也可以控制每一条日志的输出格式;通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。
log4j.properties
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
这个log4j的配置在大部分项目都可以使用,无需在更改,如果想更深入理解log4j的配置可以参考这里。
4.4测试
创建测试表
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`userid` varchar(64) NOT NULL,
`username` varchar(64) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
从上图可以看到我个人放置文件的整体架构,其中实体类 、DAO、XML文件可以通过上面3.4的MyBatis Generator工具生成!
User.java
package xyz.heykin.test.persistence;
import java.io.Serializable;
public class User implements Serializable{
private String userid;
private String username;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid == null ? null : userid.trim();
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username == null ? null : username.trim();
}
}
UserMapper.xml
<?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="xyz.heykin.test.repository.UserDao" >
<resultMap id="BaseResultMap" type="xyz.heykin.test.persistence.User" >
<result column="userid" property="userid" jdbcType="VARCHAR" />
<result column="username" property="username" jdbcType="VARCHAR" />
</resultMap>
<insert id="insert" parameterType="xyz.heykin.test.persistence.User" >
insert into user (userid, username)
values (#{userid,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="xyz.heykin.test.persistence.User" >
insert into user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="userid != null" >
userid,
</if>
<if test="username != null" >
username,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="userid != null" >
#{userid,jdbcType=VARCHAR},
</if>
<if test="username != null" >
#{username,jdbcType=VARCHAR},
</if>
</trim>
</insert>
</mapper>
UserDao.java
package xyz.heykin.test.repository;
import xyz.heykin.test.persistence.User;
public interface UserDao {
public void insert(User user);
}
IUserService.java
,此次加多个方法测试我们的事务配置
package xyz.heykin.test.service;
import org.springframework.transaction.annotation.Transactional;
public interface IUserService {
@Transactional
public void addUser();
public void user();
}
UserServiceImpl.java
package xyz.heykin.test.service.impl;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.heykin.test.persistence.User;
import xyz.heykin.test.repository.UserDao;
import xyz.heykin.test.service.IUserService;
@Service("userService")
public class UserServiceImpl implements IUserService{
private static Logger logger = Logger.getLogger(UserServiceImpl.class);
@Autowired
private UserDao userDao;
public void addUser() {
User user = new User();
user.setUserid("1111");
user.setUsername("heykin");
userDao.insert(user);
logger.info(user.getUsername());
}
public void user() {
User user = new User();
user.setUserid("11112");
user.setUsername("heykin1");
userDao.insert(user);
logger.info(user.getUsername());
}
}
测试类test.java
package xyz.heykin.test.mybatis;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import xyz.heykin.test.service.IUserService;
public class test {
@Test
public void test(){
ApplicationContext ac = new ClassPathXmlApplicationContext("spring-mybatis.xml");
IUserService userService = (IUserService) ac.getBean("userService");
userService.addUser();
userService.user();
}
}
最后预期执行成果,user()方法报错,提示read-only,只插入一条数据。这是正确的,因为我们指定了几个固定的方法头开始的方法才能增删改。
5、Spring与Spring MVC的整合
Spring与MyBatis的整合已完成,我们的目标与完成一半,接下来就是Spring与Spring MVC,还有配置freemarker了,不多废话,just do it~~
5.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: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-3.1.xsd">
<context:property-placeholder location="classpath:config/config.properties" ignore-unresolvable="true" />
<context:component-scan base-package="xyz.heykin.*.action" use-default-filters="true">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<bean id="freemarkerConfig" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:config/freemarker.properties"/>
</bean>
<bean id="freemarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="freemarkerSettings" ref="freemarkerConfig" />
<property name="templateLoaderPath" value="/WEB-INF/views/" />
<property name="freemarkerVariables">
<map>
<entry key="xml_escape" value-ref="fmXmlEscape" />
</map>
</property>
</bean>
<bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" />
<bean id="freemarkerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
<property name="viewNames" value="*.ftl" />
<property name="contentType" value="text/html;charset=utf-8" />
<property name="cache" value="true" />
<property name="order" value="0" />
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
<property name="order" value="1" />
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
<property name="maxUploadSize" value="20000000" />
<property name="maxInMemorySize" value="40960" />
</bean>
<mvc:resources mapping="/static/**" location="/resources/" />
<mvc:default-servlet-handler />
<mvc:view-controller path="/" view-name="redirect:/index" />
</beans>
5.2freemarker配置文件
freemarker.properties
tag_syntax=auto_detect
default_encoding=UTF-8
output_encoding=UTF-8
locale=zh_CN
date_format=yyyy-MM-dd
time_format=HH:mm:ss
datetime_format=yyyy-MM-dd HH:mm:ss
number_format=#.####
#template_exception_handler=ignore
#template_exception_handler=debug
template_exception_handler=html_debug
#template_exception_handler=rethrow
cache_storage=strong:5000, soft
template_update_delay=1
#set null value default is null string
classic_compatible=true
5.3配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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_2_5.xsd" version="2.5">
<display-name>SSM</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<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-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.json</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
此处,如果把spring-mvc.xml
更名为springMVC-servlet.xml
,然后放在与web.xml
同级目录,那么下面的这段代码是可以去掉的!
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
5.4测试
TestAction.java
package xyz.heykin.test.action;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value="/test")
public class TestAction {
@RequestMapping(value="/index")
public String index(HttpServletRequest request,
HttpServletResponse response, ModelMap map){
HashMap hmap = new HashMap();
hmap.put("row", "你好");
map.putAll(hmap);
return "index";
}
@RequestMapping(value="/ajaxtest")
@ResponseBody
public Object ajaxtest(){
HashMap hmap = new HashMap();
hmap.put("row", "中文测试");
return hmap;
}
}
并新建一个index.jsp文件,在这里我就不测关于MyBatis方面的了,剩下的交给你们了 = =
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<html>
<head>
<title>测试</title>
</head>
<body>
${row}
</body>
</html>
6、结尾
至此,SSM项目的整合已完成。如果运行不成功,那就继续寻找BUG吧,把BUG统统干掉~~