首页 > 代码库 > 【MyBatis系列】:SpringMvc+MyBatis之初始化建表(01)

【MyBatis系列】:SpringMvc+MyBatis之初始化建表(01)

一、工程目录

技术分享技术分享技术分享

 

二、定义系统初始化核心类

1.com.system.utils.service.SystemInitService

package com.system.utils.service;import javax.servlet.ServletContext;import org.apache.commons.lang3.StringUtils;import org.apache.log4j.Logger;import org.simpleframework.xml.Serializer;import org.simpleframework.xml.core.Persister;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.Resource;import org.springframework.stereotype.Service;import org.springframework.web.context.WebApplicationContext;import org.springframework.web.context.support.WebApplicationContextUtils;import com.system.utils.bean.DataBaseInit;/** * 系统初始化 * @author Carl * @date 2016-09-13 */@Servicepublic abstract class SystemInitService {        private static final Logger log = Logger.getLogger(SystemInitService.class);        @Autowired    private IDataBaseInitService dataBaseInitService;        /**     * 获取初始化接口     * @param beanName      * @param context     */    public final static void initialization(final String beanName, final ServletContext context){        try {            final WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);            final SystemInitService initService = (SystemInitService) applicationContext.getBean(beanName, SystemInitService.class);            initService.init(context);        } catch (Exception e) {            log.error(e.getMessage(),e);        }    }            /**     * 系统初始化数据     *      */    public abstract void initData();        /**     * 资源路径     * @return     */    public abstract String resourcePath();    /**     * 初始化数据库     *      * @author Carl     */    private void initDatabase() {        try {            if (StringUtils.isEmpty(resourcePath())) {                return;            }            Resource resource = new ClassPathResource(resourcePath());            final Serializer  serializer = new Persister();            DataBaseInit dataBaseInit = serializer.read(DataBaseInit.class, resource.getInputStream());            dataBaseInitService.initDatabase(dataBaseInit);        } catch (Exception e) {            log.error(e.getMessage(), e);        }    }    /**     * 初始化模板     * @param context     */    private void init(final ServletContext context) {        System.out.println("========================系统开始初始化=======================================");        initDatabase();        initData();        System.out.println("========================系统初始化结束=======================================");    }}

SystemInitService实现类 com.nc.rms.service.init.CmsSystemInitServiceImpl

package com.nc.rms.service.init;import org.springframework.stereotype.Service;import com.system.utils.service.SystemInitService;@Service("cmsSystemInitService")public class CmsSystemInitServiceImpl extends SystemInitService{            @Override    public void initData() {        System.out.println("初始化进行中");    }    @Override    public String resourcePath() {        return "sql/DatabaseInit.xml";    }}

DatabaseInit.xml  数据库建表语句存放路径 create初始化建表语句,update修改数据库表

<?xml version="1.0" encoding="UTF-8"?><databaseInit initCode="rms.table.version">        <create>        <sqlPath>sql/rms/create_table_mysql.sql</sqlPath>        <!-- <procPath></procPath> -->    </create>    <update updateVersion="1.1">        <sqlPath>sql/rms/update_table_1.1.sql</sqlPath>    </update>     </databaseInit>

 太多了不想写了,有喜欢的朋友可以直接看源码。

http://pan.baidu.com/s/1jHAcjTO

【MyBatis系列】:SpringMvc+MyBatis之初始化建表(01)