目的
写出一个demo,整合SSM三大框架:整合中创建了3个xml文件和2个资源文件,分别为:spring.xml
、spring-springmvc.xml
、spring-mybatis.xml
和jdbc.properties
、log4j.properties
,并且在web.xml文件中将以上配置文件进行整合。
1. 创建Maven工程
出现错误:情况一
解决方法:在pom.xml
文件中添加:
javax
javaee-api
7.0
情况二:右击项目点Properties
,修改如下:
- Resource中将encoding改为UTF-8
- Java Build path中将JRE System Library改为1.8
- Java Compiler中将JRE改为1.8
- Project Facts中:
- 将Java改为1.8
- 将Dynamic Web Module改为3.1注意先选住Java不选Dynamic Web Module后Apply,再选住Dynamic Web Module,在Content directory中写入src/main/webapp,并选住Generate web.xml deployment descriptor,如下图所示:
情况三:
解决方案:
在
pom.xml
文件中添加:
org.apache.maven.plugins
maven-compiler-plugin
3.1
1.8
org.mortbay.jetty
maven-jetty-plugin
6.1.26
/
8480
修改web.xml
中web-app 标签内容:
spring
maven项目创建完成!
2. Maven的依赖包
maven资源包下载地址
junit
junit
4.12
test
log4j
log4j
1.2.16
com.alibaba
druid
0.2.9
org.aspectj
aspectjweaver
1.8.9
com.alibaba
fastjson
1.2.24
org.slf4j
slf4j-api
1.7.24
org.slf4j
slf4j-log4j12
1.7.24
mysql
mysql-connector-java
6.0.4
jstl
jstl
1.2
org.springframework
spring-webmvc
4.3.1.RELEASE
org.springframework
spring-aspects
4.3.1.RELEASE
org.springframework
spring-web
4.3.1.RELEASE
org.springframework
spring-core
4.3.1.RELEASE
org.springframework
spring-oxm
4.3.1.RELEASE
org.springframework
spring-tx
4.3.1.RELEASE
org.springframework
spring-jdbc
4.3.1.RELEASE
org.springframework
spring-aop
4.3.1.RELEASE
org.springframework
spring-test
4.3.1.RELEASE
org.springframework
spring-context
4.3.1.RELEASE
org.springframework
spring-expression
4.3.1.RELEASE
org.springframework
spring-beans
4.3.1.RELEASE
org.springframework
spring-context-support
4.3.1.RELEASE
org.mybatis
mybatis
3.4.2
org.mybatis
mybatis-spring
1.3.1
javax
javaee-api
7.0
com.alibaba
druid
0.2.9
org.aspectj
aspectjweaver
1.8.9
org.codehaus.jackson
jackson-core-asl
1.9.13
com.fasterxml.jackson.core
jackson-annotations
2.9.0.pr1
com.fasterxml.jackson.core
jackson-core
2.9.0.pr1
com.fasterxml.jackson.core
jackson-databind
2.9.0.pr1
commons-fileupload
commons-fileupload
1.3.2
commons-io
commons-io
2.4
commons-codec
commons-codec
1.9
commons-dbcp
commons-dbcp
20030825.184428
commons-logging
commons-logging
1.2
aopalliance
aopalliance
1.0
javax.servlet
javax.servlet-api
3.1.0
provided
org.apache.maven.plugins
maven-compiler-plugin
3.1
1.8
org.mortbay.jetty
maven-jetty-plugin
6.1.26
/
8480
3. 将spring.xml
,spring-mybatis.xml
,spring-springmvc.xml
整合后的web.xml
spring
contextConfigLocation
classpath:spring.xml,classpath:spring-mybatis.xml
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
true
encoding
UTF-8
encodingFilter
/*
org.springframework.web.context.ContextLoaderListener
org.springframework.web.util.IntrospectorCleanupListener
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
SpringMVC
*.do
/index.jsp
15
4. Spring和Mybatis整合
jdbc.properties
validationQuery=SELECT 1
jdbc_url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=UTC
jdbc_username=root
jdbc_password=123456
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
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
spring.xml
spring-mybatis.xml
sy.service.*
mybatis的generator生成工具得到
结构目录:
sy.service中UserService.java
package sy.service;
import sy.model.User;
public interface UserService {
public User getUserById(int userId);
}
sy.service中UserServiceImpl.java
package sy.service;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import sy.dao.UserMapper;
import sy.model.User;
@Service("userService")
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
public User getUserById(int userId) {
// TODO Auto-generated method stub
return userMapper.selectByPrimaryKey(userId);
}
}
junit测试代码testMybatis.java
package sy.testMybatis;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import sy.model.User;
import sy.service.UserService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:/spring.xml", "classpath*:/spring-mybatis.xml" })
public class testMybatis {
private UserService userService;
public UserService getUserService() {
return userService;
}
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}
@Test
public void test() {
// ApplicationContext ac= new ClassPathXmlApplicationContext(new
// String[]{"spring.xml","spring-mybatis.xml"});
// UserService userService=(UserService)ac.getBean("userService");
User u = userService.getUserById(1);
System.out.println(u.getUsername());
}
}
5. Spring和 Spring-springmvc整合
spring-springmvc.xml
text/html;charset=UTF-8
UTF-8
32505856
4096
sy.controller中UserController.java
package sy.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import sy.model.User;
import sy.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@RequestMapping("/showUser")
public String toIndex(HttpServletRequest request, Model model) {
int userId = Integer.parseInt(request.getParameter("id"));
User user = this.userService.getUserById(userId);
model.addAttribute("user", user);
return "showUser";
}
}
jsp前端页面
showUser.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
Insert title here
${user.username}
启动tomcat,输入http://localhost:8080/SpringMVC/user/showUser.do?id=1
SSM配置成功!