栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java执行Sql脚本

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java执行Sql脚本

在工作中我们需要实现java程序执行sql脚本的场景,spring提供了一个很方便的工具类ScriptUtils配合dataSource(import javax.sql.DataSource;)给大家使用。具体代码如下:

@Slf4j
@Service("dataService")
public class DataServiceImpl extends ServiceImpl implements DataService {

    @Autowired
    private DataMapper dataMapper;

    @Autowired
    private DataSource dataSource;


    @Override
    public BaseResult data() {
        DataEntity entity = dataMapper.selectById(1);
        if (ObjectUtil.isNull(entity)) {
            return BaseResult.succeed(1);
        } else {
            if (entity.getDc() == 1) {
                return BaseResult.succeed(1);
            }
        }
        return BaseResult.succeed(0);
    }

    @Override
    public void dcOne() {
        try {
            PathResource resource = new PathResource("D:\data\movedata.sql");
            ScriptUtils.executeSqlScript(dataSource.getConnection(), resource);
        } catch (Exception e) {
            log.error("========执行数据迁移时出错", e);
            if (!check(1)) {
                DataEntity entity = new DataEntity();
                entity.setDc(0);
                dataMapper.insert(entity);
            }
        }
        if (!check(1)) {
            DataEntity entity = new DataEntity();
            entity.setDc(1);
            dataMapper.insert(entity);
        }
    }

    public boolean check(Integer id) {
        DataEntity entity = dataMapper.selectById(id);
        if (ObjectUtil.isNull(entity)) {
            return false;
        } else {
            return true;
        }
    }
}

`

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/843018.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号