码迷,mamicode.com
首页 > 编程语言 > 详细

手把手Maven搭建SpringMVC+Spring+MyBatis框架

时间:2017-05-19 14:28:04      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:springmvc   spring   mybatis   框架   maven   

SSM(Spring+SpringMVC+Mybatis),目前较为主流的企业级架构方案。标准的MVC设计模式,将整个系统划分为显示层、Controller层、Service层、Dao层四层,使用SpringMVC负责请求的转发和视图管理,Spring实现业务对象管理, MyBatis作为数据对象持久化引擎。

一. 框架详情

  1. Spring 是一个轻量级的Java开发框架,它是为了解决企业应用开发的复杂性而创建的。Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。 简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

  2. SpringMVC 属于SpringFrameWork的后续产品,分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。

  3. MyBatis 是一个基于Java的持久层框架。MyBatis提供的持久层框架包括SQL Maps和Data Access Objects(DAO)它消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java映射成数据库中的记录。

二. 创建Maven项目

  1. Eclipse中用Maven创建项目

    技术分享
     

  2. 按默认Next

    技术分享
     

  3. 找到maven-archetype-webapp后,点击next

    技术分享
     

  4. 填写相应的信息,GroupID是项目组织唯一的标识符,实际对应JAVA的包的结构。ArtifactID就是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称。Package填了默认给你建一个包,不写也可以。

    技术分享
     

  5. 刚建好的目录如下

    技术分享
     

  6. Maven规定必须添加以下Source Folder:
    src/main/resources
    src/main/java
    src/test/resources
    src/test/java
    在这步之前最好先项目上右键选择properties,然后点击java build path,在Librarys下,编辑JRE System Library,选择workspace default jre。

    技术分享
     

  7. 分别修改输出路径为,对应关系如下:

    技术分享
     

  8. 将项目转换成Dynamic Web Project,在项目上右键Properties,在左侧选择 Project Facets。

    技术分享
     

  9. 设置部署时的文件发布路径,删除test的两项,因为test是测试使用,并不需要部署。
    设置将Maven的jar包发布到lib下。Add -> Java Build Path Entries -> Maven Dependencies -> Finish

    技术分享
     

    三. Maven引入需要的JAR包

    Xml代码  技术分享

     

    四. 相关配置文件配置,整合SSM框架

    web.xml

    Java代码  技术分享

     

    spring.xml

    Java代码  技术分享

     


    为了代替手工使用 SqlSessionDaoSupport 或 SqlSessionTemplate 编写数据访问对象 (DAO)的代码,MyBatis-Spring 提供了一个动态代理的实现:MapperFactoryBean。这个类 可以让你直接注入数据映射器接口到你的 service 层 bean 中。当使用映射器时,你仅仅如调 用你的 DAO 一样调用它们就可以了,但是你不需要编写任何 DAO 实现的代码,因为 MyBatis-Spring 将会为你创建代理。

    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:mvc="http://www.springframework.org/schema/mvc"  

    4.     xmlns:context="http://www.springframework.org/schema/context"  

    5.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  

    6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   

    7.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   

    8.             http://www.springframework.org/schema/mvc   

    9.             http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd   

    10.             http://www.springframework.org/schema/context   

    11.             http://www.springframework.org/schema/context/spring-context-3.0.xsd   

    12.             http://www.springframework.org/schema/aop   

    13.             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   

    14.             http://www.springframework.org/schema/tx   

    15.             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">  

    16.   

    17.     <!-- 扫描service、dao组件 -->  

    18.     <context:component-scan base-package="com.yingjun.test" />  

    19.     <!-- 分解配置 jdbc.properites -->  

    20.     <context:property-placeholder location="classpath:jdbc.properties" />  

    21.       

    22.     <!-- 数据源c3p0 -->  

    23.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  

    24.         <property name="driverClass" value="${jdbc.driverClassName}" />  

    25.         <property name="jdbcUrl" value="${jdbc.url}" />  

    26.         <property name="user" value="${jdbc.username}" />  

    27.         <property name="password" value="${jdbc.password}" />  

    28.         <property name="maxPoolSize" value="${c3p0.pool.size.max}" />  

    29.         <property name="minPoolSize" value="${c3p0.pool.size.min}" />  

    30.         <property name="initialPoolSize" value="${c3p0.pool.size.ini}" />  

    31.         <property name="acquireIncrement" value="${c3p0.pool.size.increment}" />  

    32.     </bean>  

    33.   

    34.   

    35.     <!-- sessionFactory 将spring和mybatis整合 -->  

    36.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  

    37.         <property name="dataSource" ref="dataSource" />  

    38.         <property name="configLocation" value="classpath:spring-mybatis.xml" />  

    39.         <property name="mapperLocations" value="classpath*:com/yingjun/test/mapping/**/*.xml" />  

    40.     </bean>  

    41.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  

    42.         <property name="basePackage" value="com,yingjun.test.dao" />  

    43.         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />  

    44.     </bean>  

    45.   

    46.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  

    47.         <property name="dataSource" ref="dataSource" />  

    48.     </bean>  

    49.     <tx:advice id="transactionAdvice" transaction-manager="transactionManager">  

    50.         <tx:attributes>  

    51.             <tx:method name="add*" propagation="REQUIRED" />  

    52.             <tx:method name="append*" propagation="REQUIRED" />  

    53.             <tx:method name="insert*" propagation="REQUIRED" />  

    54.             <tx:method name="save*" propagation="REQUIRED" />  

    55.             <tx:method name="update*" propagation="REQUIRED" />  

    56.             <tx:method name="modify*" propagation="REQUIRED" />  

    57.             <tx:method name="edit*" propagation="REQUIRED" />  

    58.             <tx:method name="delete*" propagation="REQUIRED" />  

    59.             <tx:method name="remove*" propagation="REQUIRED" />  

    60.             <tx:method name="repair" propagation="REQUIRED" />  

    61.             <tx:method name="delAndRepair" propagation="REQUIRED" />  

    62.   

    63.             <tx:method name="get*" propagation="SUPPORTS" />  

    64.             <tx:method name="find*" propagation="SUPPORTS" />  

    65.             <tx:method name="load*" propagation="SUPPORTS" />  

    66.             <tx:method name="search*" propagation="SUPPORTS" />  

    67.             <tx:method name="datagrid*" propagation="SUPPORTS" />  

    68.   

    69.             <tx:method name="*" propagation="SUPPORTS" />  

    70.         </tx:attributes>  

    71.     </tx:advice>  

    72.     <aop:config>  

    73.         <aop:pointcut id="transactionPointcut" expression="execution(* com.yingjun.test.service..*Impl.*(..))" />  

    74.         <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />  

    75.     </aop:config>  

    76. </beans>              

    77. <?xml version="1.0" encoding="UTF-8"?>  

    78. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

    79.     xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"  

    80.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"  

    81.     version="2.4">  

    82.   

    83.     <!-- 配置 Spring -->  

    84.     <context-param>  

    85.         <param-name>contextConfigLocation</param-name>  

    86.         <param-value>classpath:spring.xml</param-value>  

    87.     </context-param>  

    88.     <listener>  

    89.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  

    90.     </listener>  

    91.     <!-- 防止Spring内存溢出监听器 -->  

    92.     <listener>  

    93.         <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  

    94.     </listener>  

    95.   

    96.     <!-- 配置springmvc -->  

    97.     <servlet>  

    98.         <servlet-name>springMVC</servlet-name>  

    99.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  

    100.         <init-param>  

    101.             <param-name>contextConfigLocation</param-name>  

    102.             <param-value>classpath:spring-mvc.xml</param-value>  

    103.         </init-param>  

    104.         <load-on-startup>1</load-on-startup>  

    105.     </servlet>  

    106.     <servlet-mapping>  

    107.         <servlet-name>springMVC</servlet-name>  

    108.         <url-pattern>/</url-pattern>  

    109.     </servlet-mapping>  

    110.       

    111.     <!-- 字符集过滤器 -->  

    112.     <filter>  

    113.         <filter-name>encodingFilter</filter-name>  

    114.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  

    115.         <init-param>  

    116.             <param-name>encoding</param-name>  

    117.             <param-value>UTF-8</param-value>  

    118.         </init-param>  

    119.         <init-param>  

    120.             <param-name>forceEncoding</param-name>  

    121.             <param-value>true</param-value>  

    122.         </init-param>  

    123.     </filter>  

    124.     <filter-mapping>  

    125.         <filter-name>encodingFilter</filter-name>  

    126.         <url-pattern>/*</url-pattern>  

    127.     </filter-mapping>  

    128.       

    129. </web-app>  

    130. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

    131.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  

    132.     <modelVersion>4.0.0</modelVersion>  

    133.     <groupId>com.yingjun.test</groupId>  

    134.     <artifactId>TradingState</artifactId>  

    135.     <packaging>war</packaging>  

    136.     <version>2.0.1</version>  

    137.   

    138.     <properties>  

    139.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  

    140.         <maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>  

    141.         <spring.version>3.2.9.RELEASE</spring.version>  

    142.         <mybatis.version>3.1.1</mybatis.version>  

    143.         <mybatisspring.version>1.1.1</mybatisspring.version>  

    144.     </properties>  

    145.   

    146.     <dependencies>  

    147.         <dependency>  

    148.             <groupId>org.springframework</groupId>  

    149.             <artifactId>spring-core</artifactId>  

    150.             <version>${spring.version}</version>  

    151.         </dependency>  

    152.         <dependency>  

    153.             <groupId>org.springframework</groupId>  

    154.             <artifactId>spring-webmvc</artifactId>  

    155.             <version>${spring.version}</version>  

    156.         </dependency>  

    157.         <dependency>  

    158.             <groupId>org.springframework</groupId>  

    159.             <artifactId>spring-test</artifactId>  

    160.             <version>${spring.version}</version>  

    161.         </dependency>  

    162.         <dependency>  

    163.             <groupId>org.mybatis</groupId>  

    164.             <artifactId>mybatis</artifactId>  

    165.             <version>${mybatis.version}</version>  

    166.         </dependency>  

    167.         <dependency>  

    168.             <groupId>org.mybatis</groupId>  

    169.             <artifactId>mybatis-spring</artifactId>  

    170.             <version>${mybatisspring.version}</version>  

    171.         </dependency>  

    172.         <dependency>  

    173.             <groupId>mysql</groupId>  

    174.             <artifactId>mysql-connector-java</artifactId>  

    175.             <version>5.1.34</version>  

    176.         </dependency>  

    177.         <dependency>  

    178.             <groupId>junit</groupId>  

    179.             <artifactId>junit</artifactId>  

    180.             <version>4.11</version>  

    181.             <scope>test</scope>  

    182.         </dependency>  

    183.         <dependency>  

    184.             <groupId>c3p0</groupId>  

    185.             <artifactId>c3p0</artifactId>  

    186.             <version>0.9.1.2</version>  

    187.         </dependency>  

    188.         <dependency>  

    189.             <groupId>org.aspectj</groupId>  

    190.             <artifactId>aspectjweaver</artifactId>  

    191.             <version>1.8.1</version>  

    192.         </dependency>  

    193.         <dependency>  

    194.             <groupId>javax.servlet</groupId>  

    195.             <artifactId>jstl</artifactId>  

    196.             <version>1.2</version>  

    197.         </dependency>  

    198.         <dependency>  

    199.             <groupId>javax.servlet</groupId>  

    200.             <artifactId>servlet-api</artifactId>  

    201.             <version>3.0</version>  

    202.             <scope>provided</scope>  

    203.         </dependency>  

    204.         <dependency>  

    205.             <groupId>javax.servlet.jsp</groupId>  

    206.             <artifactId>jsp-api</artifactId>  

    207.             <version>2.2</version>  

    208.             <scope>provided</scope>  

    209.         </dependency>  

    210.         <dependency>  

    211.             <groupId>commons-fileupload</groupId>  

    212.             <artifactId>commons-fileupload</artifactId>  

    213.             <version>1.3.1</version>  

    214.         </dependency>  

    215.         <dependency>  

    216.             <groupId>commons-lang</groupId>  

    217.             <artifactId>commons-lang</artifactId>  

    218.             <version>2.6</version>  

    219.         </dependency>  

    220.         <dependency>  

    221.             <groupId>commons-codec</groupId>  

    222.             <artifactId>commons-codec</artifactId>  

    223.             <version>1.9</version>  

    224.         </dependency>  

    225.         <dependency>  

    226.             <groupId>org.apache.httpcomponents</groupId>  

    227.             <artifactId>httpclient</artifactId>  

    228.             <version>4.5</version>  

    229.         </dependency>  

    230.         <dependency>  

    231.             <groupId>org.slf4j</groupId>  

    232.             <artifactId>slf4j-api</artifactId>  

    233.             <version>1.7.10</version>  

    234.         </dependency>  

    235.         <dependency>  

    236.             <groupId>org.slf4j</groupId>  

    237.             <artifactId>slf4j-log4j12</artifactId>  

    238.             <version>1.7.10</version>  

    239.         </dependency>  

    240.         <dependency>  

    241.             <groupId>log4j</groupId>  

    242.             <artifactId>log4j</artifactId>  

    243.             <version>1.2.17</version>  

    244.         </dependency>  

    245.         <dependency>  

    246.             <groupId>com.alibaba</groupId>  

    247.             <artifactId>fastjson</artifactId>  

    248.             <version>1.1.41</version>  

    249.         </dependency>  

    250.         <dependency>  

    251.             <groupId>org.codehaus.jackson</groupId>  

    252.             <artifactId>jackson-mapper-asl</artifactId>  

    253.             <version>1.9.13</version>  

    254.         </dependency>  

    255.     </dependencies>  

    256.   

    257.     <build>  

    258.         <plugins>  

    259.             <plugin>  

    260.                 <artifactId>maven-compiler-plugin</artifactId>  

    261.                 <version>2.3.2</version>  

    262.                 <configuration>  

    263.                     <source>1.7</source>  

    264.                     <target>1.7</target>  

    265.                 </configuration>  

    266.             </plugin>  

    267.             <plugin>  

    268.                 <artifactId>maven-war-plugin</artifactId>  

    269.                 <version>2.2</version>  

    270.                 <configuration>  

    271.                     <version>3.0</version>  

    272.                     <failOnMissingWebXml>false</failOnMissingWebXml>  

    273.                 </configuration>  

    274.             </plugin>  

    275.         </plugins>  

    276.         <finalName>${project.artifactId}_${project.version}_${maven.build.timestamp}</finalName>  

    277.     </build>  

    278. </project>  

愿意了解框架技术或者源码的朋友直接加求求(企鹅):2042849237

更多详细源码参考来源:http://minglisoft.cn/technology


手把手Maven搭建SpringMVC+Spring+MyBatis框架

标签:springmvc   spring   mybatis   框架   maven   

原文地址:http://12933830.blog.51cto.com/12923830/1927482

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!