实体映射文件:
<mapper namespace="">
<resultMap type="实体类全名" id="名字">
<id property="" column=""/>主键字段
<result property="" column=""/>非主键字段
<result property="" column=""/>
</resultMap>
<insert id="" parameterType="" resultMap=“”>
insert into students(id,name,sal) values(#{id},#{name},#{sal})
</insert>
</mapper>
parameterType:参数类型 int (急促类型) map
如果是实体类则需要使用类全名
resultMap:返回类型,一般去上面实体类映射的id,根据上面的字段映射进行封装
resultType:返回类型,当实体类的字段和表中的字段一一对应,并且相等时可以写,并且上面的实体类映射也可以不写,最好都写上。
动态查询sql:
<select id="selectByTJ" parameter="Map" resultMap="">
select student_Id,student_Name,student_Sal from student
<where>
<if test="id!=null">
student_id=#{id}
</if>
<if test="name!=null">
student_Name=#{name}
</if>
<if test="sal!=null'>
student_Sql=#{sql}
</if>
</where>
</select>
动态增加sql:
<sql id="key">
<trim suffixOverrides=",">去掉逗号
<if test="id!=null">
student_Id,
</if>
<if test="name!=null">
student_Name,
</if>
<if test="sal!=null">
student_Sal,
</if>
<trim>
</sql>
<sql id="value">
<trim suffixOverrides=",">去掉逗号
<if test="id!=null">
#{id},
</if>
<if test="name!=null">
#{name},
</if>
<if test="sal!=null">
#{sal},
</if>
<trim>
</sql>
<insert id="add" parameter="" >
insert into(<include refid="key") values(<include refid="value")
</insert>
动态修改sql:
<update id="update" parameter="map">
update student
<set>
<if test="name!=null">
student_Name=#{name},
</if>
<if test="sql!=null">
student_Sql=#{sql},
</if>
</set>
where student=#{id}
</update>
动态删除sql:
<delete id="delete" >
delete student where student_Id in
<foreach collection="array" open="(" close=")" item="ids">(这里指定了遍历类型,所以update标签中 的parameter可以省略不写)
#{ids}
</foreach>
</delete>
collection:类型,遍历的类型
open:打开
close:关闭
items:遍历对象
public class Student{
private int id;
private String name;
private Card card;
}
public class Card{
private int id;
private String num;
}
一对一映射:
创建表:
create table students(
cid int(5) primary key,
name varchar(40),
scid int(5)
constrint scid_fk foreign key() reference cards(cid)
)
create table cards(
cid int(5) primary key,
cnum varchar(20)
)
配置Mapper
<resultMap type="com.org.Student" id="studentMap">
<id property="id" column="sid"/>主键字段
<result property="name" column="sname"/>非主键字段
<association property="card" resultMap="cardMap" />
</resultMap>
property:关联属性
resultMap:关联属性对应的映射id
<resultMap type="com.org.Card" id="cardMap">
<id property="id" column="cid"/>主键字段
<result property="num" column="cnum"/>非主键字段
</resultMap>
<select id="" parameter="iint" resultMap="studentMap">
select s.student_id,s.student_name,c.cnum
from student s inner join card c on s.scid=c.cid
where s.student_id=#{id}
</select>
一对多:
表:
create table grads(
gid int(5) primary key,
gnum varchar(20)
)
create table students(
sid int(5) primary key,
sname varchar(40),
sgid int(5)
constrint sgid_fk foreign key() reference grads(gid)
)
对应的实体类:
public class grad{
private int gid;
private String gname;
private List<Student> stuList;
}
public class{
private int sid;
private Stirng sname;
private grad g;
}
对应的映射mapper:
<resultMap type="com.org.Student" id="studentMap">
<id property="sid" column="sid"/>主键字段
<result property="sname" column="sname"/>非主键字段
</resultMap>
<resultMap type="com.org.grad" id="gradMap">
<id property="gid" column="gid"/>主键字段
<result property="gname" column="gname"/>非主键字段
</resultMap>
根据班级名称查找所有学生
<select id="" parameter="string" resultMap="studentMap">
select s.sid,s.sname from students s inner join grads g on s.sgid=g.gid where g.gname=#{name}
</select>
根据学生名字查找班级
<select id="" parameter="string" resultMap="gradtMap">
select g.gid,g.gname from students s inner join grads g on s.sgid=g.gid where s.sname=#{name}
</select>
多对多映射:
创建表:
create table courses(
cid int(5) primary key,
cnum varchar(20)
)
create table students(
sid int(5) primary key,
sname varchar(40),
)
create table middle(
msid int(5),
msid int(5),
primary key(mcid,msid)
)
对应的实体类:
public class course{
private int cid;
private String cname;
private List<Student> stuList;
}
public class Student{
private int sid;
private String sname;
private List<course> couList;
}
对应的映射文件:
<resultMap type="com.org.Student" id="studentMap">
<id property="sid" column="sid"/>主键字段
<result property="sname" column="sname"/>非主键字段
</resultMap>
<resultMap type="com.org.couse" id="courseMap">
<id property="cid" column="cid"/>主键字段
<result property="cname" column="cname"/>非主键字段
</resultMap>
测试:
ThreadLocal threadLocal = new ThreadLocal();
Reader reader = Resources.getResourceAsReader(“mybatis.xml”);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
public SqlSession getSqlSession (){
SqlSession sqlsession = threadLocal .get();
if(session==null){
sqlsession = sqlSessionFactory .openSqlsession();
threadLocal .set(sqlsession );
}
return sqlsession ;
}
public void close(){
SqlSession sqlsession = threadLocal .get();
if(session!=null){
sqlsession.close();
threadLocal remove();
}
}