insert:
parameterType 参数类型 :你传递的参数类型 ,如果有接口的话 就不用说清楚这个类型
selectKey:
keyProperty 属性名称
keyColumn 列名
resultType 返回值类型
order:
BEFORE 在insert之前执行 用于创建ID使用
AFTER 在insert之后执行 用于得到自动编号ID
select:
association(关联关系映射) 关联数据 或者 关联查询
public class Student {
private Integer sid;
private String sname;
private String address;
private String email;
private classroom cls; //表中的cls对象查询 student时将classroom对象中的数据也拿出来
private String phonenumber;
private Integer cid;
}
————————————————————————————————————
public class classroom {
private Integer cid;
private String curriculum;
}
关联关系相对应xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sun.mybatis.dao.StudentMappr">
<insert id="inse" parameterType="com.sun.mybatis.dao.Student">
INSERT INTO student(sname,address,email,phonenumber,cid)
VALUES(#{sname},#{address},#{email},#{phonenumber},#{cid})
<selectKey keyProperty="sid" order="AFTER" resultType="java.lang.Integer" keyColumn="sid">
-- 这里的返回类型要写resultType="java.lang.Integer"不然无法识别你要返回的类型
-- 这是查询返回你插入的主键号,因为自动编号是无法识别你掺入的是第几列order="AFTER"这里参数是之后,因为是插入完再拿到值
SELECT LAST_INSERT_ID()
</selectKey>
</insert>
<resultMap id="SelStuCls" type="com.sun.mybatis.dao.Student">
<id column="sid" property="sid"/>
<result column="sname" property="sname"/>
<result column="address" property="address"/>
<result column="email" property="email"/>
<result column="phonenumber" property="phonenumber"/>
<association property="cls" javaType="com.sun.mybatis.dao.classroom">
<!--这里classroom是student的关联表-->
<!--将classroom对象放入Student中的cls对象当中-->
<result column="cid" property="cid"></result>
<result column="curriculum" property="curriculum"></result>
</association>
</resultMap>
<select id="SelectStuAndCls" resultMap="SelStuCls">
SELECT s.*,c.`curriculum`FROM student s INNER JOIN classroom c ON s.cid=c.cid
</select>
<select id="SelectStu" resultMap="SelStuCls">
-- resultMap 映射结果集
-- resultMap拿到SelStuCls映射文件 对应的是要拿出来的数据
SELECT s.*,c.`curriculum`FROM student s INNER JOIN classroom c ON s.cid=c.cid where sid=#{param1}
</select>
</mapper>
main操作类写法
public class Teat {
public static void main(String[] args) {
SqlSessionFactory sqlSessionFactory = Mybatisconfig.sqlSessionFactory;
SqlSession sqlSession = sqlSessionFactory.openSession();
StudentMappr studentMappr = sqlSession.getMapper(StudentMappr.class);
Student>students = studentMappr.select();
}
}