最近接到一个项目,决定拿maven构建项目,框架用SSM,结果在环境搭建上就遇到了不少麻烦,就写这篇博客记录一下,防止以后忘记。
声明几点:1. 这篇博客默认各位已经装好MyEclipse和Maven2.这篇博客里的各种配置文件都只配置了最基本的东西,SSM整合有很多可选的配置,在用某个框架的某些功能的时候可能因为某些配置没有配而出错这是正常现象。。。好了,废话到此为止,下面正式开始搭建项目!
3.1.1.RELEASE
3.2.8
1.7.12
1.2.17
junit
junit
4.11
test
javax
javaee-api
6.0
org.springframework
spring-test
${spring.version}
org.springframework
spring-core
${spring.version}
org.springframework
spring-oxm
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-expression
${spring.version}
org.springframework
spring-orm
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.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
我这里只配置了基本信息,数据库连接还有很多可选配置,如连接池初始连接数量,增加连接数量等,这些都有默认值,不一定要配。
这里不要照搬我的代码,需要你配置自己的数据库连接信息,如用户名、密码、数据库名等
log4j.properties是我从网上找的一份,配置的比较多,相应的调试时打印的信息也比较多,便于排错。下面是全部代码:
log4j.rootLogger=DEBUG, stdout, fileout , logfile
log4j.logger.com.fiscal = DEBUG
log4j.logger.com.system = DEBUG
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, fileout
log4j.logger.java.sql.ResultSet = DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=D:/zpssoa.log
log4j.appender.logfile.MaxFileSize=512KB
log4j.appender.logfile.MaxBackupIndex=3
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
spring.xml配置如下,里面有注释,有spring和mybatis经验的同学应该能看懂,就不在这里介绍了
/index.jsp
contextConfigLocation
classpath:spring.xml
org.springframework.web.context.ContextLoaderListener
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
true
encoding
UTF-8
encodingFilter
/*
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
SpringMVC
*.action
package com.zlw.test.entity;
public class User {
private int id;
private String username;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserame() {
return username;
}
public void setUserame(String name) {
this.username = name;
}
}
package com.zlw.test.mapper;
public interface UserMapper {
String findUserById(int id);
}
package com.zlw.test.service;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.zlw.test.entity.User;
import com.zlw.test.mapper.UserMapper;
@Service
public class UserService {
@Resource
private UserMapper userMapper;
public String findUserById(int id) {
return userMapper.findUserById(id);
}
}
package com.zlw.test.action;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zlw.test.entity.User;
import com.zlw.test.service.UserService;
@Controller
@RequestMapping("/user")
public class UserAction {
@Resource
private UserService userService;
@RequestMapping("/findUserById")
public String findUserById(int id, Model model) {
String username = userService.findUserById(id);
if (username != null) {
model.addAttribute("username", username);
} else {
model.addAttribute("username", "未找到");
}
return "test";
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
测试页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
测试页面
成功 名字是:${username }