发表于: 2018-01-11 23:41:09

1 586


今天主要用mybatis和spring搭建mawen项目,以前可以搭得好的,现在不知怎么老打不好,重建了四五个mawen项目,最后才成功了。

1.重建第一个:

找不到类符号

2.第二次错误很奇怪,爆红,但是按住ALT键+enter也没反应,不能解决


3.第三次才做好了一个很简单的,并不是Spring和mybatis搭建,只是mybatis的基础

尝试解释一下:resource="com/how2java/pojo/Category.xml"代表映射Category.xml。

Category.xml的内容是:

<mapper namespace="com.how2java.pojo">
   <select id="listCategory" resultType="Category">
       select * from category_
</select>
</mapper>

命名空间是com.how2java.pojo,

将来执行的sql语句select * from category_.

id: listCategory 。resultType="Category" 表示返回的数据和Category关联起来

Category类就不说了。

测试TestMybatis里面

String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session=sqlSessionFactory.openSession();

List<Category> cs=session.selectList("listCategory");
for (Category c : cs) {
System.out.println(c.getName());

前面三句话代表,根据配置文件:mybatis-config得到sqlSessionFactory。再根据sqlSessionFactory得到session

最后通过session的selectList方法,调用sql语句listCategory。listCategory这个就是在配置文件Category.xml中那条sql语句设置的id。

执行完毕之后,得到一个Category集合


4. 应用程序找Mybatis要数据

 mbatis从数据库中找来数据

 通过mybatis-config.xml 定位哪个数据库

 通过Category.xml执行对应的select语句

 Catgory.xml把返回的数据库记录封装在Category对象中

把多个Category对象装在一个Category集合中

返回一个Category集合


明天的计划:搭好mybatis+spring,结尾任务一

遇到的问题:好多啊

今天的收获:mybatis的运行原理,有点了解了

java任务一开始时间:2017.12.05

预计demo时间:2018.01-05

可能有延期风险,原因是:已经延期了,基础比较差,

禅道链接地址:http://task.ptteng.com/zentao/project-task-501.html



返回列表 返回列表
评论

    分享到