mybatis

1.mybatis基础知识
-
jdbc的简单封装;
-
jdbc本质是java源码与sql指令混合的编程模式,导致易出错,难调试;
-
mybatis将java代码与sql指令两分离,便于高效编程开发;
-
ibatis演化而来,商品名为mybatis
-
ORM(object mapping relationship)对象关系映射:JDBC、mybatis、hibernate、jpa
-
持久化与反持久化
2.mybatis映射文件编写规范
-
位置:放置maven项目的资源包下mapping/*.xml
-
命名:与接口名一致
-
-
每个指令对应接口的一个方法
-
查询---select
-
添加---insert
-
修改---update
-
删除---delete
-
-
每条指令的id属性值为接口方法的方法名,由于xml中每个节点的id必须唯一,意味着,该接口的方法名必须不重复(mybatis接口的方法不得重载)
-
每个指令的resultType 为方法的返回值类型或封装类型
映射注册到配置文件中
映射文件内容应用关系
mybatis-config.xml:
<!-- 加载数据库配置参数-->
<properties resource="db.properties">
<!-- 将映射文件注册到数据库配置中-->
<mapper resource="" />
接口名.xml:
sql片段:(使sql语句中不带*)
<sql id="自己起与select中相等">
字段名
</sql>
例:
<sql id="course_colums">
cno,
cname,
tno
</sql>
<select id="findCourses" resultType="com.qf.j2112.po.Course">
select
<include refid="course_colums"></include>
from course
</select>
位置:
resources/mapping/
第一种:没有map映射
<mapper namespace="接口全类名">
<insert>增 语句</insert>
<delete>删 语句</delete>
<select id="方法名" resultType="返回类型" parameterType="参数类型">增 语句</select>
注:增删改除了resultType没有 余下相同
<update>改 语句</update>
</mapper>
第二种:resultMap映射
<mapper namespace="接口全类名">
//映射
<resultMap id="自己起名列 teacherMap" type="主表的全类名">
<id property="主表主键名" colum="大写"></id>
<result property="字段名" colum="大写"/>
//关联
<association property="关联的表名" column="关联表的外键" javaType="关联表全类名">
<id property="关联表主键名" colum="大写"></id>
<result property="字段名" colum="大写"/>
</association>
注:如果是集合的话association改为collection
</resultMap>
<sql>
sql片段
</sql>
<select>
查询语句
</select>
</mapper>

浙公网安备 33010602011771号