maven搭建ssm项目

参考1:使用idea搭建maven+ssm项目

start.

第一部分:项目创建

1、在idea中,点击new project。选择maven、jdk,并从模板创建project,注意模板选择5中框中的模板(否则后续可能出现问题),点击下一步,创建project。

maven搭建ssm项目_第1张图片

2、填写项目的groupid和artifactid,并点击下一步。

maven搭建ssm项目_第2张图片

3、选择自己的maven目录,点击下一步

maven搭建ssm项目_第3张图片

4、点击finsh,完成项目创建。

maven搭建ssm项目_第4张图片

 

第二部分:项目结构

maven搭建ssm项目_第5张图片

 

第三部分:项目配置文件编写

5、配置pom.xml,导入需要引用的ssm包。




    4.0.0

    com.isoft
    myssm
    1.0-SNAPSHOT
    war

    myssm Maven Webapp
    
    http://www.example.com

    
        UTF-8
        1.7
        1.7

        4.2.6.RELEASE
        
        3.4.0
        
        1.7.7
        1.2.17
        
        2.7.1
    

    
        
            junit
            junit
            4.11
            test
        

        
        
            jstl
            jstl
            1.2
        

        
        
            com.alibaba
            fastjson
            1.2.7
        

        
        
            commons-dbcp
            commons-dbcp
            1.4
        
        
            commons-pool
            commons-pool
            1.6
        

        
        
            org.springframework
            spring-test
            ${spring.version}
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
            org.springframework
            spring-core
            ${spring.version}
        
        
            org.springframework
            spring-aop
            ${spring.version}
        
        
            org.springframework
            spring-jms
            ${spring.version}
        
        
            org.springframework
            spring-jdbc
            ${spring.version}
        
        
            org.springframework
            spring-tx
            ${spring.version}
        
        
            org.springframework
            spring-context-support
            ${spring.version}
            jar
            compile
        
        
            org.aspectj
            aspectjweaver
            1.7.4
        
        
            aspectj
            aspectjrt
            1.5.4
        
        
            org.codehaus.jackson
            jackson-mapper-asl
            1.9.11
        
        
        
            org.mybatis
            mybatis
            3.4.1
        
        
            org.mybatis
            mybatis-spring
            1.3.0
        
        
            mysql
            mysql-connector-java
            5.1.21
        
        
        
            c3p0
            c3p0
            0.9.1.2
            jar
        

        
            org.slf4j
            slf4j-api
            1.7.5
        
        
            org.slf4j
            slf4j-log4j12
            1.7.5
        
        
            log4j
            log4j
            1.2.17
        
        
        
            com.fasterxml.jackson.core
            jackson-core
            2.7.3
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.7.3
        
        
            com.fasterxml.jackson.core
            jackson-annotations
            2.7.3
        
    

    
        myssm
        
            
                
                
                    org.apache.tomcat.maven
                    tomcat7-maven-plugin
                    2.2
                    
                        8081
                        /
                    
                
                
                    maven-clean-plugin
                    3.1.0
                
                
                
                    maven-resources-plugin
                    3.0.2
                
                
                    maven-compiler-plugin
                    3.8.0
                
                
                    maven-surefire-plugin
                    2.22.1
                
                
                    maven-war-plugin
                    3.2.2
                
                
                    maven-install-plugin
                    2.5.2
                
                
                    maven-deploy-plugin
                    2.8.2
                
            
        
    

6、配置web工程必须的web.xml文件





    Archetype Created Web Application

    
        contextConfigLocation
        classpath:applicationContext.xml
    
    
        log4jConfigLocation
        classpath:log4j.properties
    

    
    
        encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            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
    
    
        SpringMVC
        
        /
    

    
        /index.jsp
    

    
    
        30
    

7、配置spring(applicationContext.xml)、SpringMVC(spring-mvc.xml)、mybatis(spring-mybatis.xml)所需配置的xml文件,及其他配置文件(如:数据库信息配置config.properties、日志配置log4j.properties)。

applicationContext.xml




    
        
            
                classpath:config.properties
            
        
        
    
    
    
        
    

    

spring-mvc.xml




    
        
            
            
            
            
        
    

    
    
    

    
    
    
    
        
            
                
                
                
            
        
    

    
    
        
            
                text/html;charset=UTF-8
                application/json
            
        
        
            
                WriteMapNullValue
                QuoteFieldNames
            
        
    

    
    
        
        
    

spring-mybatis.xml




    
    
        
        
        
        
        
    

    
    
        
        
        
    

    
    
        
        
    

    
    
        
    
    
    


config.properties

#JDBC
#jdbc.driver=oracle.jdbc.OracleDriver
jdbc.driver=com.mysql.jdbc.Driver
#jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:test
jdbc.url=jdbc:mysql://localhost:3306/myblog?useSSL=false&useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root
#\u5B9A\u4E49\u521D\u59CB\u8FDE\u63A5\u6570
#jdbc.initialSize=0
#\u5B9A\u4E49\u6700\u5927\u8FDE\u63A5\u6570
#jdbc.maxActive=20
#\u5B9A\u4E49\u6700\u5927\u7A7A\u95F2
#jdbc.maxIdle=20
#\u5B9A\u4E49\u6700\u5C0F\u7A7A\u95F2
#jdbc.minIdle=1
#\u5B9A\u4E49\u6700\u957F\u7B49\u5F85\u65F6\u95F4
#jdbc.maxWait=60000

log4j.properties

### set log levels ###
log4j.rootLogger = info ,  stdout ,  D ,  E

### \u8F93\u51FA\u5230\u63A7\u5236\u53F0 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c %x - %m%n
#log4j.appender.stdout.layout.ConversionPattern =  %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n

### \u8F93\u51FA\u5230\u65E5\u5FD7\u6587\u4EF6 ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = C://logs/ssm.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG 
## \u8F93\u51FADEBUG\u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

### \u4FDD\u5B58\u5F02\u5E38\u4FE1\u606F\u5230\u5355\u72EC\u6587\u4EF6 ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File = C://logs/error.log
## \u5F02\u5E38\u65E5\u5FD7\u6587\u4EF6\u540D
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR 
## \u53EA\u8F93\u51FAERROR\u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7!!!
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

第四部分:初始化,编写简单测试代码,确认搭建成功。

8、编写jsp文件

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    
    
    
    
    
    111




Hello World!

点击返回数据

点击返回页面

home.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    
    
    
    
    
    222



第二个页面

9、编写控制层controller。

isoft/controller/TestController.java

package isoft.controller;

import com.alibaba.fastjson.JSONObject;
import isoft.service.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/index")
public class TestController {

    private static final Logger LOGGER = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private TestService testService;

    @RequestMapping(value = "/testAction", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject indexTest(String data) {

        int i = testService.testAction();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("test", i);

        return jsonObject;
    }
    
    @RequestMapping(value = "/testActionDemo2", method = RequestMethod.GET)
    public String indexTest2() {
        return "home";
    }

}

10、编写service层接口及实现类。

isoft/service/TestService.java

package isoft.service;

public interface TestService {

    int testAction();

}

isoft/service/impl/TestServiceImpl.java

package isoft.service.impl;

import isoft.dao.TestDao;
import isoft.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TestServiceImpl implements TestService {

    @Autowired
    TestDao testDao;

    @Override
    public int testAction() {
        return testDao.selectCount();
    }
}

11、编写dao层需要的接口及mapper文件。

isoft/dao/TestDao.java

package isoft.dao;

public interface TestDao {

    int selectCount();

}

mapper/test.xml





    

第五部分:配置Tomcat,启动、访问项目(也尝试了直接使用maven的tomcat插件方式配置可以启动的tomcat,但是配置成功之后,项目启动报错,暂时没有解决,所以先按照以下方式配置)

12、点击run-Edit Configurations

maven搭建ssm项目_第6张图片

13、添加一个新的local tomcat,并配置(或选择)tomcat。

maven搭建ssm项目_第7张图片

14、点击添加,将新建的项目配置关联到tomcat,然后配置访问路径为“localhost:xxxx/myssm”,点击确定保存tomcat。

maven搭建ssm项目_第8张图片

15、启动项目,请求“http://localhost:8088/myssm/”,项目搭建完成,示例代码正常可用。结果如下所示:

maven搭建ssm项目_第9张图片

 

第六部分:ps

之前提到的maven配置tomcat插件的方式,配置并启动tomcat,配置方式如下:

1、在pom.xml文件中配置tomcat插件引入,如下图:

maven搭建ssm项目_第10张图片

2、点击run-Edit Configurations

3、配置一个maven方式的项目启动,command line填写为“clean tomcat7:run”,点击ok。

maven搭建ssm项目_第11张图片

4、启动项目,完成配置。访问,查看效果。(但是目前启动的时候会报错)

ps:报错如下:

"C:\D\program files\Java\jdk1.8.0_171\bin\java.exe" -Dmaven.multiModuleProjectDirectory=C:\D\iSoft\myssm "-Dmaven.home=C:\D\program files\idea\IntelliJ IDEA 2018.1.4\plugins\maven\lib\maven3" "-Dclassworlds.conf=C:\D\program files\idea\IntelliJ IDEA 2018.1.4\plugins\maven\lib\maven3\bin\m2.conf" "-javaagent:C:\D\program files\idea\IntelliJ IDEA 2018.1.4\lib\idea_rt.jar=4034:C:\D\program files\idea\IntelliJ IDEA 2018.1.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\D\program files\idea\IntelliJ IDEA 2018.1.4\plugins\maven\lib\maven3\boot\plexus-classworlds-2.5.2.jar" org.codehaus.classworlds.Launcher -Didea.version=2018.1.4 -s "C:\D\program files\maven\apache-maven-3.5.3\conf\settings.xml" clean tomcat7:run
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building myssm Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[WARNING] The artifact aspectj:aspectjrt:jar:1.5.4 has been relocated to org.aspectj:aspectjrt:jar:1.5.4
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ myssm ---
[INFO] Deleting C:\D\iSoft\myssm\target
[INFO] 
[INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) > process-classes @ myssm >>>
[WARNING] The artifact aspectj:aspectjrt:jar:1.5.4 has been relocated to org.aspectj:aspectjrt:jar:1.5.4
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ myssm ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\D\iSoft\myssm\src\main\java
[INFO] skip non existing resourceDirectory C:\D\iSoft\myssm\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ myssm ---
[INFO] No sources to compile
[INFO] 
[INFO] <<< tomcat7-maven-plugin:2.2:run (default-cli) < process-classes @ myssm <<<
[INFO] 
[INFO] --- tomcat7-maven-plugin:2.2:run (default-cli) @ myssm ---
[INFO] Running war on http://localhost:8081/
[INFO] Creating Tomcat server configuration at C:\D\iSoft\myssm\target\tomcat
[INFO] create webapp with contextPath: 
五月 25, 2019 12:30:38 上午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-bio-8081"]
五月 25, 2019 12:30:38 上午 org.apache.catalina.core.StandardService startInternal
信息: Starting service Tomcat
五月 25, 2019 12:30:38 上午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/7.0.47
五月 25, 2019 12:30:40 上午 org.apache.catalina.core.ApplicationContext log
信息: No Spring WebApplicationInitializer types detected on classpath
五月 25, 2019 12:30:40 上午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
五月 25, 2019 12:30:40 上午 org.apache.catalina.core.StandardContext listenerStart
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
	at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
	at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
	at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
	at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:609)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:510)
	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
	at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)
	... 21 more

五月 25, 2019 12:30:40 上午 org.apache.catalina.core.StandardContext startInternal
严重: Error listenerStart
五月 25, 2019 12:30:40 上午 org.apache.catalina.core.StandardContext startInternal
严重: Context [] startup failed due to previous errors
五月 25, 2019 12:30:40 上午 org.apache.catalina.core.ApplicationContext log
信息: Closing Spring root WebApplicationContext
五月 25, 2019 12:30:40 上午 org.apache.catalina.core.StandardContext listenerStop
严重: Exception sending context destroyed event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
	at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:170)
	at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1006)
	at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:982)
	at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:934)
	at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:581)
	at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:116)
	at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4980)
	at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5626)
	at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:160)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

五月 25, 2019 12:30:40 上午 org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
严重: The web application [] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
五月 25, 2019 12:30:40 上午 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
严重: The web application [] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak.
五月 25, 2019 12:30:40 上午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-bio-8081"]

end.

 

你可能感兴趣的:(maven搭建ssm项目)