• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
删库然后跑路
博客园    首页    新随笔    联系   管理    订阅  订阅

mybatis和spring实际最基本的操作流程

我是一名新手,在这个过程中遇到了很多问题。

最后终于整明白操作是如何的。

(一定要手敲代码啊,别想我一样,眼高手低。等于白学。楼主要郁闷死了,今天刚好是新春佳节,外面很热闹。但是我菜的心凉凉的。决定无论如何都要整理出来)

 

我决定记录,如何操作,实践。。。

 

 

 

没有dao类时,是如何操作数据的。

主要是用sqlsession的selectList,等他自己提供的方法传入映射文件的namepace+sql语句的id

有很多重复劳动

String config="mybatis.xml";
InputStream in=Resources.getResourceAsStream(config);
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
SqlSessionFactory factory=builder.build(in);
SqlSession sqlSession=factory.openSession();
​
String Id="com.bj.dao.StudentDao.selectStudents"
List<Student>students=sqlSession.selectList();
for(Student student:students){
    sout(student);
}

 

有dao类时

String config="mybatis.xml";
InputStream in=Resource.getResourceAsStrean(config);
SqlSessionFactroyBuild sqlSessionFactroyBuild=new SqlSessionFactoryBuild();
SqlSessionFactory sqlSessionFactory=sqlSessionFactoryBuild.build(in);
SqlSession sqlSession=sqlSessionFactory.open();
​
​
StudentDao studentDao=sqlSession.getMapper(StudentDao.class);
List<Student>students=studentDao.selectStudent();
for(Student student:students){
    sout(student);
}

 

 1 我来说一下楼主实验出来的机制:
 2     我学的是某b站教程,他叫我们吧映射文件和dao接口放一起,我不明白其中机制。
 3      于是有了如下发现。
 4      
 5 
 6     主配置文件中
 7 
 8 <mappers>
 9 <mapper resource>
10 </mappers>
11 
12 
13 
14 <mapper namespace="如果你">
15     <
16     
17 </mapper>

 

spring整合mybatis

 

posted @ 2024-05-06 01:22  删库然后跑路  阅读(19)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3