博主一直想找个机会来整理一下以前的学习笔记,最近刚好有时间,就索性以SSM框架为基础,然后逐步的集成一些博主在工作中常用的技术。
系统:window7
IDE:Myeclipse2014
JDK版本:1.8
tomcat:8
maven:3.3
myeclipse配置maven方式请自行百度,新建maven项目,选择webapp。
新建的项目目录基本机构
这里主要是整合过程,maven的讲解就不细说了。
4.0.0
com.w3cjava
W3cJavaSSM
war
0.0.1-SNAPSHOT
W3cJavaSSM
http://www.w3cjava.com
UTF-8
4.1.4.RELEASE
2.5.0
junit
junit
3.8.1
test
org.springframework
spring-core
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-test
${spring.version}
test
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-orm
${spring.version}
org.mybatis
mybatis-spring
1.2.4
log4j
log4j
1.2.17
mysql
mysql-connector-java
5.1.34
org.mybatis
mybatis
3.3.1
com.alibaba
druid
1.0.18
com.alibaba
fastjson
1.2.3
com.fasterxml.jackson.core
jackson-annotations
${jackson.version}
com.fasterxml.jackson.core
jackson-core
${jackson.version}
com.fasterxml.jackson.core
jackson-databind
${jackson.version}
org.aspectj
aspectjweaver
1.8.4
javax.servlet
jstl
1.2
org.apache.commons
commons-lang3
3.4
commons-fileupload
commons-fileupload
1.3.1
W3cJavaSSM
org.apache.maven.plugins
maven-compiler-plugin
1.7
mybatis.xml在spring-context.xml中已公国import引入,之所以分开配置,主要是考虑到这个文件配置的主要是数据库相关的信息。一些文件配置的细节说明都在xml文件里有说明,就不细说了。
#Mysql驱动
jdbc.driverClass=com.mysql.jdbc.Driver
#数据源
jdbc.url=jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&characterEncoding=utf8
jdbc.user=root
jdbc.password=123456
log4j.rootLogger=debug, stdout, logfile
log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.org.springframework=DEBUG
log4j.logger.org.compass=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%t] [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=empi.log
最后一步,就是web.xml配置了
W3cJavaSSM
contextConfigLocation
classpath:spring-context*.xml
org.springframework.web.context.ContextLoaderListener
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
SpringMVC
/
-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', '1', '1');
INSERT INTO `user` VALUES ('2', '2', '2');
package com.w3cjava.pojo;
public class User{
private String id;
private String name;
private Integer age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package com.w3cjava.dao;
import java.util.List;
import org.springframework.stereotype.Component;
import com.w3cjava.pojo.User;
@Component
public interface UserMapper {
public List getAllUsers();
public User findById(String id);
}
映射文件UserMapper.xml所在位置在mybatis.xml文件中有配置,一般会选择配置在resources下建立mapping目录下。
id, name, age
package com.w3cjava.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.w3cjava.dao.UserMapper;
import com.w3cjava.pojo.User;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List getAllUsers(){
return userMapper.getAllUsers();
}
public User findById(String id){
return userMapper.findById(id);
}
}
package com.w3cjava.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.w3cjava.pojo.User;
import com.w3cjava.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value={"list",""})
public String index(Model model){
List allUsers = userService.getAllUsers();
model.addAttribute("allUsers", allUsers);
return "user/userList";
}
@RequestMapping(value={"add"})
public String add(){
return "user/userAdd";
}
@RequestMapping(value={"edit"})
public String edit(User user){
User user2 = userService.findById(user.getId());
return "user/userEdit";
}
@RequestMapping(value={"delete"})
public String delete(User user){
userService.findById(user.getId());
return "user/userList";
}
}
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ include file="/static/include/taglib.jsp"%>
w3cjava
新增
ID
登录名
工号
编辑
删除
${user.id}
${user.name}
${user.age}
编辑
删除
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ include file="/static/include/taglib.jsp"%>
新增
新增
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ include file="/static/include/taglib.jsp"%>
新增
编辑
页面中通过include引入taglib.jsp,主要作用是把常用的页面所需配置项放到这里。
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
至此,SSM框架已经完成整合,完整的目录机构:
下面通过Tomcat启动项目,访问http://localhost:8080/user截图如下
SSM整合源码:Github,这篇整合的源码在bak文件夹下