美文网首页
SSM三大框架整合

SSM三大框架整合

作者: Cheemin | 来源:发表于2018-03-05 22:38 被阅读0次

    1.创建数据库

    2.创建spring的maven项目

    • 解决web.xml缺失

    o 项目名右击---Java EE Tools-----Generate .....
    • 解决JDK版本问题,在POM中添加并update Maven

    <build>  
       <plugins>  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-compiler-plugin</artifactId>  
                <configuration>  
                    <source>1.8</source>
                    <target>1.8</target>  
                </configuration>  
            </plugin>  
        </plugins>  
    </build>
    

    3.导入jar包

    <dependencies>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
           <version>4.2.4.RELEASE</version>
       </dependency>
     <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-core</artifactId>
           <version>4.2.4.RELEASE</version>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-beans</artifactId>
           <version>4.2.4.RELEASE</version>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-expression</artifactId>
           <version>4.2.4.RELEASE</version>
       </dependency>
    <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-web</artifactId>
       <version>4.2.4.RELEASE</version>
    </dependency>
     <dependency>
       <groupId>aopalliance</groupId>
       <artifactId>aopalliance</artifactId>
       <version>1.0</version>
    </dependency>
    <dependency>
       <groupId>org.aspectj</groupId>
       <artifactId>aspectjweaver</artifactId>
       <version>1.6.8</version>
    </dependency>
    <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-aop</artifactId>
       <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-aspects</artifactId>
       <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-test</artifactId>
       <version>4.2.4.RELEASE</version>
       <scope>test</scope>
    </dependency>
     <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-jdbc</artifactId>
           <version>4.2.4.RELEASE</version>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-tx</artifactId>
           <version>4.2.4.RELEASE</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.2.2</version>
       </dependency>
       <dependency>
           <groupId>commons-pool</groupId>
           <artifactId>commons-pool</artifactId>
           <version>1.5.3</version>
       </dependency>
       <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>druid</artifactId>
           <version>1.0.18</version>
       </dependency>
       <dependency>
           <groupId>c3p0</groupId>
           <artifactId>c3p0</artifactId>
           <version>0.9.1.2</version>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-webmvc</artifactId>
           <version>4.2.4.RELEASE</version>
       </dependency>
     
     <dependency>
       <groupId>cglib</groupId>
       <artifactId>cglib</artifactId>
       <version>2.2.2</version>
    </dependency>
     
     
     <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>4.12</version>
       <scope>test</scope>
    </dependency>
    <dependency>
       <groupId>log4j</groupId>
       <artifactId>log4j</artifactId>
       <version>1.2.17</version>
    </dependency>
     
     <dependency>
       <groupId>org.mybatis</groupId>
       <artifactId>mybatis</artifactId>
       <version>3.2.7</version>
    </dependency>
    <dependency>
       <groupId>org.mybatis</groupId>
       <artifactId>mybatis-spring</artifactId>
       <version>1.2.2</version>
    </dependency>
    <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-api</artifactId>
       <version>1.7.5</version>
    </dependency>
    <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-log4j12</artifactId>
       <version>1.7.5</version>
       <scope>test</scope>
    </dependency>
    <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-context-support</artifactId>
       <version>4.2.4.RELEASE</version>
    </dependency>
       <dependency>
               <groupId>javax.servlet</groupId>
               <artifactId>servlet-api</artifactId>
               <version>3.0-alpha-1</version>
               <scope>provided</scope> 
           </dependency>
           
           <dependency>
               <groupId>javax.servlet</groupId>
               <artifactId>jstl</artifactId>
               <version>1.2</version>
           </dependency>
           <dependency>
               <groupId>javax.servlet.jsp</groupId>
               <artifactId>jsp-api</artifactId>
               <version>2.1</version>
            <scope>provided</scope> 
           </dependency>
           
           
           <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
       <groupId>commons-io</groupId>
       <artifactId>commons-io</artifactId>
       <version>2.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
    <dependency>
       <groupId>commons-fileupload</groupId>
       <artifactId>commons-fileupload</artifactId>
       <version>1.3.1</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.8.8</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.8.8</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.8.8</version>
    </dependency>
    </dependencies>
    

    4.开发配置文件

    4.1mybatis的配置文件SqlMapConfig.xml

    <?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="STDOUT_LOGGING" />
       </settings>
    </configuration>
    

    4.2spring核心配置文件applicationContext.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:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:c="http://www.springframework.org/schema/c"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <!-- 包扫描的配置 -->
        <context:component-scan base-package="cn.cheemin.springmvc.controller,cn.cheemin.springmvc.service"/>             
        <!-- 数据库连接池 -->
        <context:property-placeholder location="classpath:*.properties"/>
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="${jdbc.driver}"></property>
            <property name="url" value="${jdbc.url}"></property>
            <property name="username" value="${jdbc.username}"></property>
            <property name="password" value="${jdbc.password}"></property>
        </bean>
        <!-- sqlSessionFactoryBean 与mybatis整合 -->
        <bean class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <!-- 设置我们的mapper文件的扫描路径 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="cn.cheemin.springmvc.mapper"></property>
        </bean>
        <!-- 事物管理配置 -->
        <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <!-- 试用注解的方式来开启事物 -->
        <tx:annotation-driven/>
    </beans>
    

    4.3 springmvc核心配置文件

    <?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" 
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
           http://www.springframework.org/schema/mvc 
           http://www.springframework.org/schema/mvc/spring-mvc-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/task
           http://www.springframework.org/schema/task/spring-task-4.0.xsd
           http://code.alibabatech.com/schema/dubbo        
           http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
           
           <!-- 加载属性文件 -->
           <context:property-placeholder location="classpath:*.properties"/>
           <!-- 配置扫描器 -->
           <context:component-scan base-package="cn.cheemin.springmvc.controller,cn.cheemin.springmvc.service"/>
           <!-- 配置处理器映射器  适配器 -->
           <mvc:annotation-driven/>
           
           <!-- 配置视图解释器 jsp -->
           <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
               <property name="prefix" value="/WEB-INF/jsp/"/>
               <property name="suffix" value=".jsp"/>
           </bean>
    </beans>
    

    4.4其他重要配置文件

    • db.properties

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

    • source.properties

    //将常用,不常变的数据写入配置文件中
    • log4j.properties

    log4j.rootLogger=INFO,A
    log4j.appender.A=org.apache.log4j.ConsoleAppender
    log4j.appender.A.Target=System.out
    log4j.appender.A.layout=org.apache.log4j.PatternLayout
    log4j.appender.A.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n
    #log4j.rootLogger=debug , stdout , D , E
    log4j.appender.B=org.apache.log4j.RollingFileAppender 
    log4j.appender.B.File=project-jmall-log.log
    log4j.appender.B.MaxFileSize=10MB
    log4j.appender.B.Threshold=ALL
    log4j.appender.B.layout=org.apache.log4j.PatternLayout
    log4j.appender.B.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n
    log4j.logger.cn.cheemin.springmvc.mapper=DEBUGER
    

    • web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
       http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
       <welcome-file-list>
           <welcome-file>customer.action</welcome-file>
       </welcome-file-list>
       <!-- 上下文的位置 -->
       <context-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>classpath:applicationContext.xml</param-value>
       </context-param>
       <!-- Spring的监听器 -->
       <listener>
           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
       </listener>
       <!-- POST提交过滤器 UTF-8 -->
       <filter>
           <filter-name>encoding</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>
       </filter>
       <filter-mapping>
           <filter-name>encoding</filter-name>
           <url-pattern>*.action</url-pattern>
       </filter-mapping>
       <!-- 前端控制器 -->
       <servlet>
           <servlet-name>crm</servlet-name>
           <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
           <init-param>
               <param-name>contextConfigLocation</param-name>
               <!-- 此处不配置 默认找 /WEB-INF/[servlet-name]-servlet.xml -->
               <param-value>classpath:springmvc.xml</param-value>
           </init-param>
           <load-on-startup>1</load-on-startup>
       </servlet>
       <servlet-mapping>
           <servlet-name>crm</servlet-name>
           <!-- 1:*.do *.action 拦截以.do结尾的请求 (不拦截 jsp png jpg .js .css) 2:/ 拦截所有请求 
               (不拦截.jsp) 建议使用此种 方式 (拦截 .js.css .png) (放行静态资源) 3:/* 拦截所有请求(包括.jsp) 此种方式 不建议使用 -->
           <url-pattern>*.action</url-pattern>
       </servlet-mapping>
    </web-app>
    

    5.建立包结构

    根据spring配置文件和springmvc配置文件中的扫描包建立对应包本例包结构:
    • cn.CHEEMIN.springmvc
    o controller
    o service

    o pojo
    o mapper

    6.创建实体类与mapper接口与xml

    • 根据数据库表结构创建pojo实体类,并生成getter和setter
    数据库中bigint对应Long
    • 根据pojo实体类创建mapper接口与mapper.xml
    mapper.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="cn.cheemin.springmvc.mapper.BaseDictMapper">
    <select id="getBaseDictList" parameterType="string" resultType="cn.cheemin.springmvc.pojo.BaseDict">
    SELECT * FROM base_dict WHERE dict_type_code = #{dict_type_code}
    </select>
    </mapper>

    7.运行前准备

    项目中加入jsp、css、fonts、js等前端
    • jsp放入WEB-INF
    • 其他放入webapp
    • 将分页功能utils包添加到与mapper同级目录下。
    o 包括分页标签实现类l以及Page实体类
    • 分页标签tld文件夹放入jsp同级目录
    o 分页标签tld文件中的tag-class指明为分页标签类的全限定名

    8.编写controller层

    • 在类名前加@controller注解
    o 在controller内部方法前加@RequestMapping("浏览器访问路径(去除项目名称)")
    o 内部方法写完之后,项目即可初步运行。通过前一步的访问路径,可以调用一些方法。
    o controller成员变量使用@Autowired注入

    • 功能实现:controller调用service,service调用mapper

    9.编写service层

    @Service声明JavaBean
    @Transactional开启事务

    成员变量使用@Autowired

    10.编写mapper

    在sqlyog中编写能成功执行的sql语句后,复制到mapper.xml中修改

    相关文章

      网友评论

          本文标题:SSM三大框架整合

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