3梳綿を統合するSSMフレームワーク

統合手順

動的Webプロジェクトを構築するための0、

1、必要なjarパッケージ

  1. (SpringMVCを含む)春
  2. MyBatisの関連のjarパッケージ
  3. MyBatisの春統合パッケージ(いくつかの奇妙なエラーMyBatisのスプリング-2.0.2.jarを避けるために、高いバージョンを使用して個人的なお勧めします)
  4. データベース駆動型のパッケージ(mysqlのコネクタ-javaの-5.1.44.jar)
  5. サードパーティの接続プール(ドルイド-1.1.10.jar、春もDBCPのC3P0接続プールまたはプールc3p0-0.9.5.2.jar接続することができます)
  6. JSONの依存ジャクソン
  7. JSTLタグライブラリ

    以下のオプションです
  8. また、MyBatisのリバースエンジニアリングを使用することが、必要性はMyBatisの発電機コア-1.3.5.jarと増加したプロファイルをインポートしますmbg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>

    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!-- 配置数据库连接 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/ssm_crud" userId="root"
            password="123">
        </jdbcConnection>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- 指定Javabean生成的位置 -->
        <javaModelGenerator targetPackage="com.fei.pojo"
            targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- 指定sql映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="mapper"
            targetProject=".\src\main\resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- 指定mapper(dao)接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.fei.mapper" targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 指定每个表的生成策略 -->
        <table tableName="tbl_emp" domainObjectName="Employee"></table>
        <table tableName="tbl_dept" domainObjectName="Department"></table>

    </context>
</generatorConfiguration>

同様に、メインプログラムのコアとして

package com.fei.test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

/**
 * mybatis代码生成主程序
 * @Author xiaofei
 * @CreateDate 2019年9月2日
 */
public class MBGTest {
    
    public static void main(String[] args) throws Exception {
        
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("mybatis-generator.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
        
        System.out.println("代码生成完毕!请查收");
    }
}
  1. ページネーションプラグインを使用するには、必要がpagehelper-5.1.2.jarとjsqlparser-1.0.jarをインポートします
  2. ファイルアップロードパッケージだけでなく、他の関連するガイドを使用するには

2、統合のアイデア

  1. dao层
    • sqlMapConfig.xml、空のファイルがあることが、ファイルのヘッダーを必要とすることができます
    • ApplicationContextの-dao.xml
      • データベース接続プールのドルイド
      • sqlSessionFactoryオブジェクト、あなたは春必要とMyBatisの統合パッケージが追加されます
      • 動的プロキシの拡張版を開発するためマッパー設定ファイルスキャナ、マッパー
  2. サービス層
    • ApplicationContextの-service.xmlに:スキャナ構成パッケージ、走査コンポーネントクラス注釈付き@service
    • ApplicationContextの-tx.xml構成された総務
  3. ウェブ層
    • springmvc.xml:3つのコアコンポーネント
  4. web.xmlのフロントエンドコントローラ、インターセプタ、エンコーダフィルタは
    、ファイル内のDAO層とサービス層構成であってもよいです

ここでは、設定ファイルがある、私は-dao.xmlのApplicationContextとApplicationContextの-service.xmlサービスDAO層は、ばねmybatis.xmlのプロフィールを書きました

3、設定ファイルを書きます

  1. applicationContext.xmlを
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    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:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd 
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd 
                        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd ">
    
    <!-- spring的配置文件,这里主要配置业务和逻辑有关的配置,包括数据源,事务控制 -->
    <context:component-scan base-package="com.fei.service">
        <!-- <context:exclude-filter type="annotation" expression=" org.springframework.stereotype.Controller"/> -->
    </context:component-scan>
    
    <!-- ==================数据源的配置start================== -->
    <!-- 告诉spring让他去读取db.properties配置文件 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!-- ==================数据源的配置end================== -->


    <!-- ==============配置spring和mybatis的整合start============== -->
    <!-- MyBatis工厂 -->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 指定mybatis核心配置文件的位置 -->
        <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
        <!-- 指定mapper文件的位置 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>

    <!-- 配置扫描器,将mybatis接口的实现(一个代理对象)加入到ioc容器中,Mapper动态代理开发,扫描 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 指定基本包,扫描所有mapper接口的实现,加入到ioc容器中 -->
        <property name="basePackage" value="com.fei.mapper"/>
    </bean>
    
    <!-- 配置一个可以执行批量操作的sqlSession -->
    <bean id="sqlSession" class=" org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
        <constructor-arg name="executorType" value="BATCH"/>
    </bean>
    <!-- ==============配置spring和mybatis的整合end============== -->


    <!-- ====================事务控制的配置start==================== -->
    <!-- 注解事务 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 控制住数据源里面连接的开启、关闭、回滚等操作 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 1)开启基于注解的事务 -->
    <!-- <tx:annotation-driven transaction-manager="transactionManager"/> -->
    <!-- 2)开启基于xml配置的事务(比较重要的都是用配置形式) -->
    <aop:config>
        <!-- 切入点表达式 -->
        <aop:pointcut expression="execution(* com.fei.service..*(..))" id="txPoint"/>
        <!-- 配置事务增强 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
    </aop:config>
    <!-- 配置事务增强,事务如何切入 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 所有的方法都是事务方法 -->
            <tx:method name="*"/>
            <tx:method name="get*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <!-- ====================事务控制的配置end==================== -->
    
    
    <!-- spring配置文件核心点(数据源,与mybatis的整合,事务控制) -->
        
    <!-- springmvc的异常处理器 -->
    <!-- <bean class="com.fei.exception.CustomExceptionResolver"/> -->

</beans>

  1. 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:context="http://www.springframework.org/schema/context"
    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/context http://www.springframework.org/schema/context/spring-context.xsd
                        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
                        
    <!-- springmvc的配置文件,主要包含网站跳转的逻辑控制 -->
    
    <!-- 配置controller扫描,精准扫描 -->
    <context:component-scan base-package="com.fei.controller" />

    <!-- 定义视图文件解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
    <!-- 两个标准配置 -->
    <!-- 将springmvc不能处理的请求交给tomcat -->
    <mvc:default-servlet-handler/>
    <!-- 能支持一些springmvc更高级的一些功能,JSR303校验,快捷的ajax...,映射动态请求 -->
    <mvc:annotation-driven/>
    
    <!-- ================ 上面是springmvc的基本配置 ================ -->
    <!-- 对静态资源放行 -->
    <!-- <mvc:resources location="/css/" mapping="/css/**"/>
    <mvc:resources location="/js/" mapping="/js/**"/>
    <mvc:resources location="/fonts/" mapping="/fonts/**"/>
    <mvc:resources location="/img/" mapping="/img/**"/>
    <mvc:resources location="/bower_components/" mapping="/bower_components/**"/>
    <mvc:resources location="/dist/" mapping="/dist/**"/>
    <mvc:resources location="/plugins/" mapping="/plugins/**"/>  -->
    
    <!-- 配置拦截器(多个) -->
    <!-- <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <mvc:exclude-mapping path="/login"/>
            <mvc:exclude-mapping path="/static/**"/>
            <bean class="com.fei.ssmcrm.interceptor.LoginInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors> -->
    
    <!-- 读取配置文件,解决硬编码问题 -->
    <!-- <context:property-placeholder location="classpath:code-params.properties" /> -->
    
</beans>

  1. db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///ssm_crud?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123
  1. 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>

    <!-- 配置别名,放置实体类 -->
    <typeAliases>
        <package name="com.fei.pojo"/>
    </typeAliases>
    
    <!-- pagehelper分页插件注册 -->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!-- 分页参数合理化 -->
            <property name="resonable" value="true"/>
        </plugin>
    </plugins>
    
</configuration>

  1. 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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

    <!-- 1、启动spring容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 2、springmvc的前端控制器,拦截所有请求 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- 3、字符编码过滤器,一定要放在所有过滤器之前 -->
    <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>forceRequestEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <!-- 4、使用Restful风格的URI,将页面普通的post请求转为指定的delete或put请求 -->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- 处理直接发送put请求的过滤器 -->
    <filter>
        <filter-name>HttpPutFormContentFilter</filter-name>
        <filter-class> org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HttpPutFormContentFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

以下は、プロジェクトのlibディレクトリとパッケージ構造であり、

プロジェクトパッケージ構造

libディレクトリ

おすすめ

転載: www.cnblogs.com/zxfei/p/11469308.html