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