码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-09-28 19:19:38      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

一、工程目录

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

 

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

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
 */
@Service
public 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)

标签:

原文地址:http://www.cnblogs.com/ncjava/p/5917391.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!