Mybatis用法

来源:互联网 发布:windows操作系统界面 编辑:程序博客网 时间:2024/05/21 19:23

看到一篇关于Mybatis的博文,http://blog.csdn.net/fuxingdaima/article/details/10527399有详细的用法介绍。

现在做的项目中,用到了Mybatis框架。刚一开始,并不懂如何使用,都是另一个哥们带着我做到了现在。上午的时候被问到Mybatis的原理,竟无言以对……所以,下午翻看了下Mybatis的介绍,感觉还是只知道些基本用法,但对于实现原理,还是无法理解。趁现在这个机会,就具体的说一下我的认识。

Mybatis是一个基于Java的持久层框架,包括两个部分:一是SQL maps;二是DAO。

Mybatis支持普通的SQL查询和高级映射。Mybatis消除了所有的JDBC代码和参数设置,统一的用xml进行配置管理,即Mybatis的配置文件,包含了

①给POJO类的长路径起个别名;

②配置数据库的用户名和密码等参数;

③指明关系表的配置文件(即POJO对应的xml文件)

等内容。

综上,Mybatis应该包括以下几块内容:

1、Mybatis的配置文件mybatis-config.xml文件;

2、对应的POJO类;

3、和POJO类对应的xml关系表配置文件(包含有对该POJO类在数据库中对应记录的增/删/改/查操作等);

4、Mybatis应用程序中需要用到sqlSessionFactory实例,所避免重复创建,可封装一个sqlSessionFactory实例。

5、和3中的xml配置文件对应的接口;

其中,在最后一块内容中,接口也就相当于DAO层了,通过将接口实例化,也就得到了一个POJO的实例,可直接提供给上面的service层。

对于以上的几点说明,可参见最上面的链接看一下博文中举的实例,还是很容易理解的。另外,在我们的项目中,稍微有点儿不同的,在于接口的变化。

下图是项目的结构图:

用DAO层取代了接口,相应的,获取结果集的方式也有了变化,下面给出在DAO中得到结果集的Java代码:

插入样例:

<span style="white-space:pre"></span>public void addParticipants(Patent patent, List<Integer> list) {SqlSession session = SqlSessionUtil.getSqlSession();try {for (Integer id : list) {Map<String, Object> params = new HashMap<String, Object>();params.put("patentId", patent.getPatentId());params.put("id", id);session.insert("insertPatentParticipant", params);}session.commit();} catch (Exception e) {session.rollback();e.printStackTrace();<span style="white-space:pre"></span>}
查询样例:
public List<Patent> getPatentByProj(Integer id) {SqlSession session = SqlSessionUtil.getSqlSession();List<Patent> list = session.selectList("selectPatentByProjId", id);session.close();return list;}
多对一、一对多关联的处理:

1、多对一:association

例如,学生和班级的关系就是多个学生对应一个班级的关系。这时候在学生的xml文件中,要用association进行关联。

<!--StudentMapper.xml--->  <?xml version="1.0" encoding="gbk"?>  <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"    "mybatis-3-mapper.dtd">  <mapper namespace="org.phf.mapping.StudentMapper">      <!--开启缓存-->    <cache />       <!--resultMap标签将column属性(数据库表中的列名) 映射到 property属性(Student对象中的字段)-->    <resultMap id="clazzResult" type="Clazz">      <id column="id" property="id"/>      <result column="code" property="code"/>      </resultMap>       <resultMap id="studentResult" type="Student">      <id column="id" property="id"/>      <result column="name" property="name"/>      <result column="sex" property="sex"/>      <result column="age" property="age"/>      <!--<association>标签体现的是多对一关联关系,-->      <association property="clazz" javaType="Clazz" resultMap="clazzResult"/>      </resultMap>        <select id="find" parameterType="Integer" resultMap="studentResult">      SELECT * FROM tb_student s,tb_clazz c      WHERE s.clazz_id=c.id AND s.id = #{id}    </select>  </mapper>  

2、一对多:collection

<!---ClazzMapper.xml---->  <?xml version="1.0" encoding="gbk"?>  <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"    "mybatis-3-mapper.dtd">  <mapper namespace="org.phf.mapping.ClazzMapper">      <!--开启缓存-->    <cache />          <resultMap id="clazzResult" type="Clazz">          <id column="id" property="id"/>          <result column="code" property="code"/>          <collection property="studentList" ofType="Student"               column="id" select="findStudentById"/>      </resultMap>        <resultMap id="studentResult" type="Student">          <association property="clazz" javaType="Clazz" column="class_id" select="find"/>      </resultMap>        <select id="findStudentById" parameterType="Integer" resultMap="studentResult">          select * from tb_student where clazz_id=#{class_id}      </select>            <select id="find" parameterType="Integer" resultMap="clazzResult">          select * from tb_clazz where id=#{id}       </select>  </mapper>  


0 0
原创粉丝点击