好的,谢谢老师
















在 2023-07-26 21:04:20,"Jiabao Sun" <jiabao....@xtransfer.cn.INVALID> 写道:
>SqlSession 需要关闭,建议使用 SqlSessionManager,可以不用手动关闭 SqlSession。
>
>
>On 2023/07/18 02:13:16 lxk wrote:
>> 在flink内需要使用mybatis做些简化查询的工作,目前我的使用方式如下
>> 
>> public class MybatisUtil {
>> 
>> private static final Logger LOGGER = 
>> LogFactory.createNewLogger("MybatisUtil");
>>     private static ThreadLocal<SqlSession> tl = new 
>> ThreadLocal<SqlSession>();
>>     private static SqlSessionFactory factory = null;
>> //    private static  SqlSession sqlSession = null;
>> static {
>> // 1 读取配置文件 config.xml
>> InputStream in = null;
>>         try {
>> in = Resources.getResourceAsStream("batis.xml");
>> } catch (IOException e) {
>> // TODO Auto-generated catch block
>> e.printStackTrace();
>>             throw new RuntimeException(e);
>> }
>> // 2 创建SqlSessionFactory
>> factory = new SqlSessionFactoryBuilder().build(in);
>> }
>> 
>> 
>> 
>> public static SqlSession getSqlSession(){
>> SqlSession sqlSession = tl.get();
>>         if(sqlSession == null){
>> sqlSession = factory.openSession();
>> tl.set(sqlSession);
>> LOGGER.info("sqlSession创建成功,连接为:{},时间为:{}", sqlSession,LocalTimeUtil.now());
>> }
>> return sqlSession;
>> }
>> 
>> 
>> }
>> 以上是工具类
>> 我在open方法中获取sqlsession,然后在invoke方法中使用mapper
>> public void open(Configuration parameters) throws Exception {
>> sqlSession = MybatisUtil.getSqlSession();
>> }
>> 
>> public List<SaleSource> map(HeaderFullWithPreOrder headerFullWithPreOrder) 
>> throws Exception {
>> SelectAddCartMapper mapper = sqlSession.getMapper(SelectAddCartMapper.class);
>> .......其他省略....
>> }
>> 
>> 想问下这种方式使用是否正确。以及sqlsession是否需要关闭,看见相关帖子有说如果sqlsession不关闭的话会把连接打满
>> 

回复