• SpringMVC+Spring+mybatis项目从零开始--Spring mybatis mysql配置实现


    上一章我们把SSM项目结构已搭建(SSM框架web项目从零开始--分布式项目结构搭建)完毕,本章将实现Spring,mybatis,mysql等相关配置。

    1.    外部架包依赖引入

    外部依赖包引入可以根据相关包名从

    网站:http://mvnrepository.com/

    获取依赖引入语句结构。

    SSM项目详细依赖包请见ssm-parent中的pom.xml文件。

    1.1  spring框架依赖包引入

    spring-context

    spring-beans

    spring-jdbc

    spring aspects

    spring-webmvc

    1.2  spring-mybatis依赖包引入

    mybatis

    mybatis-spring

    1.3  数据库依赖包引入

    mysql-connector-java

    druid

    1.4  redis依赖包引入

    jedis

    1.5  web相关依赖包引入

    jstl

    servlet-api

    jsp-api

    Jackson-databind

    1.6  其他日志和公共包依赖引入

    slf4j-log4j12

    junit

    commons-langs

    commons-io

    2.    各项目间依赖关系以及架包引入

    ssm-parent实现了所有外部依赖包引入

    (1)ssm-commons 实现公共的组件,工具类等

    (2)ssm-manager 实际业务管理工程,聚合工程,

    其包含工程依赖(1)

                                   (3)ssm-manager-pojo

                                       (4)ssm-manager-mapper 依赖(3)

                                       (5)ssm-manager-service  依赖(3,4)

                                       (6)ssm-manager-controller 依赖(3,5)

    3.    Spring简单介绍

    Spring是一个开源框架,Spring是于2003年兴起的一个轻量级的Java开发框架,由Rod Johnson创建。简单来说,Spring是一个分层的JavaSE/EEfull-stack(一站式) 轻量级开源框架。

    轻量——从大小与开销两方面而言Spring都是轻量的。完整的Spring框架可以在一个大小只有1MB多的JAR文件里发布。并且Spring所需的处理开销也是微不足道的。此外,Spring是非侵入式的:典型地,Spring应用中的对象不依赖于Spring的特定类。

    控制反转——Spring通过一种称作控制反转(IoC)的技术促进了低耦合。当应用了IoC,一个对象依赖的其它对象会通过被动的方式传递进来,而不是这个对象自己创建或者查找依赖对象。你可以认为IoC与JNDI相反——不是对象从容器中查找依赖,而是容器在对象初始化时不等对象请求就主动将依赖传递给它。

    面向切面——Spring提供了面向切面编程的丰富支持,允许通过分离应用的业务逻辑与系统级服务(例如审计(auditing)和事务transaction)管理)进行内聚性的开发。应用对象只实现它们应该做的——完成业务逻辑——仅此而已。它们并不负责(甚至是意识)其它的系统级关注点,例如日志或事务支持。

    容器——Spring包含并管理应用对象的配置和生命周期,在这个意义上它是一种容器,你可以配置你的每个bean如何被创建——基于一个可配置原型(prototype),你的bean可以创建一个单独的实例或者每次需要时都生成一个新的实例——以及它们是如何相互关联的。然而,Spring不应该被混同于传统的重量级的EJB容器,它们经常是庞大与笨重的,难以使用。

    框架——Spring可以将简单的组件配置、组合成为复杂的应用。在Spring中,应用对象被声明式地组合,典型地是在一个XML文件里。Spring也提供了很多基础功能(事务管理、持久化框架集成等等),将应用逻辑的开发留给了你。

    MVC——Spring的作用是整合,但不仅仅限于整合,Spring框架可以被看做是一个企业解决方案级别的框架。客户端发送请求,服务器控制器(由DispatcherServlet实现的)完成请求的转发,控制器调用一个用于映射的类HandlerMapping,该类用于将请求映射到对应的处理器来处理请求。HandlerMapping将请求映射到对应的处理器Controller(相当于Action)在Spring当中如果写一些处理器组件,一般实现Controller接口,在Controller中就可以调用一些Service或DAO来进行数据操作 ModelAndView用于存放从DAO中取出的数据,还可以存放响应视图的一些数据。如果想将处理结果返回给用户,那么在Spring框架中还提供一个视图组件ViewResolver,该组件根据Controller返回的标示,找到对应的视图,将响应response返回给用户。

    所有Spring的这些特征使你能够编写更干净、更可管理、并且更易于测试的代码。它们也为Spring中的各种模块提供了基础支持。

    --摘自百度百科

    总结几点:

    1)   spring是轻量级的,而且是开发源码的。

    2)   spring有控制反转和依赖注入的特性,将类之间的主动关联转换为被动关联,实现的相互间的解耦,通过配置文件,简化了开发;

    3)   aop面向切面编程,将系统核心业务与服务分离,实现了代码的解耦,通过切面实现全局调用,如日志,事务等;

    4)   与其他框架能很好的集成,如mybatis,struts2,hibernate,redis等。

    详情见官网:  http://spring.io/

    4.    mybatis简单介绍

    MyBatis是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis避免了几乎所有的 JDBC代码和手动设置参数以及获取结果集。MyBatis可以对配置和原生Map使用简单的 XML或注解,将接口和Java的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。

    详情见官网:http://www.mybatis.org/mybatis-3/zh/index.html

           mybatis相对hibernate优势

    1)   mybatis是轻量级的,学习成本较低,易上手;

    2)   mybatis是半自动化的,相关的sql需要手动实现;

    mybatis工作原理:

    5.    Spring,mybatis,mysql等配置实现

    由于SSM项目中ssm-controller依赖所有模块,为了方便配置文件易于管理,一般将项目的配置文件统一放置于smm-controller目录文件。

    5.1  ssm-manager-controller项目中的所有配置文件

    db.properties

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://127.0.0.1:3306/ussm?characterEncoding=utf-8
    jdbc.username=root
    jdbc.password=root

    log4j.pertites

    #log4j.rootLogger=DEBUG,INFO,Console,ERROR,stdout
    log4j.rootLogger=INFO,Console,ERROR,stdout
    
    # Druid
    log4j.logger.druid.sql=DEBUG
    log4j.logger.druid.sql.DataSource=warn
    log4j.logger.druid.sql.Connection=warn
    log4j.logger.druid.sql.Statement=DEBUG
    log4j.logger.druid.sql.ResultSet=warn
    
    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=[%p][%d{yyyy-MM-dd HH:mm:ss,SSS}][%c]%m%n

    SqlMapConfig.xml(可参考如下mybatis配置文档:http://www.mybatis.org/mybatis-3/zh/configuration.html#settings

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
     <settings>
            <!-- 日志开启 -->
            <setting name="logImpl" value="LOG4J"/>
            <!-- 二级缓存开启 -->
            <setting name="cacheEnabled" value="true"/>
            <setting name="lazyLoadingEnabled" value="false"/>
            <setting name="aggressiveLazyLoading" value="true"/>
        </settings>
    </configuration>

    applicationContext-mapper.xml

    <?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:jdbc="http://www.springframework.org/schema/jdbc"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
       
        <!-- 配置文件 -->
        <context:property-placeholder location="classpath:config/*.properties"/>
        <!-- 数据源 -->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
           destroy-method="close">
           <property name="url" value="${jdbc.url}" />
           <property name="username" value="${jdbc.username}" />
           <property name="password" value="${jdbc.password}" />
           <property name="driverClassName" value="${jdbc.driver}" />
           <property name="maxActive" value="10" />
           <property name="minIdle" value="5" />
           <property name="logAbandoned" value="true" />
        </bean>
        <!-- 配置sqlsessionFactory -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
           <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
           <property name="dataSource" ref="dataSource"></property>
        </bean>
        <!-- 配置扫描包,加载mapper代理对象 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
           <property name="basePackage" value="com.ssm.manager.mapper"></property>
        </bean>
    </beans>
    View Code

    applicationContext-service.xml

    <?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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
     
        <!-- 自动扫描service包 -->
        <context:component-scan base-package="com.ssm.manager.service"></context:component-scan>
     
        <import resource="applicationContext-mapper.xml"/>
        <import resource="applicationContext-trans.xml"/>
    </beans>
    View Code

    applicationContext-springmvc.xml

    <?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:aop="http://www.springframework.org/schema/aop" 
        xmlns:util="http://www.springframework.org/schema/util"  
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xmlns:mvc="http://www.springframework.org/schema/mvc"  
        xmlns:context="http://www.springframework.org/schema/context" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- MVC -->
    
        <!--  通过Web.xml的DispatcherServlet加载 -->
        
        <!-- 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的 -->
        <!-- <mvc:annotation-driven /> --> 
        <mvc:annotation-driven />
        <!--避免IE执行AJAX时,返回JSON出现下载文件 -->
        <bean id="mappingJacksonHttpMessageConverter"
              class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>text/html;charset=UTF-8</value>
                    <value>application/json; charset=utf-8</value>
                </list>
            </property>
        </bean>
        <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
        <bean
                class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
            <property name="messageConverters">
                <list>
                    <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->
                </list>
            </property>
        </bean>
        <!-- 2. 组件扫描路径配置,让Spring 容器知道需要扫描哪些包路径下可以加载到容器中的类 -->
        <!--     多个扫描路径配置  base-package="com.app,com.core,JUnit4" 也可以写多份,一般直接写多份 -->
        <context:component-scan base-package="com.ssm.manager.*" />
    
         <!-- 启动spring事务注解, $该启用必须在springMVC中,而不能在applicationContext.xml中配置,不然事务注解无效$
             也就是说只有这一行才能真正开启事务,单独地在类或方法上注解@Transaction只是作了事务标记而以-->  
         <tx:annotation-driven  transaction-manager="transactionManager" />
        <!-- 3. 处理在类级别上的@RequestMapping注解 -->
        
        <bean    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
            <property name="interceptors">
                <list>
                    <!-- 多个拦截器,顺序执行 -->
                    <!-- <ref bean="SpringMVCInterceptor" /> -->
                    <!-- <ref bean="OpenSessionInViewInterceptor" /> -->
                </list>
            </property>
        </bean>
        
        <!-- 4.处理方法级别上的@RequestMapping注解 -->
        <bean 
                class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
            <property name="webBindingInitializer">
                <bean
                    class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
                    <property name="conversionService">
                        <bean
                            class="org.springframework.format.support.FormattingConversionServiceFactoryBean"></bean>
                    </property>
                </bean>
            </property>
        </bean>
    
        <!-- 5.对模型视图名称的解析,即给模型视图名称添加前后缀 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/" /> <!-- 让ModelAndView("jsp/teacher/listTeachers.jsp") 从/WEB-INF/目录下开始 -->
            <property name="suffix" value="" />
            <!-- <property name="suffix" value=".jsp" /> -->
            <!-- Spring内部资源解析类 -->
            <property name="viewClass"    value="org.springframework.web.servlet.view.InternalResourceView" />
        </bean>
        
        <!-- 6.异常解析器 -->  
      <bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
        <property name="exceptionMappings">  
          <props>  
            <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">jsp/common/exception</prop>  
          </props>  
        </property>  
      </bean>  
        
    </beans>
    View Code

    applicationContext-trans.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
     
        <!-- 事务管理器 -->
        <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
           <!-- 数据源 -->
           <property name="dataSource" ref="dataSource" />
        </bean>
        <!-- 通知 -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
           <tx:attributes>
               <!-- 传播行为 -->
               <tx:method name="save*" propagation="REQUIRED" />
               <tx:method name="insert*" propagation="REQUIRED" />
               <tx:method name="add*" propagation="REQUIRED" />
               <tx:method name="create*" propagation="REQUIRED" />
               <tx:method name="delete*" propagation="REQUIRED" />
               <tx:method name="update*" propagation="REQUIRED" />
               <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
               <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
               <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
           </tx:attributes>
        </tx:advice>
        <!-- 切面 -->
        <aop:config>
           <aop:advisor advice-ref="txAdvice"
               pointcut="execution(* com.ssm.manager.service.*.*(..))" />
        </aop:config>
    </beans>
    View Code

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        id="ssm" version="2.5">
        <display-name>ssm-manager</display-name>
         <!-- spring配置文件整合配置 -->
          <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/applicationContext-service.xml</param-value>
          </context-param>
        
          <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          </listener>
          
         <context-param>
          <param-name>log4jConfigLocation</param-name>
          <param-value>classpath:config/log4j.properties</param-value>
          <!-- <param-value>/WEB-INF/log4j.xml</param-value> -->
        </context-param>
        <listener>
           <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
        </listener>
          <!-- 字符编码过滤器 -->
          <filter>
            <filter-name>characterEncodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
              <param-name>encoding</param-name>
              <param-value>UTF-8</param-value>
            </init-param>
            <init-param>
              <param-name>forceEncoding</param-name>
              <param-value>true</param-value>
            </init-param>
          </filter>
        
          <filter-mapping>
            <filter-name>characterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
          </filter-mapping>
        
          <!-- springmvc配置 -->
          <servlet>
            <servlet-name>spring</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath:spring/applicationContext-springmvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
          </servlet>
          <servlet-mapping>
            <servlet-name>spring</servlet-name>
            <url-pattern>*.action</url-pattern>
            <url-pattern>*.js</url-pattern>
            <url-pattern>*.css</url-pattern>
            <url-pattern>/css/**"</url-pattern>
            <url-pattern>/images/**</url-pattern>
            <url-pattern>/js/**</url-pattern>
          </servlet-mapping>
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
            <welcome-file>index.jsp</welcome-file>
            <welcome-file>default.html</welcome-file>
            <welcome-file>default.htm</welcome-file>
            <welcome-file>default.jsp</welcome-file>
        </welcome-file-list>
    </web-app>
    View Code

    UserController.java

    package com.ssm.manager.controller;
    
    import java.util.List;
    
    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.ResponseBody;
    import org.springframework.web.servlet.ModelAndView;
    
    import com.ssm.manager.pojo.User;
    import com.ssm.manager.service.UserService;
    
    @Controller
    public class UserController {
        @Autowired
        private UserService userService;
        
        @RequestMapping("getUsers")
        @ResponseBody
        public Object findUsers() {
            List<User> list = userService.getUsers();
            return list;
        }
        
        @RequestMapping("xxxTest")
        public ModelAndView getTestHtml() {
            System.out.println("=======================================");
            return new ModelAndView("test.html");
        }
    }
    View Code

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>com.ssm.manager</groupId>
            <artifactId>ssm-manager</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
        <artifactId>ssm-manager-controller</artifactId>
        <packaging>war</packaging>
        <dependencies>
            <dependency>
                <groupId>com.ssm.manager</groupId>
                <artifactId>ssm-manager-service</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <scope>test</scope>
            </dependency>
            <!-- 引入json包,防止报 com/fasterxml/jackson/core/JsonProcessingException 错误 -->
            <dependency>
                <groupId>org.codehaus.jackson</groupId>
                <artifactId>jackson-mapper-asl</artifactId>
            </dependency>
            <!-- 引入json包 -->
            <dependency>
                <groupId>org.codehaus.jackson</groupId>
                <artifactId>jackson-core-asl</artifactId>
            </dependency>
            <!-- 引入json包 -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
            </dependency>
            <!-- 引入json包 -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
            </dependency>
            <!-- 引入json包 -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
            </dependency>
        </dependencies>
    </project>
    View Code

    5.2  ssm-manager-mapper项目中所有配置文件

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <parent>
        <groupId>com.ssm.manager</groupId>
        <artifactId>ssm-manager</artifactId>
        <version>0.0.1-SNAPSHOT</version>
      </parent>
      <artifactId>ssm-manager-mapper</artifactId>
      <!-- 依赖管理 -->
        <dependencies>
            <dependency>
                <groupId>com.ssm.manager</groupId>
                <artifactId>ssm-manager-pojo</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <!-- Mybatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
            </dependency>
            <!-- MySql -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <!-- 连接池 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
            </dependency>
            <!-- 连接池 -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
            </dependency>
        </dependencies>
        <!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
        <build>
            <resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                    </includes>
                    <filtering>false</filtering>
                </resource>
            </resources>
        </build>
    </project>
    View Code

    UserMapper.xml

    <?xml version="1.0" encoding="UTF-8"?>  
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"    
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">   
    <mapper namespace="com.ssm.manager.mapper.UserMapper">   
        <resultMap id="UserMapper" type="com.ssm.manager.pojo.User">   
            <id property="id" column="id" />   
            <result property="userName" column="userName" />   
            <result property="password" column="password" />   
        </resultMap>   
           
        <select id="getUsers" resultMap="UserMapper">   
            select id,userName,password from t_user       
        </select> 
        <insert id="xxxUsers" parameterType="int">
            INSERT INTO t_user (id,userName,password) VALUES (3,'lisi','123456');
        </insert>
    </mapper>
    View Code

    UserMapper.java

    /**   
     * @Title: UserMapper.java 
     * @Package com.ssm.manager.mapper 
     * @Description: TODO(用一句话描述该文件做什么) 
     * @author 天源迪科 
     * @date 2017年10月11日 下午4:46:07 
     * @version V1.0   
     * @Copyright: 2017 www.tydic.com Inc. All rights reserved. 
     * 注意:本内容仅限于深圳天源迪科信息技术股份有限公司内部传阅,禁止外泄以及用于其他的商业目 
     */
    package com.ssm.manager.mapper;
    
    import java.util.List;
    
    import com.ssm.manager.pojo.User;
    
    /** 
     * @ClassName: UserMapper 
     * @Description: TODO(这里用一句话描述这个类的作用) 
     * @author 天源迪科
     * @date 2017年10月11日 下午4:46:07 
     *  
     * @Copyright: 2017 www.tydic.com Inc. All rights reserved. 
     * 注意:本内容仅限于深圳天源迪科信息技术股份有限公司内部传阅,禁止外泄以及用于其他的商业目 
     */
    public interface UserMapper {
    
        public List<User> getUsers();
    }
    View Code

    5.3  ssm-manager-pojo所有文件的配置

    User.java

    /**   
     * @Title: User.java 
     * @Package com.ssm.manager.pojo 
     * @Description: TODO(用一句话描述该文件做什么) 
     * @author 天源迪科 
     * @date 2017年10月11日 下午4:43:35 
     * @version V1.0   
     * @Copyright: 2017 www.tydic.com Inc. All rights reserved. 
     * 注意:本内容仅限于深圳天源迪科信息技术股份有限公司内部传阅,禁止外泄以及用于其他的商业目 
     */
    package com.ssm.manager.pojo;
    
    /** 
     * @ClassName: User 
     * @Description: TODO(这里用一句话描述这个类的作用) 
     * @author 天源迪科
     * @date 2017年10月11日 下午4:43:35 
     *  
     * @Copyright: 2017 www.tydic.com Inc. All rights reserved. 
     * 注意:本内容仅限于深圳天源迪科信息技术股份有限公司内部传阅,禁止外泄以及用于其他的商业目 
     */
    public class User {
    
        private int id;
        private String userName;
        private String password;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getUserName() {
            return userName;
        }
        public void setUserName(String userName) {
            this.userName = userName;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        @Override
        public String toString() {
            return "User [id=" + id + ", userName=" + userName + ", password=" + password + "]";
        }
        
        
    }
    View Code

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <parent>
        <groupId>com.ssm.manager</groupId>
        <artifactId>ssm-manager</artifactId>
        <version>0.0.1-SNAPSHOT</version>
      </parent>
      <artifactId>ssm-manager-pojo</artifactId>
    </project>
    View Code

    5.4  ssm-manager-service所有文件的配置信息

    UserService.java

    package com.ssm.manager.service;
    import java.util.List;
    
    import com.ssm.manager.pojo.User;
    
    /**   
     * @Title: UserService.java 
     * @Package  
     * @Description: TODO(用一句话描述该文件做什么) 
     * @author 天源迪科 
     * @date 2017年10月11日 下午4:49:28 
     * @version V1.0   
     * @Copyright: 2017 www.tydic.com Inc. All rights reserved. 
     * 注意:本内容仅限于深圳天源迪科信息技术股份有限公司内部传阅,禁止外泄以及用于其他的商业目 
     */
    
    /** 
     * @ClassName: UserService 
     * @Description: TODO(这里用一句话描述这个类的作用) 
     * @author 天源迪科
     * @date 2017年10月11日 下午4:49:28 
     *  
     * @Copyright: 2017 www.tydic.com Inc. All rights reserved. 
     * 注意:本内容仅限于深圳天源迪科信息技术股份有限公司内部传阅,禁止外泄以及用于其他的商业目 
     */
    public interface UserService {
    
        public List<User> getUsers();
    }
    View Code

    UserServiceImpl.java

    /**   
     * @Title: UserServiceImpl.java 
     * @Package com.ssm.manager.service 
     * @Description: TODO(用一句话描述该文件做什么) 
     * @author 天源迪科 
     * @date 2017年10月11日 下午4:51:52 
     * @version V1.0   
     * @Copyright: 2017 www.tydic.com Inc. All rights reserved. 
     * 注意:本内容仅限于深圳天源迪科信息技术股份有限公司内部传阅,禁止外泄以及用于其他的商业目 
     */
    package com.ssm.manager.service;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.ssm.manager.mapper.UserMapper;
    import com.ssm.manager.pojo.User;
    
    /** 
     * @ClassName: UserServiceImpl 
     * @Description: TODO(这里用一句话描述这个类的作用) 
     * @author 天源迪科
     * @date 2017年10月11日 下午4:51:52 
     *  
     * @Copyright: 2017 www.tydic.com Inc. All rights reserved. 
     * 注意:本内容仅限于深圳天源迪科信息技术股份有限公司内部传阅,禁止外泄以及用于其他的商业目 
     */
    @Service
    public class UserServiceImpl implements UserService {
        @Autowired
        private UserMapper userMapper;
        
        public List<User> getUsers() {
            
            return userMapper.getUsers();
        }
    
    
    }
    View Code

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <parent>
        <groupId>com.ssm.manager</groupId>
        <artifactId>ssm-manager</artifactId>
        <version>0.0.1-SNAPSHOT</version>
      </parent>
      <artifactId>ssm-manager-service</artifactId>
      <!-- 依赖管理 -->
        <dependencies>
            <dependency>
                <groupId>com.ssm.manager</groupId>
                <artifactId>ssm-manager-mapper</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <!-- Spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
            </dependency>
        </dependencies>
    </project>
    View Code

    5.5  ssm-manager-commons所有文件配置信息

     pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <parent>
        <groupId>com.ssm.parent</groupId>
        <artifactId>ssm-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
      </parent>
      <groupId>com.ssm.commons</groupId>
      <artifactId>ssm-commons</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    </project>
    View Code

    5.6  ssm-parent所有文件的配置信息

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.ssm.parent</groupId>
      <artifactId>ssm-parent</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>pom</packaging>
      <properties>
              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
              <java.version>1.7</java.version>
            <junit.version>4.12</junit.version>
            <spring.version>4.1.3.RELEASE</spring.version>
            <mybatis.version>3.2.8</mybatis.version>
            <mybatis.spring.version>1.2.2</mybatis.spring.version>
            <mysql.version>5.1.32</mysql.version>
            <slf4j.version>1.6.4</slf4j.version>
            <log4j.version>1.2.17</log4j.version>
            <jackson.version>2.4.2</jackson.version>
            <druid.version>1.0.9</druid.version>
            <jstl.version>1.2</jstl.version>
            <servlet-api.version>2.5</servlet-api.version>
            <jsp-api.version>2.0</jsp-api.version>
            <commons-lang3.version>3.3.2</commons-lang3.version>
            <commons-io.version>1.3.2</commons-io.version>
            <jedis.version>2.7.2</jedis.version>
        </properties>
        <dependencyManagement>
            <dependencies>
                <!-- Apache工具组件 -->
                <dependency>
                  <groupId>log4j</groupId>
                  <artifactId>log4j</artifactId>
                  <version>${log4j.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.commons</groupId>
                    <artifactId>commons-lang3</artifactId>
                    <version>${commons-lang3.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.commons</groupId>
                    <artifactId>commons-io</artifactId>
                    <version>${commons-io.version}</version>
                </dependency>
                <!-- Jackson Json处理工具包 -->
                <dependency>
                    <groupId>com.fasterxml.jackson.core</groupId>
                    <artifactId>jackson-databind</artifactId>
                    <version>${jackson.version}</version>
                </dependency>
                <!-- 单元测试 -->
                <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>${junit.version}</version>
                    <scope>test</scope>
                </dependency>
                <!-- 日志处理 -->
                <dependency>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                    <version>${slf4j.version}</version>
                </dependency>
                <!-- Mybatis -->
                <dependency>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis</artifactId>
                    <version>${mybatis.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis-spring</artifactId>
                    <version>${mybatis.spring.version}</version>
                </dependency>
                <!-- MySql -->
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>${mysql.version}</version>
                </dependency>
                <!-- 连接池 -->
                <dependency>
                    <groupId>com.alibaba</groupId>
                    <artifactId>druid</artifactId>
                    <version>${druid.version}</version>
                </dependency>
                <!-- Spring -->
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-beans</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-webmvc</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-jdbc</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-aspects</artifactId>
                    <version>${spring.version}</version>
                </dependency>
                <!-- JSP相关 -->
                <dependency>
                    <groupId>jstl</groupId>
                    <artifactId>jstl</artifactId>
                    <version>${jstl.version}</version>
                </dependency>
                <dependency>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                    <version>${servlet-api.version}</version>
                    <scope>provided</scope>
                </dependency>
                <dependency>
                    <groupId>javax.servlet</groupId>
                    <artifactId>jsp-api</artifactId>
                    <version>${jsp-api.version}</version>
                    <scope>provided</scope>
                </dependency>
                <!-- Redis客户端 -->
                <dependency>
                    <groupId>redis.clients</groupId>
                    <artifactId>jedis</artifactId>
                    <version>${jedis.version}</version>
                </dependency>
                <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl -->
                <dependency>
                  <groupId>org.codehaus.jackson</groupId>
                  <artifactId>jackson-mapper-asl</artifactId>
                  <version>1.9.13</version>
                </dependency>
                <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl -->
                <dependency>
                  <groupId>org.codehaus.jackson</groupId>
                  <artifactId>jackson-core-asl</artifactId>
                  <version>1.9.13</version>
                </dependency>
                <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
                <dependency>
                  <groupId>com.fasterxml.jackson.core</groupId>
                  <artifactId>jackson-annotations</artifactId>
                  <version>2.9.0</version>
                </dependency>
                <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
                <dependency>
                  <groupId>com.fasterxml.jackson.core</groupId>
                  <artifactId>jackson-core</artifactId>
                  <version>2.9.0</version>
                </dependency>
                <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
                <dependency>
                  <groupId>com.fasterxml.jackson.core</groupId>
                  <artifactId>jackson-databind</artifactId>
                  <version>2.9.0</version>
                </dependency>
            </dependencies>
        </dependencyManagement>
        
        <build>
            <finalName>${project.artifactId}</finalName>
            <plugins>
                <!-- 资源文件拷贝插件 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>2.7</version>
                    <configuration>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
                <!-- java编译插件 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.2</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
            </plugins>
            <pluginManagement>
                <plugins>
                    <!-- 配置Tomcat插件 -->
                    <plugin>
                        <groupId>org.apache.tomcat.maven</groupId>
                        <artifactId>tomcat7-maven-plugin</artifactId>
                        <version>2.2</version>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    </project>
    View Code

    所有配置文件信息配置完毕:

    8.    总结

    本章我们讲解了SSM项目中配置各项目pom文件、spring的事务、springaop、mybatis等配置。

    代码路径(待确定):

    mysql创建语句:

    /*
    Navicat MySQL Data Transfer
    
    Source Server         : GX
    Source Server Version : 50636
    Source Host           : 127.0.0.1:3306
    Source Database       : ussm
    
    Target Server Type    : MYSQL
    Target Server Version : 50636
    File Encoding         : 65001
    
    Date: 2017-10-11 21:10:51
    */
    
    SET FOREIGN_KEY_CHECKS=0;
    
    -- ----------------------------
    -- Table structure for t_user
    -- ----------------------------
    DROP TABLE IF EXISTS `t_user`;
    CREATE TABLE `t_user` (
      `id` int(32) NOT NULL,
      `userName` varchar(255) DEFAULT NULL,
      `password` varchar(255) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
    
    -- ----------------------------
    -- Records of t_user
    -- ----------------------------
    INSERT INTO `t_user` VALUES ('1', 'liuren', '123456');
    INSERT INTO `t_user` VALUES ('2', 'lisi', '123456');
    INSERT INTO `t_user` VALUES ('3', 'lisi', '123456');

    三.  SSM项目-redis缓存策略和配置实现

    四.  SSM项目-Springmvc配置实现

  • 相关阅读:
    向你的C语言项目中加入多线程
    <解析>speaker verification模型中的GE2E损失函数
    【笔记】 springCloud-configServer配置中心
    springboot--ActiveMQ--消息队列
    Fdfs上传的图片批量删除
    【笔记】负载均衡Robbin之不同服务使用不同的策略
    【笔记】Ribbon负载均衡伪随机算法
    【笔记】01 -- Spring-Cloud介绍
    linux防火墙
    SpringBoot起飞系列-使用idea搭建环境(二)
  • 原文地址:https://www.cnblogs.com/lr393993507/p/7652821.html
Copyright © 2020-2023  润新知