用eclipse或idea創建一個web工程,在這裡演示使用idea,項目命名為ssm
根據習慣,簡歷包目錄,分別為
config在WEB-INF下建立兩個目錄
lib一共需要如下jar包
spring所有包 mybatis spring-mybatis整合包 aspectj jstl log4j c3p0 mysql百度雲下載 鏈接:http://pan.baidu.com/s/1dDB8cxV 密碼:3cqp
在config.mybatis下創建mybatis.xml配置文件,SSM整合和mybatis在不需要二級緩存等配置時,mybatis配置文件只需要一個框架,不需要書寫具體內容,框架如下
在config.spring下創建springmvc.xml文件,用來配置springmvc
applicationContext-dao.xml
在config.spring下創建applicationContext-dao.xml文件,用來配置DAO層的內容,在這裡連接數據庫的數據源使用C3P0,所以在這之前先在config中添加db.properties來指定數據庫連接信息
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://127.0.0.1:3306/mybatis?characterEncoding=UTF-8
jdbc.username = root
jdbc.password = root
applicationContext-dao.xml內容:
applicationContext-service.xml
在config.spring中添加applicationContext-service.xml,用來把service層的javabean注入到IOC容器中
applicationContext-transtion.xml
在config.spring中添加applicationContext-transtion.xml,用來配置AOP事務
為了方便mybatis的調試,使用log4j作為日志輸出,在src根目錄中添加log4j.properties
log4j.rootLogger=DEBUG,Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.org.apache=INFO
web.xml需要實現三個功能
配置spring文件
contextConfigLocation
classpath:cn/elinzhou/OrderSpringMVC/config/spring/applicationContext-*.xml
org.springframework.web.context.ContextLoaderListener
配置springmvc中的前端控制器
dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:cn/elinzhou/OrderSpringMVC/config/spring/springmvc.xml
1
dispatcher
/
解決post亂碼問題
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
CharacterEncodingFilter
/*
這裡使用一張用戶表做示例,表結構如下
DROP TABLE IF EXISTS `user`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(32) NOT NULL COMMENT '用戶名稱',
`sex` char(1) default NULL COMMENT '性別',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8;
根據表結構建立POJO類
package cn.elinzhou.OrderSpringMVC.pojo;
import java.util.Date;
public class User {
private Integer id;
private String username;
private String sex;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username == null ? null : username.trim();
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex == null ? null : sex.trim();
}
}
mapper.java
package com.elin4it.ssm.mapper;
import com.elin4it.ssm.pojo.User;
import java.util.List;
public interface UserMapper {
//查詢用戶列表
List findUser() throws Exception;
}
mapper.xml
創建一個service接口,添加一個方法,用來查詢所用用戶
UserService.java
package com.elin4it.ssm.service;
import com.elin4it.ssm.pojo.User;
import java.util.List;
/**
* Created by elin on 15-7-1.
*/
public interface UserService {
List findUser()throws Exception;
}
添加UserService的實現類UserServiceImpl.java
package com.elin4it.ssm.service;
import com.elin4it.ssm.mapper.UserMapper;
import com.elin4it.ssm.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Description: UserServiceImpl
* Author: Elin Zhou
* Create: 2015-07-01 20:59
*/
@Service
public class UserServiceImpl implements UserService {
//自動注入mapper接口
@Autowired
private UserMapper userMapper;
@Override
public List findUser() throws Exception {
//調用mapper接口中的方法,獲取用戶列表
List users = userMapper.findUser();
return users;
}
}
創建後端控制器,前端控制器根據用戶請求映射到後端控制器中的方法,並且返回視圖和數據來渲染視圖
package com.elin4it.ssm.controller;
import com.elin4it.ssm.pojo.User;
import com.elin4it.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.servlet.ModelAndView;
import java.util.List;
import java.util.Map;
/**
* Description: UserController
* Author: Elin Zhou
* Create: 2015-07-01 19:30
*/
@Controller
@RequestMapping(/user)
public class UserController {
//自動注入service
@Autowired
private UserService userService;
@RequestMapping(/findUser)
public String findUser(Map map) throws Exception{
//調用service中的查詢用戶列表方法來獲取用戶列表
List users = userService.findUser();
//把用戶列表放到形參傳入的map中,相當於執行request.addAttribute()方法
map.put(users,users);
return user/userList;
}
}
在WEB-INF的views文件夾中添加user文件加,用來存放用戶相關的視圖,在添加一個userList.jsp文件
<%@ taglib prefix=c uri=http://java.sun.com/jsp/jstl/core %>
<%--
Created by IntelliJ IDEA.
User: elin
Date: 15-7-1
Time: 下午8:53
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType=text/html;charset=UTF-8 language=java %>
userList
id
username
sex
${i.id}
${i.username}
${i.sex}