美文网首页
SSM框架搭建总结

SSM框架搭建总结

作者: icecrea | 来源:发表于2017-08-03 21:42 被阅读38次

    需要的依赖介绍:
    spring-webmvc 导入该jar包会自动导入很多spring相关依赖的包
    jackson-core,data-bind,annotations 这三个是json相关的包
    spring-jdbc,mybatis,mybatis-spring两者结合的一个插件,mysql-connector-java 是mysql驱动,commons-dbcp数据库连接池
    junit测试

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.2.8.RELEASE</version>
    </dependency>
    
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.8.5</version>
    </dependency>
    
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.5</version>
    </dependency>
    
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.8.5</version>
    </dependency>
    
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>3.2.8.RELEASE</version>
    </dependency>
    
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.0</version>
    </dependency>
    
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.3.1</version>
    </dependency>
    
    <dependency>
        <groupId>Mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.38</version>
    </dependency>
    
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>
    
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    

    配置Spring MVC控制器: web.xml 添加dispatcherservlet

      <servlet>
        <description></description>
        <display-name>DispatcherServlet</display-name>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <description></description>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:conf/spring-*.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
      </servlet-mapping>
    

    配置文件格式可以根据spring版本去官网找 此处格式为

    <?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">
    
       <!--  具体配置 -- >
    <beans>
    

    配置Spring-MVC: conf/spring-mvc.xml

      <!-- 配置组件扫描 -->
      <context:component-scan 
          base-package="com.bugsys.controller"/>
      <!-- 配置MVC注解扫描 -->
      <mvc:annotation-driven />
    

    配置MyBatis: conf/spring-mybatis.xml

    <!-- 配置dbcp连接池: 连接到数据库-->
    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <!-- 连接池的基本连接参数 -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/cloud_note"></property> 
        <property name="username" value="root"></property>
        <property name="password" value=""></property>
        <!-- 连接池可选参数 -->
        <property name="maxActive" value="50"></property> 
        <property name="initialSize" value="5"></property> 
        <property name="maxIdle" value="5"></property> 
    </bean>
    
    <!-- 配置MyBatis的 Session 工厂 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!-- 声明MyBatis SQL 声明文件保存的地方 -->
    
        <property name="mapperLocations" value="classpath:mapper/*.xml">    </property>
    
      </bean>
    
    <!-- 配置MyBatis的自动接口扫描 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!-- 数据访问接口的存储位置 -->
            <property name="basePackage" value="cn.tedu.note.dao"></property> 
        </bean>
    
    配置spring-mybatis.xml 包含bean: dpcp链接池, mybatis的session工厂, mybatis自动扫描接口
        dpcp连接池: 
            id=dataSource 其参数是包下的set方法取消set,并且把第一个字母换成小写 
            driverClassName/url/username/password
        session工厂:来自mybatis-spring这个包 
            sqlSessionFactoryBean dataSource ref='引用前面定义好的的dataSource对象' 
            mapperLocations 生命mybatis sql声明文件保存的地方  value="classpath:mapper/*.xml" 
        mybatis自动扫描接口: 数据访问接口的存放位置
            mybatis-spring包下 org.mybatis.spring.mapper包下的 MapperScannerConfigurer方法 basePackage 
    

    相关文章

      网友评论

          本文标题:SSM框架搭建总结

          本文链接:https://www.haomeiwen.com/subject/wnlmlxtx.html