mybatis中sqlsessiontemplate的使用

 时间:2026-02-14 20:48:58

1、sqlsessiontemplate在mybatis中已经很少去使用了,他破坏了面向对象以及面向接口编程的理念,这里简单介绍使用,防止看到的时候不了解

mybatis中sqlsessiontemplate的使用

2、首先配置,结合spring配置。

<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">

<constructor-arg ref="sqlSessionFactory" />

</bean>

mybatis中sqlsessiontemplate的使用

3、<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

    <property name="dataSource" ref="dataSource" />

        <property name="configLocation" value="classpath:mybatis1/mybatis-config.xml"></property>

      

mybatis中sqlsessiontemplate的使用

4、  <!-- mapper扫描 -->

        <property name="mapperLocations" value="classpath:mybatis1/*/*.xml"></property>

    </bean>

还有一些基本的配置没有展示;

mybatis中sqlsessiontemplate的使用

5、sqlSessionTemplate.insert(str, obj);

插入对象

sqlSessionTemplate.update(str, obj);

更新对象

sqlSessionTemplate.delete(str, objs);

删除对象

sqlSessionTemplate.selectOne(str, obj);

mybatis中sqlsessiontemplate的使用

6、编写接口,

sqlSessionTemplate.selectOne(str, obj);

引用接口

public PageData getUserByNameAndPwd(PageData pd)throws Exception{

return (PageData)dao.findForObject("UserMapper.getUserInfo", pd);

}

mybatis中sqlsessiontemplate的使用

7、sqlsessiontemplate在每次操作数据库时都会创建sqlsession对象,相对比较安全,但是sqlsession比较浪费资源,在后面的mybatis都使用的单例模式。创建sqlsession,节约资源

mybatis中sqlsessiontemplate的使用

  • 征途2迷踪林怎么去
  • 贪婪洞窟2冰法加点推荐,冰法玩法介绍
  • 和田玉山流水与戈壁料的区别
  • 火影忍者如何参加新手活动
  • 秋葵黄瓜炒肉片的做法
  • 热门搜索
    怎么辨别耐克鞋真假 牛奶面膜怎么做 我的世界怎么换材质包 黑雾之源怎么打 用电脑怎么赚钱 手机照片误删怎么恢复 喉咙发炎怎么好得快 小凤仙浮漂怎么样 怎么拍视频 怎么删除聊天记录