[【SSM】Eclipse使用Maven创建Web项目+整合SSM框架]
自己接触ssm框架有一段时间了,从最早的接触新版ITOO项目的(SSM/H+Dobbu zk),再到自己近期来学习到的《淘淘商城》一个ssm框架的电商项目。用过,但是还真的没有自己搭建过,一直都是用别人搭建好的。所以,从网上找了一些材料,结合自己来解决过程中的一些问题,使自己对ssm框架理解更加深了,不为分享,只是总结一下,下次有机会再搭建的时候,直接看自己的博客,省得找了!
一、准备环境:
maven:apache-maven-3.2.3
jdk:jdk1.8.0_25
tomcat:tomcat-6.0.37
二、配置Maven、jdk
1、Window——>Preferences——>Maven——>设置自己的Settings
2、Window——>Preferences——>Java——>Installed JREs——>Add
配置好maven之后,就可以在本地设置好的地方看到对应的Maven文件了,还有包含resp文件夹
三、新建Maven项目:
1、右击——>New(或者是File——>New)——>other——>Maven——>Maven Project——>Next
2、这时候,我们就成功创建出一个Maven项目了,项目结构如下图:
3、右击项目,选择Properties进行一些配置:
到这里,我们的Maven项目就建好了,接下来,我们来整合搭建SSM([spring](http://lib.csdn.net/base/javaee) MVC + Spring + Mybatis)
首先我们来配置一些整合框架所必须要的jar包,也就是来配置我们的pom文件,然后直接alt+f5,保存后,就会自动下载,存放在之前配置好的本地maven仓库中。
pox.xml配置依赖的内容:
4.0.0
com.ssm
Maven_Project
war
0.0.1-SNAPSHOT
Maven_Project Maven Webapp
http://maven.apache.org
4.0.2.RELEASE
3.2.8
1.7.12
1.2.17
junit
junit
4.11
test
javax
javaee-api
7.0
org.springframework
spring-test
${srping.version}
org.springframework
spring-core
${srping.version}
org.springframework
spring-oxm
${srping.version}
org.springframework
spring-tx
${srping.version}
org.springframework
spring-jdbc
${srping.version}
org.springframework
spring-aop
${srping.version}
org.springframework
spring-context
${srping.version}
org.springframework
spring-context-support
${srping.version}
org.springframework
spring-expression
${srping.version}
org.springframework
spring-orm
${srping.version}
org.springframework
spring-web
${srping.version}
org.springframework
spring-webmvc
${srping.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.2.2
mysql
mysql-connector-java
5.1.35
commons-dbcp
commons-dbcp
1.4
jstl
jstl
1.2
log4j
log4j
${log4j.version}
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
com.alibaba
fastjson
1.2.6
org.codehaus.jackson
jackson-mapper-asl
1.9.13
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.4
commons-codec
commons-codec
1.10
Maven_Project
2、在src/main/resources下添加配置文件:applicationContext.xml:
配置数据库连接池:jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/maven_ssm
username=root
password=root
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000
配置日志: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
配置我们的ioc注入:spring-dao.xml
spring-db.xml
配置spring的事务管理器:spring-tx.xml
接下来,我们来整合spring mvc:spring-mvc.xml:
text/html;charset=UTF-8
修改web.xml
Archetype Created Web Application
/index.jsp
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
true
encoding
UTF-8
encodingFilter
/*
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/spring-mvc.xml
1
true
SpringMVC
/
到这里,我们的ssm框架就完成了,下面,我们来个实现:
在src/main/java下分别添加我们的dao包,service包,还有controller包.包里面添加相应的类,效果图如下:
UserController.java:
package com.ssm.controller;
import java.util.List;
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 com.ssm.dto.User;
import com.ssm.service.IUserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private IUserService userService;
@RequestMapping("/userList")
public String userList(HttpServletRequest request,Model model){
List uList = userService.getAllUser();
model.addAttribute("uList", uList);
return "userList";
}
@RequestMapping("/showUser")
public String showUser(HttpServletRequest request,Model model){
int userId = Integer.parseInt(request.getParameter("id"));
User user = userService.getUserById(userId);
model.addAttribute("user", user);
return "showUser";
}
@RequestMapping("/addUserUI")
public String addUserUI(){
return "addUser";
}
@RequestMapping("/addUser")
public String addUser(HttpServletRequest request,Model model){
User user = new User();
user.setName(String.valueOf(request.getParameter("name")));
user.setPassword(String.valueOf(request.getParameter("password")));
user.setAge(Integer.parseInt(String.valueOf(request.getParameter("age"))));
userService.addUser(user);
return "redirect:/user/userList";
}
}
IUserDao.java:
package com.ssm.dao;
import java.util.List;
import java.util.Map;
import com.ssm.dto.User;
public interface IUserDao {
public User queryByPrimaryKey(Integer id);
public List queryUserByBatch(Map params);
public void insertUser(User user);
public void insertUserByBatch(List list);
public void deleteByPrimaryKey(Integer id);
public void delteUserByBatch(Map params);
public void updateByPrimaryKey(Integer id);
public List getAllUser();
}
User.java:
package com.ssm.dto;
public class User {
private Integer id;
private String name;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
}
UserMapper.xml:
id, name, password, age
insert into user
id,
name,
password,
age,
#{id,jdbcType=INTEGER},
#{name,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER},
insert into USER
(ID,
NAME,
PASSWORD,
AGE)
select A.* from
(
select
#{user.id,jdbcType=INTEGER},
#{user.name,jdbcType=VARCHAR},
#{user.password,jdbcType=VARCHAR},
#{user.age,jdbcType=INTEGER}
from dual
) A
delete from user
where id = #{id,jdbcType=INTEGER}
delete from user
where id IN
#{ids,jdbcType=DECIMAL}
update user
name = #{name,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER},
where id = #{id,jdbcType=INTEGER}
IUserService.java:
package com.ssm.service;
import java.util.List;
import com.ssm.dto.User;
public interface IUserService {
public User getUserById(int userId);
public void insertUser(User user);
public void addUser(User user);
public List getAllUser();
}
UserServiceImpl.java:
package com.ssm.service.Impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.ssm.dao.IUserDao;
import com.ssm.dto.User;
import com.ssm.service.IUserService;
@Service("userService")
public class UserServiceImpl implements IUserService {
@Resource
private IUserDao userDao;
public User getUserById(int userId) {
return userDao.queryByPrimaryKey(userId);
}
public void insertUser(User user) {
userDao.insertUser(user);
}
public void addUser(User user) {
userDao.insertUser(user);
}
@Override
public List getAllUser() {
return userDao.getAllUser();
}
}
接下来就是我们的静态页面了jsp文件了,在WEB-INF文件夹下新建jsp文件夹,添加userinfo.jsp
userinfo.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath=request.getSSScheme()+"://request.getServerName()"+":"+request.getServerPort+Path+
"/"%>
My JSP 'userinfo.jsp' starting page
哦,忘记配置tomcat,配置一下tomcat,然后部署发布项目,进行测试
到此,框架成
总结: 真心知道用maven之后,我们搭框架真的方便多了,最起码不用个顶个的去找jar包,然后逐一的 导入,只需要在pom文件里面配置一下依赖,就好了。虽然说比较简单,可是可能是自己没有动手弄过吧,期间出了各种各样的错,或者说是失误,但是,总之,耗时一天半,总算是通了,这也是收获, 加油吧,少(qing)年。