zoukankan      html  css  js  c++  java
  • 【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
     */
    @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

  • 相关阅读:
    设计模式:简单工厂模式
    datav轮播表使用事例
    POI操作Excel常用方法总结 .
    序列图像三维重建 学习过程流水账
    python面向对象编程
    python批量生成word文档
    Linux 网络配置方法 nmtui 配置
    leetcode 剑指 Offer 67. 把字符串转换成整数 & leetcode 8. 字符串转换整数 (atoi)
    leetcode 剑指 Offer 59
    leetcode 剑指 Offer 53
  • 原文地址:https://www.cnblogs.com/ncjava/p/5917391.html
Copyright © 2011-2022 走看看