您的位置:首页 > 其它

SSM整合的关键配置文件整理

2018-04-02 11:25 344 查看
最近经常在配置SSM+Maven,把关键配置文件都记录一下

JDBC数据库配置文件

文件名:jdbc.properties
文件路径:src/main/resources/jdbc.properties
文件内容:jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql:///ssmcrud?useSSL=truejdbc.username=root
jdbc.password=123456
 

web.xml核心配置文件

文件名:web.xml
路径:src/main/webapp/WEB-INF/web.xml
配置内容:

1.  文件头

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

2.  注册spring配置文件的位置

 <!--2.1 注册Spring配置文件的位置 -->   <context-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath:applicationContext.xml</param-value>
   </context-param>
 

3.  注册servletcontext监听器

 <!--2.2 注册ServletContext监听器 -->   <!-- 用于创建Spring容器对象放入ServletContext的域属性空间中,保证整个应用中的Spring容器唯一 -->   <listener>      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
 

4.  注册字符集过滤器

 <!--2.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>forceReponseEncoding</param-name>        <param-value>true</param-value>      </init-param>   </filter>    <filter-mapping>      <filter-name>CharacterEncodingFilter</filter-name>      <url-pattern>/*</url-pattern>
   </filter-mapping>
 

5.  配置springMVC中央调度器

 <!--2.4 配置中央调度器拦截所有请求-->   <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:spring-mvc.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>
 

6.  配置Rest风格的URI(可选)

 <!--2.5 使用Rest风格的URI -->   <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>
 

7.  解决Tomcat对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>
 

Spring核心配置文件

文件名:applicationContext.xml
路径:/src/main/resource/applicationContext.xml
配置内容:

1.  文件头

 <?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"   xsi:schemaLocation="   http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-4.2.xsd   http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-4.2.xsd   http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-4.2.xsd   http://www.springframework.org/schema/tx    http://www.springframework.org/schema/tx/spring-tx.xsd">

2.  数据源配置

 <!--**********    数据源配置     **********-->   <!-- 1. 配置读取jdbc.properties文件 -->   <context:property-placeholder location="classpath:jdbc.properties"/>   <!-- 2. 配置C3P0连接池 -->   <bean name="C3P0DataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource">      <property name="jdbcUrl"value="${jdbc.url}" ></property>      <property name="driverClass"value="${jdbc.driver}" ></property>      <property name="user"value="${jdbc.username}" ></property>      <property name="password"value="${jdbc.password}" ></property>
   </bean>
 

3.  业务注册

 <!--**********    业务注册    **********-->   <!-- Service的注册业务逻辑注册 -->   <!-- 配置扫描器 -->
   <context:component-scan base-package="com.crud.service" />
 

4.  整合Mybatis

 <!--**********    整合Mybatis   **********-->   <!-- 1. 注册SqlSessionFactoryBean -->   <bean id="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">      <!-- 指定mybatis主配置文件的位置 -->      <property name="configLocation"value="classpath:mybatis.xml"/>      <!-- 连接池注入 -->      <property name="dataSource"ref="C3P0DataSource"/>      <!-- 指定mapper文件的位置 -->      <property name="mapperLocations"value="classpath:mapper/*.xml"/>   </bean>   <!-- 2. 配置扫描器 -->   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">      <property name="sqlSessionFactoryBeanName"value="sqlSessionFactory"/>      <!-- 扫描所有的DAO接口加入到IOC容器中 -->      <property name="basePackage"value="com.crud.dao" />
   </bean>
 

5.  配置批量执行的sqlsession(可选)

 <!--**********    批量执行的SqlSession      ********** -->   <bean id="sqlSession"class="org.mybatis.spring.SqlSessionTemplate">      <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />      <constructor-arg name="executorType" value="BATCH" />
   </bean>
 

6.  事务配置

 <!--**********    事务配置    **********-->   <!-- 1. 配置事务管理器 -->   <bean id="transactionManager"   class="org.springframework.jdbc.datasource.DataSourceTransactionManager">      <!-- 控制住数据源 -->      <property name="dataSource"ref="C3P0DataSource"/>   </bean>      <!-- 2. 注册事务通知 -->   <!-- 比较重要的要使用配置式 -->   <tx:advice id="transactionAdvice"transaction-manager="transactionManager">      <tx:attributes>        <!-- 所有方法都是事务方法 -->        <tx:method name="*" isolation="DEFAULT"propagation="REQUIRED"/>        <!-- get查询方法为只读,优化查询效率 -->        <tx:method name="get*" read-only="true"/>      </tx:attributes>   </tx:advice>   <!-- 3. aop配置 -->   <aop:config>      <aop:pointcut expression="execution(* com.crud.service..*(..))"id="myPointCut"/>      <aop:advisor advice-ref="transactionAdvice" pointcut-ref="myPointCut"/>
   </aop:config>
 

SpringMVC核心配置文件

文件名:spring-mvc.xml
路径:/src/main/resources/spring-mvc.xml
配置内容:

1.  表头

 <?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:p="http://www.springframework.org/schema/p"   xmlns:context="http://www.springframework.org/schema/context"   xmlns:mvc="http://www.springframework.org/schema/mvc"   xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsd       http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
       http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd">

2.  配置组件扫描器

 <!--跳转逻辑的配置 -->   <!-- 扫描业务逻辑组件,配置组件扫描器,注解式使用,只扫描控制器-->
   <context:component-scan base-package="com.crud.controller" />

3.  配置视图解析器

 <!--2.配置视图解析器 -->   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">      <!-- 前缀 -->      <property name="prefix"value="/WEB-INF/views/" />      <!-- 后缀 -->      <property name="suffix"value=".jsp" />
   </bean>

4.  配置静态资源交给tomcat处理

 <!--把spirngmvc不能处理的请求交给tomcat比如静态资源图片等-->
   <mvc:default-servlet-handler/>

5.  配置注解驱动(高级功能)

 <!--配置注解驱动映射动态请求支持springmvc一些高级功能,比如JSR303校验,快捷AJAX请求-->
   <mvc:annotation-driven/>
 

MyBatis核心配置文件

1.  表头

 <?xml version="1.0"encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTDConfig 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
 

2.  开启驼峰命名规则

 <!--开启驼峰命名规则 -->   <settings>      <setting name="mapUnderscoreToCamelCase"value="true"/>
   </settings>
 

3.  配置实体类别名

 <!--配置别名实体类的位置方便引用-->   <typeAliases>      <package name="com.crud.dao"/>
   </typeAliases>
 

4.  配置pagehelper分页功能(可选)

 <plugins>       <!-- com.github.pagehelper为PageHelper类所在包名 -->       <plugin interceptor="com.github.pagehelper.PageInterceptor">         <!-- 不到达不正确的页码 -->         <property name="reasonable"value="true"/>      </plugin>
   </plugins>
 

Mybatis核心-逆向工程配置

mbg.xml

文件名:mbg.xml
路径:项目根目录
配置内容:

1.   配置数据库连接以及不生成注释

 <context id="DB2Tables"targetRuntime="MyBatis3">      <!-- 配置 suppressAllComments 为true表示生成的文件不包含注释 -->      <commentGenerator>        <property name="suppressAllComments" value="true" />      </commentGenerator>      <jdbcConnection driverClass="com.mysql.jdbc.Driver"        connectionURL="jdbc:mysql:///ssmcrud?useSSL=false"        userId="root"        password="123456">      </jdbcConnection>      <javaTypeResolver>        <property name="forceBigDecimals" value="false" />
      </javaTypeResolver>

2.   指定java bean生成的位置

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

3.   指定SQL映射文件生成的位置

 <sqlMapGenerator targetPackage="mapper"        targetProject=".\src\main\resources">        <property name="enableSubPackages" value="true" />
      </sqlMapGenerator>
 

4.   指定dao接口生成的位置

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

5.   指定每个表的生成策略

 <table tableName="tbl_emp"domainObjectName="Employee"></table>
      <table tableName="tbl_dept"domainObjectName="Department"></table>
 

MBGTest.java

       使用java代码完成逆向工程功能
 
代码:
package com.crud.test;
 
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
 
importorg.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
importorg.mybatis.generator.config.xml.ConfigurationParser;
importorg.mybatis.generator.exception.InvalidConfigurationException;
importorg.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
 
public class MBGTest {
       publicstatic void main(String[] args) throws IOException,
       XMLParserException,SQLException,
       InterruptedException,InvalidConfigurationException {
              List<String>warnings = new ArrayList<String>();
              booleanoverwrite = true;
              FileconfigFile = new File("mbg.xml");
              ConfigurationParsercp = new ConfigurationParser(warnings);
              Configurationconfig = cp.parseConfiguration(configFile);
              DefaultShellCallbackcallback = new DefaultShellCallback(overwrite);
              MyBatisGeneratormyBatisGenerator = new MyBatisGenerator(config, callback, warnings);
              myBatisGenerator.generate(null);
       }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: