SSM--Spring、SpringMVC、Mybatis新三大框架MyEclipse项目搭建

转自:https://www.cnblogs.com/LasyisOriSin/p/6632257.html

SSM三大框架随着互联网的发展,在非企业级应用开发领域其使用占比已经逐渐超过了原来的三驾马车SSH,其清晰的模块化,轻量级在追求效率的互联网项目开发中更占优势。

  SSM框架项目一般基于Maven,通过Maven的jar包管理及其他工具,web项目的搭建和维护变得更加简便。

  相比于Eclipse需要配置集成Maven项目,MyEclipse安装好后则自动集成进来了Maven,故本文将基于MyEclipse,简单地介绍如何搭建一个基本的SSM框架项目,具体步骤见下文:

  1、新建一个Maven项目

      

      

      点击next后,在catalog中选择All Catalog,Filter中输入webapp进行搜索过滤,随后再结果中选择maven-archetype-webapp,再点击next:

      

      (Ps:

      此处若不需要通过Maven archetype来创建项目的话(Maven archetype是一个插件,用于构建自定义的组织结构的web项目),可直接跳过,进行一个简单项目的创建;本例需要创建一个web项目,需选择maven-archetype-webapp。

      

      )

      点击next后如下,其中Artifact Id为项目名;

      

      随后进行项目的配置:

      右键项目-Properties,出现以下界面,对编码进行设置:

      

      随后对依赖环境进行配置,此处默认总是选择1.5的JDK,可能是我的Myeclipse的Maven默认设置有点小问题。remove掉换成1.7的jdk(edit修改貌似总是有点小问题,还是remove掉重新来的稳妥。。):

      

      随后配置新的jdk:

      

      

      

       配置完JDK后,再配置编译器的版本:

      (若改完BuildPath后直接点到Java Compiler编译器设置窗口的话,会弹出以下弹窗,点击Apply应用即可:

      

      )

      

      随后再将Project Facets将项目特性配置好,其中Dynamic Web Module/Java/JavaScript/Maven Support for Java EE Projects都已经勾选,

      需要将Dynamic Web Module版本选择为3.0、Java选择1.7,并勾选JSTL Libraries选择最新版本1.2.2。

      (Ps:此处是选择各个特性的较新版本,若有其他版本需要,则需自行尝试,检查冲突性,同时Maven也会给出相关版本不兼容的提示)

      

       (Ps:若是选择构建简单项目,则也直接按上面的勾选即可,但配置完后项目可能会报错,见下:

      配置完后项目如下所示,发现项目存在报错:cannot detect Web Project version

      

      

      错误报在pom.xml处,

      (Project Object Model,项目对象模型。通过xml格式保存的pom.xml文件。

       作用类似ant的build.xml文件,功能更强大。

      该文件用于管理:源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的url、项目的依赖关系等等。)

      双击进去后,对其版本进行设置:

      

      设置后报错消除,到此为止SSM项目的创建基本完成。

         比较奇怪的是,设置了版本后pom.xml的内容仅仅增加了一个插件maven-war-plugin,该插件用于将Maven项目打包成war包:

      

      但设置完版本后如果将此插件删除,报错则不再出现了???比较疑惑这是怎么个情况。。。

      )

       

  2、SSM三大框架jar包导入:

  在pom.xml中导入jar包,直接在<project>标签下插入以下内容:

复制代码

  1     <!-- 导入的Jar包详细配置及注释 -->
  2     <!-- 统一导入的SSM框架jar包的版本,避免版本冲突,方便后续jar包导入的版本管理及统一维护修改 -->
  3     <properties>
  4         <!-- spring版本号 -->
  5         <spring.version>4.0.2.RELEASE</spring.version>
  6         <!-- mybatis版本号 -->
  7         <mybatis.version>3.2.6</mybatis.version>
  8         <!-- log4j日志文件管理包版本 -->
  9         <slf4j.version>1.7.7</slf4j.version>
 10         <log4j.version>1.2.17</log4j.version>
 11     </properties>
 12     <dependencies>
 13         <dependency>
 14             <groupId>junit</groupId>
 15             <artifactId>junit</artifactId>
 16             <version>4.11</version>
 17             <!-- 表示开发的时候引入,发布的时候不会加载此包 -->
 18             <scope>test</scope>
 19         </dependency>
 20         <!-- spring核心包 -->
 21         <dependency>
 22             <groupId>org.springframework</groupId>
 23             <artifactId>spring-core</artifactId>
 24             <version>${spring.version}</version>
 25         </dependency>
 26 
 27         <dependency>
 28             <groupId>org.springframework</groupId>
 29             <artifactId>spring-web</artifactId>
 30             <version>${spring.version}</version>
 31         </dependency>
 32         <dependency>
 33             <groupId>org.springframework</groupId>
 34             <artifactId>spring-oxm</artifactId>
 35             <version>${spring.version}</version>
 36         </dependency>
 37         <dependency>
 38             <groupId>org.springframework</groupId>
 39             <artifactId>spring-tx</artifactId>
 40             <version>${spring.version}</version>
 41         </dependency>
 42 
 43         <dependency>
 44             <groupId>org.springframework</groupId>
 45             <artifactId>spring-jdbc</artifactId>
 46             <version>${spring.version}</version>
 47         </dependency>
 48 
 49         <dependency>
 50             <groupId>org.springframework</groupId>
 51             <artifactId>spring-webmvc</artifactId>
 52             <version>${spring.version}</version>
 53         </dependency>
 54         <dependency>
 55             <groupId>org.springframework</groupId>
 56             <artifactId>spring-aop</artifactId>
 57             <version>${spring.version}</version>
 58         </dependency>
 59 
 60         <dependency>
 61             <groupId>org.springframework</groupId>
 62             <artifactId>spring-context-support</artifactId>
 63             <version>${spring.version}</version>
 64         </dependency>
 65 
 66         <dependency>
 67             <groupId>org.springframework</groupId>
 68             <artifactId>spring-test</artifactId>
 69             <version>${spring.version}</version>
 70         </dependency>
 71         <!-- mybatis核心包 -->
 72         <dependency>
 73             <groupId>org.mybatis</groupId>
 74             <artifactId>mybatis</artifactId>
 75             <version>${mybatis.version}</version>
 76         </dependency>
 77         <!-- mybatis/spring包 -->
 78         <dependency>
 79             <groupId>org.mybatis</groupId>
 80             <artifactId>mybatis-spring</artifactId>
 81             <version>1.2.2</version>
 82         </dependency>
 83         <!-- 导入java ee jar 包 -->
 84         <dependency>
 85             <groupId>javax</groupId>
 86             <artifactId>javaee-api</artifactId>
 87             <version>7.0</version>
 88         </dependency>
 89         <!-- 导入Mysql数据库链接jar包 -->
 90         <dependency>
 91             <groupId>mysql</groupId>
 92             <artifactId>mysql-connector-java</artifactId>
 93             <version>5.1.30</version>
 94         </dependency>
 95         <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
 96         <dependency>
 97             <groupId>commons-dbcp</groupId>
 98             <artifactId>commons-dbcp</artifactId>
 99             <version>1.2.2</version>
100         </dependency>
101         <!-- JSTL标签类 -->
102         <dependency>
103             <groupId>jstl</groupId>
104             <artifactId>jstl</artifactId>
105             <version>1.2</version>
106         </dependency>
107         <!-- 日志文件管理包 -->
108         <!-- log start -->
109         <dependency>
110             <groupId>log4j</groupId>
111             <artifactId>log4j</artifactId>
112             <version>${log4j.version}</version>
113         </dependency>
114 
115 
116         <!-- 格式化对象,方便输出日志 -->
117         <dependency>
118             <groupId>com.alibaba</groupId>
119             <artifactId>fastjson</artifactId>
120             <version>1.1.41</version>
121         </dependency>
122 
123 
124         <dependency>
125             <groupId>org.slf4j</groupId>
126             <artifactId>slf4j-api</artifactId>
127             <version>${slf4j.version}</version>
128         </dependency>
129 
130         <dependency>
131             <groupId>org.slf4j</groupId>
132             <artifactId>slf4j-log4j12</artifactId>
133             <version>${slf4j.version}</version>
134         </dependency>
135         <!-- log end -->
136         <!-- 映入JSON -->
137         <dependency>
138             <groupId>org.codehaus.jackson</groupId>
139             <artifactId>jackson-mapper-asl</artifactId>
140             <version>1.9.13</version>
141         </dependency>
142         <!-- 上传组件包 -->
143         <dependency>
144             <groupId>commons-fileupload</groupId>
145             <artifactId>commons-fileupload</artifactId>
146             <version>1.3.1</version>
147         </dependency>
148         <dependency>
149             <groupId>commons-io</groupId>
150             <artifactId>commons-io</artifactId>
151             <version>2.4</version>
152         </dependency>
153         <dependency>
154             <groupId>commons-codec</groupId>
155             <artifactId>commons-codec</artifactId>
156             <version>1.9</version>
157         </dependency>
158     </dependencies>

复制代码

     如果你精确地知道你要导入的jar包的包名及版本,那么就可以才用上面的方式导入依赖包。但是如果记不太清也不要紧,Maven提供了依赖jar包搜索的功能,点击pom.xml的Dependencies可进入jar包管理界面如下:

  

  其中关于pom.xml的介绍:

    Overview:显示maven项目的一些基本信息. 

    Dependencies:添加jar包的页面,很重要! 

    Plugins:添加maven插件的页面.比如tomcat-maven-plugin等. 

    Reporting:从没用过,无视~ 

    Dependency Hierarchy:用于显示jar包的依赖关系.没事的时候可以看看jar包的依赖关系. Effective POM:显示maven的编译路径,plugin之类的.也可以无视. 

    pom.xml:导入jar包的信息,可以在其中进行修改.重要 

      Dependency Graph:依赖图

  

  接下来对Spring及Mybatis进行整合,在src/main/resource/下新建spring-mybatis.xml(数据库侧配置),并将以下配置内容写入:

  (其中<context:property-placeholder location="classpath:jdbc.properties"/><!-- 加载配置文件 -->是一种加载存放着数据库相关配置信息的properties文件方式,

  除了此种,还可以在<bean>标签中定义,定义后可以在xml中引用或者在java代码中使用注解方式实现注入(使用@Value标签)。具体可见一篇介绍博客:http://blog.csdn.net/eson_15/article/details/51365707

  )

复制代码

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:tx="http://www.springframework.org/schema/tx"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 7     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd 
 8     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
 9     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
10     <context:property-placeholder location="classpath:jdbc.properties"/><!-- 加载配置文件 -->
11     
12     <bean id="dataSource" class="${dataSource}"
13         destroy-method="close">
14            <property name="driverClassName" value="${driverClassName}" />  
15         <property name="url" value="${url}" />  
16         <property name="username" value="${user}" />  
17         <property name="password" value="${password}" />  
18         <!-- 初始化连接大小 -->  
19         <property name="initialSize" value="${initialSize}"></property>  
20         <!-- 连接池最大数量 -->  
21         <property name="maxActive" value="${maxActive}"></property>  
22         <!-- 连接池最大空闲 -->  
23         <property name="maxIdle" value="${maxIdle}"></property>  
24         <!-- 连接池最小空闲 -->  
25         <property name="minIdle" value="${minIdle}"></property>  
26         <!-- 获取连接最大等待时间 -->  
27         <property name="maxWait" value="${maxWait}"></property>
28     </bean>
29     <context:annotation-config /> <!-- 此行语句使得resource autowired 等四个注解可以使用 -->
30     
31    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->  
32     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
33         <property name="dataSource" ref="dataSource" />  
34         <!-- 自动扫描mapping.xml文件 -->  
35         <property name="mapperLocations" value="classpath:mapper/*.xml"></property>  
36     </bean>  
37     <!-- DAO接口所在包名,Spring会自动查找其下的类 -->  
38     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
39         <property name="basePackage" value="cn.job.dao" />  
40         <!-- 
41             <property name="sqlSessionFactory" value="sqlSessionFactoryBean"/>
42             这句话可写可不写,因为spring会去自动查找并注入
43          -->  
44     </bean>  
45     
46     <!-- 配置事务管理器 -->
47     <bean id="txManager"
48         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
49         <property name="dataSource" ref="dataSource" />
50     </bean>
51     
52     <!-- 事务扫描开始(开启@Tranctional) -->
53     <tx:annotation-driven transaction-manager="txManager" />
54 
55 </beans>

复制代码

 

  随后在web.xml中配置容器启动内容:

  (spring-*.xml代表读取所有以spring-开头的xml文件,

  classpath就是代表  /WEB-INF /classes/  这个路径,

  

  classpath 和 classpath* 区别:

  classpath:只会到你的class路径中查找找文件;
  classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找 ---- 够深入的吧

复制代码

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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_2_5.xsd" version="2.5">
 3   <welcome-file-list>
 4     <welcome-file>index.jsp</welcome-file>
 5   </welcome-file-list>
 6   <!-- 启动spring容器 -->
 7   <servlet>
 8     <servlet-name>springmvc</servlet-name>
 9     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
10     <init-param>
11       <param-name>contextConfigLocation</param-name>
12       <param-value>classpath:spring-*.xml</param-value>
13     </init-param>
14     <!-- 启动顺序优先 -->
15     <load-on-startup>1</load-on-startup>
16   </servlet>
17   <!-- 对请求后缀进行筛选 -->
18   <servlet-mapping>
19     <servlet-name>springmvc</servlet-name>
20     <url-pattern>*.e</url-pattern>
21   </servlet-mapping>
22 </web-app>

复制代码

  配置视图层(展示层)的spring-web.xml文件:  

(<mvc:annotation-driven/>相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean,配置一些messageconverter。即解决了@Controller注解的使用前提配置。

<context:annotation-config/>是对包进行扫描,实现注释驱动Bean定义,同时将bean自动注入容器中使用。即解决了@Controller标识的类的bean的注入和使用。)

复制代码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    <!-- 注解扫描和驱动配置 -->
    <context:component-scan base-package="cn.job.action"/>     
    <mvc:annotation-driven/>
</beans>

复制代码

  最后配置业务层spring-service.xml,与视图层类似,仅仅是扫了@Controller注解驱动部分:

  

复制代码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    <context:component-scan base-package="cn.job.services"/>     
     
</beans>

复制代码

  配置完后,接下来就可以写代码了,从dao层开始,写数据库侧接口:

复制代码

 1 package cn.job.dao;
 2 
 3 import org.springframework.stereotype.Repository;
 4 
 5 @Repository("testDao")
 6 public interface testDao {
 7 
 8     String getSysdate();
 9     
10     void testPG();
11 }

复制代码

  对应地写sql映射文件:

复制代码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"      
 "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="cn.job.dao.testDao">
    <select id="getSysdate" resultType="String">
        select now()
    </select>
    <update id="testPG">
         drop table if exists mid_ydyh,mid_ydsyxz,mid_ydbyxz,mid_ydsyxzC,mid_ydsyxzR;
         
         select  a.* into mid_ydsyxz from WID_CHN_PRD_SERV_MKT_MON a
         where a.acct_month=201702
         and a.NEW_FLAG='T'
         and a.product_id=208511296;
    </update>
</mapper>

复制代码

  每一个接口中的方法都必须在mapper.xml中的<mapper>标签中对应着有一个id唯一的bean,在bean标签内写着对应的sql语句,同时每一个接口最好1对1地有一个mapper.xml映射文件:

  

  最后,根据想写的demo把剩余的action、services、index.jsp写好,结构即如下:

   

  成功!

    

     

    对SSM框架的掌握仅仅停留在应用层面,十分惭愧,后续将以此博客为起点,逐渐学习SSM框架中经典的实现原理及方式!

    每天进步一点点!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值