Maven多模块SSM框架搭建

整合SSM和Maven多模块,搭建一个简单的开发架构,具体的做法如下:
1、首先建一个Maven的项目,使用eclipse新建other->Maven Project,类型必须选择为pom。创建成功后的pom.xml代码如下:



4.0.0
com.cn.itava
Itava-main
0.0.1-SNAPSHOT
pom


2、在此基础上面新建各个Maven子模块,Web模块创建的时候选择pom类型为war,其他的为默认类型jar。
[img]http://dl2.iteye.com/upload/attachment/0121/7338/2ed95c1b-ba24-35a9-8ab9-5bfe1a736d2f.png[/img]
创建成功后在父模块会映射出这些包路径。
[img]http://dl2.iteye.com/upload/attachment/0121/7340/716ef073-855a-3f06-81c4-a2e2f5eaf653.png[/img]
成功后父模块的pom.xml文件代码如下,添加相应的jar包(Spring 4.0.2.RELEASE mybatis 3.2.6 ):



4.0.0
com.cn.itava
Itava-main
0.0.1-SNAPSHOT
pom

Itava-dao
Itava-util
Itava-web
Itava-service
itava-controller




4.0.2.RELEASE

3.2.6

1.7.7
1.2.17





junit
junit
4.11

test



org.springframework
spring-core
${spring.version}



org.springframework
spring-web
${spring.version}


org.springframework
spring-oxm
${spring.version}


org.springframework
spring-tx
${spring.version}



org.springframework
spring-jdbc
${spring.version}



org.springframework
spring-webmvc
${spring.version}


org.springframework
spring-aop
${spring.version}



org.springframework
spring-context-support
${spring.version}



org.springframework
spring-test
${spring.version}


org.springframework
spring-orm
${spring.version}



org.mybatis
mybatis
${mybatis.version}



org.mybatis
mybatis-spring
1.2.2



javax
javaee-api
7.0



mysql
mysql-connector-java
5.1.30



commons-dbcp
commons-dbcp
1.2.2



jstl
jstl
1.2




log4j
log4j
${log4j.version}



com.alibaba
fastjson
1.1.41


org.slf4j
slf4j-api
${slf4j.version}



org.slf4j
slf4j-log4j12
${slf4j.version}




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.9



com.cn.itava
Itava-controller
0.0.1-SNAPSHOT


com.cn.itava
Itava-dao
0.0.1-SNAPSHOT


com.cn.itava
Itava-service
0.0.1-SNAPSHOT


com.cn.itava
Itava-util
0.0.1-SNAPSHOT





3、至此相应的包结构已经建好。
在Itava-web包中添加配置文件,spring-mvc.xml,spring-mybatis.xml,jdbc.properties,log4j.properties。包结构如下:
[img]http://dl2.iteye.com/upload/attachment/0121/7342/f255d477-30e6-34e5-b6ac-0bca4fabe53b.png[/img]
spring-mvc.xml文件内容如下:


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">



class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">


text/html;charset=UTF-8




class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">














class="org.springframework.web.multipart.commons.CommonsMultipartResolver">











spring-mybatis.xml文件的内容如下:


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">



class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">



destroy-method="close">






























class="org.springframework.jdbc.datasource.DataSourceTransactionManager">






jdbc.properties内容如下:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=sa
#\u5B9A\u4E49\u521D\u59CB\u8FDE\u63A5\u6570
initialSize=0
#\u5B9A\u4E49\u6700\u5927\u8FDE\u63A5\u6570
maxActive=20
#\u5B9A\u4E49\u6700\u5927\u7A7A\u95F2
maxIdle=20
#\u5B9A\u4E49\u6700\u5C0F\u7A7A\u95F2
minIdle=1
#\u5B9A\u4E49\u6700\u957F\u7B49\u5F85\u65F6\u95F4
maxWait=60000


log4j.properties代码如下:

#\u5B9A\u4E49LOG\u8F93\u51FA\u7EA7\u522B
log4j.rootLogger=INFO,Console,File
#\u5B9A\u4E49\u65E5\u5FD7\u8F93\u51FA\u76EE\u7684\u5730\u4E3A\u63A7\u5236\u53F0
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#\u53EF\u4EE5\u7075\u6D3B\u5730\u6307\u5B9A\u65E5\u5FD7\u8F93\u51FA\u683C\u5F0F\uFF0C\u4E0B\u9762\u4E00\u884C\u662F\u6307\u5B9A\u5177\u4F53\u7684\u683C\u5F0F
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n

#\u6587\u4EF6\u5927\u5C0F\u5230\u8FBE\u6307\u5B9A\u5C3A\u5BF8\u7684\u65F6\u5019\u4EA7\u751F\u4E00\u4E2A\u65B0\u7684\u6587\u4EF6
log4j.appender.File = org.apache.log4j.RollingFileAppender
#\u6307\u5B9A\u8F93\u51FA\u76EE\u5F55
log4j.appender.File.File = logs/ssm.log
#\u5B9A\u4E49\u6587\u4EF6\u6700\u5927\u5927\u5C0F
log4j.appender.File.MaxFileSize = 10MB
# \u8F93\u51FA\u6240\u4EE5\u65E5\u5FD7\uFF0C\u5982\u679C\u6362\u6210DEBUG\u8868\u793A\u8F93\u51FADEBUG\u4EE5\u4E0A\u7EA7\u522B\u65E5\u5FD7
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


web.xml文件配置如下:


xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
Archetype Created Web Application


contextConfigLocation
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
true


SpringMVC

/


/index.jsp





5、Itava-dao实现如下:
[img]http://dl2.iteye.com/upload/attachment/0121/7351/7998348c-eed8-35ac-966e-d59327f1038c.png[/img]
这里面的user_sqlMap.xml,User.java,UserDao.java代码是通过mybatis-generator自动生成的。
可参照博客:http://blog.csdn.net/zhshulin/article/details/23912615

6、Itava-service实现如下:

[img]http://dl2.iteye.com/upload/attachment/0121/7358/707c9382-16a8-390b-bb2e-70c6b56f273b.png[/img]
UserService代码:

package org.itava.service;

import org.itava.pojo.User;


public interface UserService {
public User getUserById(int userId);
}




UserServiceImpl代码:

package org.itava.service.impl;

import javax.annotation.Resource;

import org.itava.dao.UserDao;
import org.itava.pojo.User;
import org.itava.service.UserService;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserServiceImpl implements UserService {
@Resource
private UserDao userDao;

public User getUserById(int userId) {
return this.userDao.selectByPrimaryKey(userId);
}


}



7、Itava-controller代码如下:
[img]http://dl2.iteye.com/upload/attachment/0121/7363/f304fef6-1f50-307f-ac49-6dd60b247a4d.png[/img]
UserController实现代码如下:

package org.itava.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;



import org.itava.pojo.User;
import org.itava.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


@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";
}
}


8、在Itava-web中新建jsp文件,在pom.xml文件中配置tomcat服务器

[img]http://dl2.iteye.com/upload/attachment/0121/7367/9d1b8209-4505-3876-bea9-1bafbf7e4835.png[/img]

pom.xml代码如下:


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4.0.0

com.cn.itava
Itava-main
0.0.1-SNAPSHOT

com.cn.itava
Itava-web
0.0.1-SNAPSHOT
war
Itava-web Maven Webapp
http://maven.apache.org


junit
junit
3.8.1
test


javax.servlet
servlet-api
2.5
provided


jstl
jstl
1.2


com.cn.itava
Itava-controller
0.0.1-SNAPSHOT




UTF-8
UTF-8

UTF-8


Itava-web


org.codehaus.mojo
tomcat-maven-plugin
1.1

http://localhost:8080/manager/html
Itava-main
admin
admin







jdk-1.6

true
1.6


1.6
1.6
1.6







showUser.jsp文件内容:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>



测试



${user.userName}




9、启动tomcat,访问界面http://localhost:8080/Itava-web/user/showUser?id=1


[img]http://dl2.iteye.com/upload/attachment/0121/7372/4ddaea19-5484-3f58-b373-3ad339755958.png[/img]

你可能感兴趣的:(Maven)