Maven下SSM框架搭建-上

汉语版

虽然是讲述如何搭建一个框架,但我不打算将每个细节都用截图或者粘贴的形式来表现出来。在这里,会简要说明一下如何将整个环境搭建出来,并且将我在搭建环境中遇到的

问题是如何解决的说出来,时间不是很多。所能表达出的有限,还请见谅。由于本次文章较长,所以会分上中下来写。如果造成阅读不便,还请见谅。具体内容如下:

【I】事前准备

1.开发工具:Eclipse(Oxygen.1 Release (4.7.1))

2.项目管理工具:Maven

3.数据库:MySQL

4.浏览器:Chrome


【2】结构预览(navigator视图下)

sql_web

    --.settings

    --logs

        --ssm.log

    --src

       --main

            --java

                 --edu

                      --tostronger

                             --scu

                                    --common

                                    --controller

                                           --BaseController.java

                                    --entity

                                           --User.java

                                     --implement

                                           --UserImplemet.java

                                     --mapper

                                           --UserMapper.java

                                     --service

                                           --UserService.java

        --resourses

              --mybatis

                   --UserMapper.xml

              --jdbc.propertis

              --log4j.propertis

              --velocity.properties

              --mybatis.xml

              --spring-mvc.xml

              --spring-mybatis.xml

       --webapp

              --css

              --js

              --html

                  --index.html

              --fonts

              --img

              --WEB-INF

                  --velocity

                       --templates

                           --hello.vm

                 --web.xml

     --test

         --java

   --target[自动生成的文件夹,不必关心]

   --.classpath

   --.project

   --pom.xml

   --temp.txt[我自己加的,作为临时文件使用]

【3】搭建一个Maven工程

         [1]主要步骤:

                 1.新建一个maven工程,选择webapp;

                 2.填写GroupId(edu.tostronger.scu);Artifact Id(sql_web)

         [2]遇到问题

                1.index.jsp出现 javax.servlet.http.HttpServlet not found

                      ----解决:删除该文件

                2.Java Rescoures下没有src/main/java和src/text/java

                     ----解决:修改工程的Jre System  Library,换成最新版本

                    备注:后续在pom里面配置compile插件,也能解决此问题。

         [3]pom文件配置

                备注:最好把自己要配置的每一项都看看,所以就不直接粘贴配置代码,这里简要说明一下;

               1.properties

                      1.1主要配置一些版本或者属性的值,通过${}来获取到值

                      1.2实例:

<properties>
  <junit.version>4.12</junit.version>
  <project.source.encoding>UTF-8</project.source.encoding>
  <tomcat7.version>2.2</tomcat7.version>
  </properties>

              2.dependencies

                     备注:下的英文均为artifactId,可以在http://mvnrepository.com/里面查询然后找到它的Maven构建代码

                     1.1用于单元测试

                                junit

                     1.2用于日志输出

                                 log4j

                     1.3Spring相关包

                               1.3.1扩展IOC服务,对企业级服务进行支持,如远程访问,缓存等

                                            spring-context

                               1.3.2扩展支持context,在MVC方面

                                            spring-context-support

                               1.3.3核心工具包

                                           spring-core

                               1.3.4对象关系映射包,用于数据库持久化连接

                                           spring-orm

                               1.3.5对象XML映射包,用于实体与XML相互转换

                                           spring-oxm

                               1.3.6用于面向切面的编程

                                           spring-aop

                               1.3.7用于支持工程事务管理

                                           spring-tx   

                               1.3.8对jdbc进行简单的封装

                                           spring-jdbc

                               1.3.9web开发时所需要的包

                                          spring-web

                               1.3.10对junit框架的简单封装

                                          spring-junit

                               1.3.11springMVC开发时用到的类

                                          spring-webmvc

                               1.3.12表达式语言

                                          spring-expression

                               1.3.13整合aspectJ框架

                                          spring-aspects

                               备注:Spring 和SpringMvc的区别

                                        Spring : 是一个管理bean的容器,类似工具箱。

                                        Spring Mvc : 是web层的框架,主要负责请求-响应等管理。

                  1.4mybatis 相关包

                              1.4.1责数据库持久化连接

                                         mybatis

                              1.4.2将myBatis代码无缝地整合到Spring中

                                         spring-mybatis

                               1.4.3mybatis代码生成器

                                         mybatis-generator-core

                 1.5mysql 相关包

                                         mysql-connector-java

                 1.6c3p0 相关包

                              1.6.1数据池连接包

                                        c3p0

                              1.6.2数据池连接辅助包

                                        mchange-commons-java

                              备注:c3p0与dbcp的区别

c3p0hibernate开发组推荐使用

                              所需jar包:

                                              a.c3p0

                                              b.mchange-commons-java

                              spring bean注入:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> 

<!--一些细节有不同-->     
      </bean>

dbcp:Spring开发组推荐使用(强行关闭连接或数据库重启后,无法reconnect,告诉连接被重置,这个设置可以解决)

所需jar包:

a.commons-dbcp

b.commons-pool

c.commons-collections

1.7json相关包

1.7.1json处理核心包

jackson-core

1.7.2json数据绑定包

jackson-databind

1.7.3json使用注解包

                                    jackson-annotations

                           1.7.4json解析和json生成包

                                    fastjson

               1.8servlet相关包

                           servlet-api

                            备注:servlet-api与javax.servlet-api的区别

               1.9视图引擎

                         1.9.1用于视图渲染

                                   velocity

                         1.9.2处理页面数据

                                   velocity-tools

                         1.9.3页面画图工具包

                                   velocity.dev

               2.0Commons组件

                         2.0.1扩展java集合

                                   commons-collections

     3.build

              1.finalName

               2.plugins

                       2.1JDK配置

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <version>${compiler.version}</version>
    <configuration>
    <source>${compiler.src.version}</source>
    <target>${compiler.tar.version}</target>
    <encoding>${project.source.encoding}</encoding>
    </configuration>
    </plugin>

                      2.2TOMCAT配置

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>${tomcat7.version}</version>
    <configuration>
    <port>8090</port>
    <path>/</path>
    <server>tomcat7</server>
    <uriEncoding>UTF-8</uriEncoding>
    </configuration>
    </plugin>

                               备注:也可以配置Jetty服务器

                      2.3WAR配置

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.1.0</version>
  <configuration>
    <warSourceExcludes>*/lib/*.jar</warSourceExcludes>  
                <webResources>  
                    <resource>  
                        <directory>src/main/webapp/</directory>  
                        <filtering>true</filtering>  
                        <targetPath>webapp</targetPath>  
                    </resource>  
                </webResources> 
    </configuration>
    </plugin>

【4】搭建数据库

         [1]新建一个数据库连接sql_web,指定用户和密码

         [2]配置jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/sql_web
jdbc.username=stronger
jdbc.password=
stronger1234.

         [3]遇到问题

                1.pom文件的mysql-connector-java版本与数据库不匹配

                        ----解决:修改mysql-connector-java版本,改成低版本的

                2.useSSL = false 

                        ----解决:jdbc.url=jdbc:mysql://127.0.0.1:3306/sql_web?useSSL=true

3.其他还有可能遇到时区不匹配、编码等问题

----解决:jdbc.url=jdbc:mysql://127.0.0.1:3306/sql_web?useSSL=true&characterEncoding=UTF-8等

【5】配置日志

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

【6】配置Spring Mvc

【7】配置velocity属性

【8】配置Spring Mybatis   

【9】配置mybatis.xml

【10】构建Java下的包与mybatis文件夹下的文件

【11】配置web.xml

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

E-vesion:

so sorry that what you may see may words, words and still words, because i just don`t want give drafts or snippets or even the shutcuts will not paste on this paper. i can told

many reason like i`m lazy or there would much work to do and no gains form this paper. most convinceable reason is that i real a lot of articals and hurt a lot form those articals.

there are so much copy and paste or there are old version swagge in your eyes. so i want share some different and try my best to make every step should be success, and

importently, you can`t build your project by copying and pasting, you need write every word or statements carefully and give your eyes on the errors and exception that you make;

and i wish you can have a good undestand on every packages or plugins or others. for the reason that the paper would be stain and long , i cut it into three part. here goes the

first part:

【I】prepare

1.IDE:Eclipse(Oxygen.1 Release (4.7.1))

2.Project Manage Tools:Maven

3.Database:MySQL

4.Broswer:Chrome


【2】structure(under navigator view)

sql_web

--.settings

--logs

--ssm.log

--src

--main

--java

--edu

--tostronger

--scu

--common

--controller

--BaseController.java

--entity

--User.java

--implement

--UserImplemet.java

--mapper

--UserMapper.java

--service

--UserService.java

--resourses

--mybatis

--UserMapper.xml

--jdbc.propertis

--log4j.propertis

--velocity.properties

--mybatis.xml

--spring-mvc.xml

--spring-mybatis.xml

--webapp

--css

--js

--html

--index.html

--fonts

--img

--WEB-INF

--velocity

--templates

hello.vm

--web.xml

--test

--java

--target[auto generate file,you needn`t care about this]

--.classpath

--.project

--pom.xml

--temp.txt[as a temp file]

【3】build a Maven project

[1]steps:

1.build a new maven project,choose webapp;

2.fill GroupId(edu.tostronger.scu) and Artifact Id(sql_web)  blanks

[2]problems

1.index.jsp throw javax.servlet.http.HttpServlet not found

----working ways:delete this file

2.src/main/java and src/text/java not in Java Rescoures 

----working ways:update Jre System  Library

tips:another working way is deploy complie plugin in pom file

[3]pom configuration

tios:do not copy and paste, try to understand every attributes;

1.properties

1.1version or attribute value,get value by using ${}

1.2example:

<properties>
  <junit.version>4.12</junit.version>
  <project.source.encoding>UTF-8</project.source.encoding>
  <tomcat7.version>2.2</tomcat7.version>
  </properties>

2.dependencies

tip:artifactId proposed in breakline,serach from http://mvnrepository.com/ ,you would found maven construct codes

1.1unit test

junit

1.2output logs

log4j

1.3Spring related packages

1.3.1extend IOC service,support company level service,like telnet ,cache and so on.

spring-context

1.3.2ectend context in the aspect of MVC

spring-context-support

1.3.3core tools 

spring-core

1.3.4object  relation mapping

spring-orm

1.3.5object xml mapping

spring-oxm

1.3.6aspect oriental program

spring-aop

1.3.7trancscation manage

spring-tx

1.3.8encapsulate jdbc

spring-jdbc

1.3.9web relate packages

spring-web

1.3.10encapsulate junit

spring-junit

1.3.11springMVC related

spring-webmvc

1.3.12expression language

spring-expression

1.3.13apply aspectJ

spring-aspects

tips:Spring  SpringMvc diff

Spring : container ,like tools box。

Spring Mvc : web layer construtor,charge request and response and so on。

1.4mybatis related packages

1.4.1connect database

mybatis

1.4.2myBatis in Spring

spring-mybatis

1.4.3mybatis code generator

mybatis-generator-core

1.5mysql related packages

mysql-connector-java

1.6c3p0 related packages

1.6.1database pool packages

c3p0

1.6.2assist packages

mchange-commons-java

tip:c3p0 dbcp diff

c3p0hibernate advocate

jar:

a.c3p0

b.mchange-commons-java

spring bean inject:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> 

<!--some detail-->     
     </bean>

dbcp:Springadvocate

jar

a.commons-dbcp

b.commons-pool

c.commons-collections

1.7json related packages

1.7.1json core packages

jackson-core

1.7.2json data bind 

jackson-databind

1.7.3json annotations

jackson-annotations

1.7.4json parse and json generator

fastjson

1.8servlet related packages

servlet-api

tips:servlet-api and javax.servlet-api diff

1.9view engine

1.9.1view render

velocity

1.9.2in page data 

velocity-tools

1.9.3canvas draft

velocity.dev

2.0Commons units

2.0.1extend java collections

commons-collections

3.build

1.finalName

2.plugins

2.1JDK configuration

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <version>${compiler.version}</version>
    <configuration>
    <source>${compiler.src.version}</source>
    <target>${compiler.tar.version}</target>
    <encoding>${project.source.encoding}</encoding>
    </configuration>
    </plugin>

2.2TOMCAT configuration

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>${tomcat7.version}</version>
    <configuration>
    <port>8090</port>
    <path>/</path>
    <server>tomcat7</server>
    <uriEncoding>UTF-8</uriEncoding>
    </configuration>
    </plugin>

tips:also you can cofig a Jetty server

2.3WAR configuration

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
    <warSourceExcludes>*/lib/*.jar</warSourceExcludes>  
                <webResources>  
                    <resource>  
                        <directory>src/main/webapp/</directory>  
                        <filtering>true</filtering>  
                        <targetPath>webapp</targetPath>  
                    </resource>  
                </webResources> 
    </configuration>
    </plugin>

【4】build database

[1]build a new connection :sql_web,assign user name and passwd

[2]config jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/sql_web
jdbc.username=stronger
jdbc.password=
stronger1234.

[3]problems 

1.pom can not go with mysql-connector-java version

----working way:change mysql-connector-java to a low version

2.useSSL = false 

----working way:jdbc.url=jdbc:mysql://127.0.0.1:3306/sql_web?useSSL=true

3.other un-match problem

----working wayjdbc.url=jdbc:mysql://127.0.0.1:3306/sql_web?useSSL=true&characterEncoding=UTF-8 and so on

【5】config logs

· 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    
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

【6】config Spring Mvc

【7】config velocity.properties

【8】config Spring Mybatis   

【9】config mybatis.xml

【10】build Java packets and mybatis mappers

【11】config web.xml












  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值