Mybatis和hibernate的入门级区别

学完SSH,今天开始学mybatis,与hibernate对比着来看,区别还是很大的。
1. 首先,mybatis不需要对象-关系映射文件(xxx.hbm.xml),取而代之的是xxxMapper.xml文件(只是默认这么命名,不一定非要这么命名),
在这个文件里面,在没有涉及到一对多,多对多等关系时,无需写对象关系映射。

2.相比hibernate的session.createQuery(sql)这种方法,mybatis可以把sql语句写在xxxMapper.xml文件中,具体写法如下:

<select id=“selectStudent” resultType=“com.mybatis.model.Student”>
select * from dq1401 where id = #{id}
</select>

其中:
id:唯一标识
resultType:返回值类型
#{id}:从传递过来的参数中获取ID值
然后可以在dao中通过

session.selectOne(“com.mybatis.StudentMapper.selectStudent”,1);

来调用

猜你喜欢

转载自my.oschina.net/u/3420367/blog/907703