经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Windows » 查看文章
Spring mybatis 之-ssm框架环境搭建(方案一)
来源:cnblogs  作者:肆无忌惮搬码工  时间:2019/7/22 10:40:35  对本文有异议

SSM框架- S-Spring  S-Spring mvc M-mybatis  就需要以下几个配置文件,放在resources文件夹下面:

db.properties 放的是数据库连接池的配置文件,有数据库jdbc的连接信息:

  1. # JDBC
  2. jdbc.driverClass=com.mysql.jdbc.Driver
  3. jdbc.connectionURL=jdbc:mysql://47.95.228.179:3306/mykidsshop?useUnicode=true&characterEncoding=utf-8&useSSL=false
  4. jdbc.username=root
  5. jdbc.password=123
  6. # JDBC Pool
  7. jdbc.pool.init=1
  8. jdbc.pool.minIdle=3
  9. jdbc.pool.maxActive=20
  10. # JDBC Test
  11. jdbc.testSql=SELECT 'x' FROM DUAL
  12. #============================#
  13. #==== Framework settings ====#
  14. #============================#
  15. # \u89c6\u56fe\u6587\u4ef6\u5b58\u653e\u8def\u5f84
  16. web.view.prefix=/WEB-INF/views/
  17. web.view.suffix=.jsp

mybatis-config.xml  放置的是mybatis相关的配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  4. <configuration>
  5. <!-- 全局参数 -->
  6. <settings>
  7. <!-- 打印 SQL 语句 -->
  8. <setting name="logImpl" value="STDOUT_LOGGING" />
  9. <!-- 使全局的映射器启用或禁用缓存。 -->
  10. <setting name="cacheEnabled" value="false"/>
  11. <!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 -->
  12. <setting name="lazyLoadingEnabled" value="true"/>
  13. <!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。 -->
  14. <setting name="aggressiveLazyLoading" value="true"/>
  15. <!-- 是否允许单条 SQL 返回多个数据集 (取决于驱动的兼容性) default:true -->
  16. <setting name="multipleResultSetsEnabled" value="true"/>
  17. <!-- 是否可以使用列的别名 (取决于驱动的兼容性) default:true -->
  18. <setting name="useColumnLabel" value="true"/>
  19. <!-- 允许 JDBC 生成主键。需要驱动器支持。如果设为了 true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。 default:false -->
  20. <setting name="useGeneratedKeys" value="false"/>
  21. <!-- 指定 MyBatis 如何自动映射 数据基表的列 NONE:不映射 PARTIAL:部分 FULL:全部 -->
  22. <setting name="autoMappingBehavior" value="PARTIAL"/>
  23. <!-- 这是默认的执行类型 (SIMPLE: 简单; REUSE: 执行器可能重复使用prepared statements语句;BATCH: 执行器可以重复执行语句和批量更新) -->
  24. <setting name="defaultExecutorType" value="SIMPLE"/>
  25. <!-- 使用驼峰命名法转换字段。 -->
  26. <setting name="mapUnderscoreToCamelCase" value="true"/>
  27. <!-- 设置本地缓存范围 session:就会有数据的共享 statement:语句范围 (这样就不会有数据的共享 ) defalut:session -->
  28. <setting name="localCacheScope" value="SESSION"/>
  29. <!-- 设置 JDBC 类型为空时,某些驱动程序 要指定值, default:OTHER,插入空值时不需要指定类型 -->
  30. <setting name="jdbcTypeForNull" value="NULL"/>
  31. </settings>
  32. <plugins>
  33. <!-- com.github.pagehelper为PageHelper类所在包名 -->
  34. <plugin interceptor="com.github.pagehelper.PageInterceptor">
  35. <!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
  36. <property name="helperDialect" value="mysql"/>
  37. <property name="reasonable" value="true"/>
  38. </plugin>
  39. </plugins>
  40. </configuration>

spring-context.xml 放置的是关于Spring的配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  5. 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
  6. <!--开启注解扫描-->
  7. <context:annotation-config />
  8. <!--不扫描Controller注解-->
  9. <context:component-scan base-package="com.qfedu">
  10. <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  11. </context:component-scan>
  12. <!-- 配置事务管理器 -->
  13. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  14. <property name="dataSource" ref="dataSource"/>
  15. </bean>
  16. <!-- 开启事务注解驱动 -->
  17. <tx:annotation-driven transaction-manager="transactionManager" />
  18. </beans>

spring-context-druid.xml  放置的是数据库连接池的配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. 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">
  5. <!-- 加载配置属性文件 -->
  6. <context:property-placeholder ignore-unresolvable="true" location="classpath:db.properties"/>
  7. <!-- 数据源配置, 使用 Druid 数据库连接池 -->
  8. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  9. <!-- 数据源驱动类可不写,Druid默认会自动根据URL识别DriverClass -->
  10. <property name="driverClassName" value="${jdbc.driverClass}"/>
  11. <!-- 基本属性 url、user、password -->
  12. <property name="url" value="${jdbc.connectionURL}"/>
  13. <property name="username" value="${jdbc.username}"/>
  14. <property name="password" value="${jdbc.password}"/>
  15. <!-- 配置初始化大小、最小、最大 -->
  16. <property name="initialSize" value="${jdbc.pool.init}"/>
  17. <property name="minIdle" value="${jdbc.pool.minIdle}"/>
  18. <property name="maxActive" value="${jdbc.pool.maxActive}"/>
  19. <!-- 配置获取连接等待超时的时间 -->
  20. <property name="maxWait" value="60000"/>
  21. <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
  22. <property name="timeBetweenEvictionRunsMillis" value="60000"/>
  23. <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
  24. <property name="minEvictableIdleTimeMillis" value="300000"/>
  25. <property name="validationQuery" value="${jdbc.testSql}"/>
  26. <property name="testWhileIdle" value="true"/>
  27. <property name="testOnBorrow" value="false"/>
  28. <property name="testOnReturn" value="false"/>
  29. <!-- 配置监控统计拦截的filters -->
  30. <property name="filters" value="stat"/>
  31. </bean>
  32. </beans>

spring-context-mybatis.xml  放置的是spring与mybatis整合的配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
  4. <!-- 配置 SqlSession -->
  5. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  6. <property name="dataSource" ref="dataSource"/>
  7. <!-- 用于配置对应实体类所在的包,多个 package 之间可以用 ',' 号分割 -->
  8. <property name="typeAliasesPackage" value="com.qfedu.entity"/>
  9. <!-- 用于配置对象关系映射配置文件所在目录 -->
  10. <property name="mapperLocations" value="classpath*:/mapper/**/*.xml"/>
  11. <property name="configLocation" value="classpath:/mybatis-config.xml"></property>
  12. </bean>
  13. <!-- 扫描 Mapper -->
  14. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  15. <property name="basePackage" value="com.qfedu.mapper" />
  16. </bean>
  17. </beans>

spring-mvc.xml  配置的Spring mvc的相关配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  7. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  8. <description>Spring MVC Configuration</description>
  9. <!-- 加载配置属性文件 -->
  10. <context:property-placeholder ignore-unresolvable="true" location="classpath:db.properties"/>
  11. <mvc:resources mapping="/static/**" location="/static/" cache-period="31536000"/>
  12. <!-- 使用 Annotation 自动注册 Bean,只扫描 @Controller -->
  13. <context:component-scan base-package="com.qfedu" use-default-filters="false">
  14. <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  15. </context:component-scan>
  16. <!-- 默认的注解映射的支持 -->
  17. <mvc:annotation-driven />
  18. <!-- 定义视图文件解析 -->
  19. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  20. <property name="prefix" value="${web.view.prefix}"/>
  21. <property name="suffix" value="${web.view.suffix}"/>
  22. </bean>
  23. <!-- 静态资源映射 -->
  24. <mvc:resources mapping="/static/**" location="/static/" cache-period="31536000"/>
  25. <!--前后端分离开发,前端访问后端时的跨域问题-->
  26. <mvc:cors>
  27. <mvc:mapping path="/**"
  28. allowed-origins="*"
  29. allowed-methods="POST, GET, OPTIONS, DELETE, PUT,PATCH"
  30. allowed-headers="Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"
  31. allow-credentials="true" />
  32. </mvc:cors>
  33. </beans>

log4j.properties   log4j日志文件的配置信息,用来打印日志

  1. log4j.rootLogger=INFO, console, file
  2. log4j.appender.console=org.apache.log4j.ConsoleAppender
  3. log4j.appender.console.layout=org.apache.log4j.PatternLayout
  4. log4j.appender.console.layout.ConversionPattern=%d %p [%c] - %m%n
  5. log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
  6. log4j.appender.file.File=logs/log.log
  7. log4j.appender.file.layout=org.apache.log4j.PatternLayout
  8. log4j.appender.A3.MaxFileSize=1024KB
  9. log4j.appender.A3.MaxBackupIndex=10
  10. log4j.appender.file.layout.ConversionPattern=%d %p [%c] - %m%n

 

原文链接:http://www.cnblogs.com/MrLiShenHong/p/11215822.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号