在项目中使用Liquibase

来源:互联网 发布:windows重启网络服务 编辑:程序博客网 时间:2024/05/17 02:25

在项目中使用liquibase ,个人感觉liquibase的作用就是写好sql或者创表语句然后来更新数据库做法如下:

liquibase和spring结合的:

首先applicationContext.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"       xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd">    <context:property-placeholder location="classpath:*.properties"/>    <!-- 配置数据源 :此处使用Druid连接池 -->    <bean id="pvaDataSource" class="com.alibaba.druid.pool.DruidDataSource"          init-method="init" destroy-method="close">        <property name="url" value="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF8"/>        <property name="username" value="root"/>        <property name="password" value="123456"/>        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>        <!-- 配置初始化大小、最小、最大 -->        <property name="initialSize" value="5"/>        <!-- 连接池中最少空闲maxIdle个连接 -->        <property name="minIdle" value="5"/>        <!-- 连接池激活的最大数据库连接总数。设为0表示无限制 -->        <property name="maxActive" value="100"/>        <!-- 最大建立连接等待时间,单位为 ms,如果超过此时间将接到异常。设为-1表示无限制 -->        <property name="maxWait" value="60000"/>        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->        <property name="timeBetweenEvictionRunsMillis" value="60000"/>        <!-- 配置连接池中连接可空闲的时间(针对连接池中的连接对象.空闲超过这个时间则断开,直到连接池中的连接数到minIdle为止),单位是毫秒 -->        <property name="minEvictableIdleTimeMillis" value="300000"/>        <!-- 用来检测连接是否有效的sql,要求是一个查询语句 -->        <property name="validationQuery" value="SELECT 'x' FROM DUAL"/>        <!-- 建议配置为true,不影响性能,并且保证安全性 -->        <property name="testWhileIdle" value="true"/>        <!-- 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能 -->        <property name="testOnBorrow" value="false"/>        <!-- 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能 -->        <property name="testOnReturn" value="false"/>        <!-- 打开PSCache,并且指定每个连接上PSCache的大小(Oracle或mysql5.5及以上使用) -->        <property name="poolPreparedStatements" value="true"/>        <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>        <!-- 配置监控统计拦截的filters -->        <property name="filters" value="stat"/>        <!-- 配置关闭长时间不使用的连接 -->        <!-- 是否清理removeAbandonedTimeout秒没有使用的活动连接,清理后并没有放回连接池(针对未被close的活动连接) -->        <property name="removeAbandoned" value="true"/>        <!-- 活动连接的最大空闲时间,1800秒,也就是30分钟 -->        <property name="removeAbandonedTimeout" value="1800"/>        <!-- 连接池收回空闲的活动连接时是否打印消息 -->        <property name="logAbandoned" value="true"/>    </bean>    <bean id="pvaLiquibase" class="liquibase.integration.spring.SpringLiquibase">        <property name="dataSource" ref="pvaDataSource"/>        <property name="changeLog" value="classpath:test.xml"/>        <property name="contexts" value="test, production"/>    </bean></beans>

然后在写test.xml 

<?xml version="1.0" encoding="UTF-8"?><databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                   xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">    <changeSet id="2014070209" author="tianying" runOnChange="true">        <comment>            调换系统配置以及运维管理的位置        </comment>             <createTable tableName="useraccount">             <column name="djk" type="varchar(255)"></column>         </createTable>         </changeSet></databaseChangeLog>


0 0
原创粉丝点击