别走…………………………
常问的几个问题!
-
什么是SQL映射文件!
mybatis 提供的一种特定的文件,主要思想是将SQL语句中程序中分离出来
(简单说) :Impl实现类换成了一种特定的文件 不需要写实现类,但是要在特定的文件中写上增删改查语句 -
MyBatis如何实现SQL映射文件中的条件查询
根据该方法的参数进行条件判断,在判断这个方法的参数类型,在指定的增删改查标签中的"parameterType属性上"上填写指定类型
-
MyBatis框架的结果映射通过什么标签
通过resultMap标签实现结果映射,主要目的:将数据库字段与javaBean的属性相对应
-
MyBatis中增删改使用什么标签
insert delete update
以上就是基本的SQL映射详解,而其中也包含的其他的东西,慢慢来…………
SQL映射文件!
mapper:映射文件的根元素,其中namespace用于区分不同的mapper,而且只能有一个mapper 或者说是接口
cache:为指定命名空间配置缓存
cache-ref:引用其他命名空间中的缓存配置
resultMap:描述结果集字段和java字段属性对应
↓ 增删改查
insert
delete
update
select
MyBatis框架的条件查询
单一查询
单个id查询
public Department selectOne(int depId);
<select id="selectOne" resultType="cn.chy.entity.Department" parameterType="int">
select depId,depName from department where id = #{depId}
</select>
接口方法:
public Department selectByName (String name);
安照名称 模糊查询
<select id="selectByName" resultType="cn.chy.entity.Department" parameterType="String">
select depId,depName from department where depName like '%'+#{depName}+'%'
</select>
多条件查询
接口方法
public Department selectByName (Department dept );
多条件查询
<select id="selectByWhere" resultType="cn.chy.entity.Department" parameterType="dept">
select depId,depName from department where id = #{depId} and depName like '%'+#{depName}+'%'
</select>
接口方法
public Department selectByName (Map<String,Object> map);
map.put("depId",val).....
多条件查询
<select id="selectByWhereTwo" resultType="cn.chy.entity.Department" parameterType="map">
select depId,depName from department where id = #{depId} and depName like '%'+#{depName}+'%'
</select>
↑就是多条件和但条件,但是参数都是与属性相关,不可分离,这样的话,我们就可以使用@Param(“自定义的名称”) // 为指定的参数取名
是不是憨了???? (继续)
结果映射
使用resultMap标签结果映射
本质上目的就是将数据库字段与javabean中字段相映射
id是引用的
<resultMap id="mydepater" type="cn.chy.entity.Department">
<id column="depId" property="depId"/>
<result column="depName" property="depName"/>
</resultMap>
// 应用map
<select id="selectAllOneTest" resultMap="mydepater">
SELECT d.depId,depName,id,last_name lastName,gender,email FROM department d LEFT JOIN tal_employee e ON e.depId = d.depId
where d.depId = #{depId} limit 1
</select>
来了 来了 最难理解的嵌套结果,他来了
assocation 对象元素嵌套 (一对多)
<resultMap id="mydepater" type="cn.chy.entity.Department">
<id column="depId" property="depId"/>
<result column="depName" property="depName"/>
// department对象属性 类型
<association property="emp" ofType="cn.chy.entity.Employee">
<id column="id" property="id"/>
<result column="lastName" property="name"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
</association>
</resultMap>
collection 集合元素嵌套
<resultMap id="mydepater" type="cn.chy.entity.Department">
<id column="depId" property="depId"/>
<result column="depName" property="depName"/>
<collection property="empList" ofType="cn.chy.entity.Employee">
<id column="id" property="id"/>
<result column="lastName" property="name"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
</collection>
</resultMap>
增
<insert id='insertOne' parameterType='cn.chy.entity.Department'>
</insert>
删
<delete id='insertOne' parameterType='cn.chy.entity.Department'>
</delete >
改
<update id='insertOne' parameterType='cn.chy.entity.Department'>
</update >
这个 就是MyBatis中一些基础的详解,对于后面使用频率特别高,值得你多看(嘿嘿)