現實中數據庫沒法保證一成不變,如如有改動,測試和開發都有些許不便,改動的工做多是重複的而且手工改動也可能形成數據不一致。 在這種狀況下,使用Liquibase將有利於咱們更好地管理數據。java
<?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_3_0.xsd" id="WebApp_ID" version="3.0"> <!-- spring 配置 begin --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-context.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- spring 配置 end --> </web-app>
<!-- 引用資源文件 --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 配置數據源 dbcp --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <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> <bean id="liquibase" class="liquibase.integration.spring.SpringLiquibase"> <property name="dataSource" ref="dataSource" /> <property name="changeLog" value="classpath:Changelog.xml" /> </bean>
<?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-2.0.xsd"> <!-- 執行sql --> <changeSet id="sql_file" author="anniweiya"> <sqlFile path="anniweiya.sql" /> </changeSet> <!-- 新建表 --> <changeSet id="create_table" author="anniweiya"> <createTable tableName="t_anniweiya_test"> <column name="id" type="int"> <constraints primaryKey="true" nullable="false" /> </column> <column name="user" type="varchar(255)"> <constraints nullable="false" /> </column> <column name="updatetime" type="date"> <constraints nullable="false" /> </column> </createTable> </changeSet> <!-- 新增列 --> <changeSet id="add_column" author="anniweiya"> <addColumn tableName="t_anniweiya_test"> <column name="password" type="varchar(255)"> <constraints nullable="false" /> </column> </addColumn> </changeSet> <!-- 修改列類型 --> <changeSet id="modify_datatype" author="anniweiya"> <modifyDataType tableName="t_anniweiya_test" columnName="password" newDataType="varchar(55)" /> </changeSet> <!-- 刪除列 --> <changeSet id="drop_column" author="anniweiya"> <dropColumn tableName="t_anniweiya_test" columnName="updatetime" /> </changeSet> <!-- 修更名 --> <changeSet id="modify_name" author="anniweiya"> <renameColumn tableName="t_anniweiya_test" oldColumnName="password" newColumnName="password1" columnDataType="varchar(255)" /> </changeSet> <!-- 執行sql語句 --> <changeSet id="t_anniweiya_test_sql" author="anniweiya"> <preConditions onFail="MARK_RAN"> <not> <columnExists tableName="t_anniweiya_test" columnName="fcreator_id" /> </not> </preConditions> <sql>ALTER TABLE `t_anniweiya_test` Add COLUMN fcreator_id int not null AFTER `user` </sql> </changeSet> </databaseChangeLog>
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class UpDataBase { public static void main(String[] args) { System.out.println("start"); ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "spring-context.xml"}); System.out.println("ok"); } }