首页 > 数据库 >SpringBoot-使用链接字符串动态创建SqlSessionFactory执行任意SQL脚本

SpringBoot-使用链接字符串动态创建SqlSessionFactory执行任意SQL脚本

时间:2023-02-25 11:04:58浏览次数:34  
标签:mapper SqlSessionFactory SpringBoot SQL GeneralMapper sqlSessionFactory sql Stri


SpringBoot-使用链接字符串动态创建SqlSessionFactory执行任意SQL脚本

引言

SpringBoot大大减少了使用XML配置的复杂性,但是想通过代码去实例化一个对象有点儿无从下手的感觉。

SpringMVC中通过XML的配置和层级关系,大致能翻译为代码去构建一个对象。

SqlSessionFactory的创建也是参考了XML的配置去构建的。

环境:SpringBoot 1.56 + Pgsql 14

目标

使用链接字符串动态创建SqlSessionFactory对象,通过mapper执行任意SQL脚本;

SqlSessionFactory的一般用法

更简单的调用可以直接注入一个mapper对象,但是隐藏了我们关注的SqlSessionFactiory对象;

mapper类和对应的xml是必不可少的组件;

要实现执行动态SQL,就要建一个通用的mapper和对应的xml文件,xml中的占位符要用${xxx}

${} 是非参数化占位符,有SQL注入风险;#{} 参数化占位符;

1.注入一个对象
@Autowired
SqlSessionFactory sqlSessionFactory;
2.实例化SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
3.实例化Mapper对象
GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class);
4.执行mapper中的方法
int n = mapper.xxxxx();

实例化SqlSessionFactory对象

封装一个getSqlSessionFactory()方法,可以动态的将链接参数传入支持多数据源切换;

private SqlSessionFactory getSqlSessionFactory(){
String driver = "org.postgresql.Driver";
//A6库
String connStr = "jdbc:postgresql://xxxxxxx:5432/CRP?currentSchema=mySchema";
String uname = "pguser";
String upwd = "123456";
//创建连接池
DataSource dataSource = new PooledDataSource(driver, connStr ,uname, upwd);
//事务控制(配置:隔离级别、自动提交等)
TransactionFactory transactionFactory = new JdbcTransactionFactory();
//环境配置(一些查询特性)
Environment environment = new Environment("development", transactionFactory, dataSource);
//创建配置
Configuration configuration = new Configuration(environment);
configuration.setMapUnderscoreToCamelCase(true);//开启驼峰规则
configuration.setCallSettersOnNulls(true);//规定查询数据为空是则返回null
//加入资源(Mapper接口)
configuration.addMapper(GeneralMapper.class);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
return sqlSessionFactory;
}

通用Mapper接口

@Repository
public interface GeneralMapper {

List<Map<String,Object>> select(@Param("sql") String strSql);

void insert(@Param("sql") String strSql);

void delete(@Param("sql") String strSql);

int update(@Param("sql") String strSql);
}

通用XML文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="my.GeneralMapper">
<select id="select" resultType="Map" statementType="STATEMENT">
${sql}
</select>
<insert id="insert" parameterType="java.lang.String">
${sql}
</insert>

<delete id="delete" parameterType="java.lang.String">
${sql}
</delete>
<update id="update" parameterType="java.lang.String">
${sql}
</update>
</mapper>

调用方式

SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
GeneralMapper mapper = sqlSession.getMapper(GeneralMapper.class);
//注意通用查询返回的是一个map对象集,map.value必须是object类型
List<Map<String, Object>> dataLst = mapper.select(querySql);


标签:mapper,SqlSessionFactory,SpringBoot,SQL,GeneralMapper,sqlSessionFactory,sql,Stri
From: https://blog.51cto.com/xxjjing/6084947

相关文章

  • MySQL 常问
    MySQL什么是索引?索引是帮助MySQL高效获取数据的排好序的数据结构索引的原理:就是把无需的数据编程有序的查询1、把创建了索引的列的内容进行排序2、把排序结构生成......
  • sql 2012 误删恢复
    一、通过消费和充值记录确定误删时间 R2012误删数据恢复过程大侠酷裤马路 2022-10-25 原文由于长时间从事企业应用系统开发,前往用户现场升级......
  • mysql增量备份脚本
    2、增量备份2.1、添加备份脚本[root@localhost]#vim/mnt/data/backup/mysql/mysql_m_bak_diff.sh#!/bin/bash#mysql增量备份time=`date+%Y%m%d`now=`date+%F'......
  • T-SQL——将字符串转为单列
    目录0.背景1.使用STRING_SPLIT函数2.自定义分裂函数3.使用示例shanzm-2023年2月22日0.背景代码中执行存储过程,参数是多个且不确定数量,期望SQL查询时使用该参数作......
  • 利用Github Action自动化部署SpringBoot项目
    环境准备一台拥有公共IP的1核1G以上配置的Linux云服务器。笔者选用的Linux发行版为CentOSLinuxrelease7.0.1406(Core)已安装好JDK8与Maven。若是还未安装,可以参考以......
  • Mysql
    底层架构   存储引擎1、InnoDB存储引擎InnoDB是MySQL的默认事务型引擎,它被设计用来处理大量的短期(short-lived)事务。除非有非常特别的原因需要使用其他的存储引......
  • 关于SQLsever2012报错的一些经验总结
    问题描述:数据库连接实例时出现报错情况;问题截图:  故障软件:SQLsever2012操作系统:windowssever2022R2数据中心期望结果:可以打开之前的实例 总结经验: 上面这......
  • Mysql数据库的表结构
    【INFORMATION_SCHEMA数据库】 是MySQL自带的,它提供了访问数据库 元数据 的方式,元数据:数据库名或表名,列的数据类型,或访问权限等。在MySQL中,把【INFORMATION_SCHEMA】......
  • docker部署mysql
    搜索mysql镜像dockersearchmysql拉取mysql镜像dockerpullmysql:5.7创建容器,设置端口映射、目录映射mkdir~/mysqlcd~/mysqldockerrun-id\-p330......
  • MYSQL的存储引擎以及系统数据库
    今天分享的是mysql的存储引擎,以及mysql数据库中相关配置状态和相关的变量存储引擎MyISAM存储引擎MyISAM引擎特点不支持事务表级锁定读写相互阻塞,写入不能读,读时不能写只缓......