mysql config.xml_mybatis-配置文件mybatis-config.xml

在mybatis-config.xml中有初始的配置:

从这可以看出,environments主要用于配置数据库相关,而且可以在里面配置多个environment。

因为有这些场景:

1)为了开发设置不同的数据库配置

2)测试和生产环境数据库不同

3)有多个数据库却共享相同的模式,即对不同的数据库使用相同的SQL映射

我们可以配置几个数据库配置,我们可以这样:

用default指定默认的数据库链接:(这里默认oracle)

我们每个数据库,对应一个SqlSessionFactory,可以明确的获取哪一个数据库的SqlSessionFactory。

根据数据库环境,获取SqlSessionFactory:

SqlSessionFactory factory = sqlSessionFactoryBuilder.build(reader, environment);

SqlSessionFactory factory = sqlSessionFactoryBuilder.build(reader, environment,properties);

我们的每个数据库信息都定义在environment中,我们看下这下面的配置:

1.transactionManager

示例配置:

type取值范围:

JDBC:简单的使用JDBC的提交和回滚设置,一览与从数据员得到的链接来管理事务范围

MANAGED:这个配置几乎什么都没做,它从来不提交或回滚一个链接,而它让容器来管理事务的整个生命周期(比如spring、jee应用服务器的上下文)

在默认情况下,MANAGED会关闭连接,如果有时候不希望这样时,可以从连接中停止它,将claseConnection属性设置为false:

2.dataSource

用来配置基本的JDBC数据源连接信息

示例配置:

type取值范围:

UNPOOLED:这个数据源的实现是每次被请求时打开和关闭连接。速度会有一些慢,适用于简单的应用程序。

这种类型的数据源只需要配置下面的6种属性(最后一项为可选):

driver

JDBC驱动名

url

JDBC URL地址

username

数据库用户名

password

数据库密码

defaultTransactionIsolationLevel

默认的链接事务隔离级别

driver.encoding

utf-8(可选项)

POOLED:这是JDBC链接对象的数据源连接池的实现,用来避免创建新的链接实例时必要的连接和认证时间。适用于当前Web应用程序用来快速响应请求

这种类型的数据源除了需要配置UNPOOLED中的基础配置外,还可以配置下面的内容:

poolMaximumActiveConnections

在任意时间正在使用链接的数量

poolMaximumIdleConnections

任意时间存在的空闲连接数,经验值建议设置在与poolMaximumActiveConnections相同即可

poolMaximumCheckoutTime

获取链接时如果没有idleConnection同时activeConnection达到最大值,则从activeConnections列表第一个链接开始,检查是否超过该设置的时间,如果超过,则被强制失效,返回链接。默认值为20000毫秒,建议设置在预期最大的SQL执行时间。

poolTimeToWait

给连接池一个打印日志状态机会的低层次设置,还有重新尝试获取连接,这些情况往往会需要很长时间。为了避免连接池没有配置时静默失败。默认值20000毫秒,建议默认设置。

poolPingQuery

发送到数据的侦测查询,用来验证连接是否正常工作,并且准备接受请求。默认为“NO PING QUERY SET”,这会引起许多数据库驱动连接由一个错误信息而导致失败,建议使用select 1,开销小

poolPingEnabled

这是开启或禁用侦测查询,如果开启,必须用一个合法的SQL语句,设置poolPingQuery属性,默认值为false,建议启用,防止服务器端异常关闭,导致客户端错误。

poolPingConnectionsNotUsedFor

用来配置poolPingQuery多长时间被调用一次。可以被设置匹配标准的数据库链接超时时间,来避免不必要的侦测。默认值0(也就是所有链接每一时刻都被侦测到,但仅仅当poolPingEnabled为true时适用)。建议小于服务器端超时时间,MySQL默认超时是8小时。

JNDI:这个数据源是为了使用如Spring或应用服务器这类的容器,容器可以集中或在外部配置数据源,然后设置JNDI上下文的引用。

这个数据源只需要配置两个属性:

initial_context

用来从初始上下文中寻找环境(也就是initialContext.lookup(initial——context)),这是个可选属性,如果被忽略,那么data_source属性将直接以initialContext为背景再次寻找

data_source

这是引用数据源实例位置的上下文的路径,它会以initial_context查询返回的环境为背景来查找,如果initial_context没有返回结果时,直接以初始上下文为环境来查找。

和其他数据源配置类似,他可以通过名“env.”的前缀来直接向初始上下文发送属性,比如:

env.encoding=UTF8

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. `web.xml`: `web.xml` 是 Java Web 应用程序的配置文件,用于配置 Servlet、Filter、Listener 等 Web 组件。主要功能如下: - 配置 Servlet 和 Servlet 映射。 - 配置 Filter 和 Filter 映射。 - 配置 Listener。 - 配置错误页面。 - 配置 Session 超时时间。 - 配置上下文参数等。 示例代码: ```xml <?xml version="1.0" encoding="UTF-8"?> <web-app 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_3_0.xsd" version="3.0"> <display-name>MyWebApp</display-name> <servlet> <servlet-name>myServlet</servlet-name> <servlet-class>com.example.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>myServlet</servlet-name> <url-pattern>/myservlet/*</url-pattern> </servlet-mapping> <error-page> <error-code>404</error-code> <location>/error.jsp</location> </error-page> </web-app> ``` 2. `springmvc-config.xml`: `springmvc-config.xml` 是 Spring MVC 框架的配置文件,用于配置 Spring MVC 相关的组件,如视图解析器、拦截器等。主要功能如下: - 配置 Spring MVC 的 HandlerMapping 和 HandlerAdapter。 - 配置视图解析器。 - 配置拦截器。 - 配置文件上传解析器等。 示例代码: ```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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <mvc:annotation-driven/> <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <mvc:interceptors> <bean class="com.example.MyInterceptor"/> </mvc:interceptors> </beans> ``` 3. `spring-mybatis.xml`: `spring-mybatis.xml` 是 Spring 整合 MyBatis 框架的配置文件,用于配置 MyBatis 相关的组件,如数据源、SqlSessionFactory、MapperScannerConfigurer 等。主要功能如下: - 配置数据源。 - 配置 SqlSessionFactory。 - 配置 MapperScannerConfigurer。 - 配置事务管理器等。 示例代码: ```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: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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <context:component-scan base-package="com.example"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mydb"/> <property name="user" value="root"/> <property name="password" value="123456"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.mapper"/> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:annotation-driven/> </beans> ``` 4. `applicationContext.xml`: `applicationContext.xml` 是 Spring 的核心配置文件,用于配置 Spring 容器中的 Bean。主要功能如下: - 配置数据源、事务管理器等基础组件。 - 配置 Service、DAO 等业务组件。 - 配置 AOP、声明式事务等。 - 配置定时任务等。 示例代码: ```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: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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <context:component-scan base-package="com.example"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mydb"/> <property name="user" value="root"/> <property name="password" value="123456"/> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:annotation-driven/> <bean id="userService" class="com.example.UserService"> <property name="userDao" ref="userDao"/> </bean> <bean id="userDao" class="com.example.UserDao"> <property name="dataSource" ref="dataSource"/> </bean> </beans> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值