mybatis和spring實際最基本的操作流程
我是一名新手,在這個過程中遇到了很多問題。
最后終于整明白操作是如何的。
(一定要手敲代碼啊,別想我一樣,眼高手低。等于白學。樓主要郁悶死了,今天剛好是新春佳節(jié),外面很熱鬧。但是我菜的心涼涼的。決定無論如何都要整理出來)
我決定記錄,如何操作,實踐。。。
沒有dao類時,是如何操作數(shù)據(jù)的。
主要是用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 于是有了如下發(fā)現(xiàn)。 4 5 6 主配置文件中 7 8 <mappers> 9 <mapper resource> 10 </mappers> 11 12 13 14 <mapper namespace="如果你"> 15 < 16 17 </mapper>
spring整合mybatis
浙公網(wǎng)安備 33010602011771號