1.返回值是简单类型

2.返回值是引用类型

    1)返回的字段与表属性相同

    2)返回的字段与表属性不同

    3)返回字段与属性相同,类型不同

3.返回值是Map

4.模糊查询

5.获取刚记录的主键值

==============================================================

工具类Mybatis.java

public class MyBatisUtil {
    private  static SqlSessionFactory factory;
    static {
        String config="mybatis.xml";
        InputStream in = null;
        try {
            in = Resources.getResourceAsStream(config);
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            factory = builder.build(in);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public  static SqlSession getSqlSession(){
        SqlSession sqlSession = null;
        if( factory != null){
            sqlSession = factory.openSession();
        }
        return sqlSession;
    }
}

1.返回值是简单类型

StudentDao.java

public Interface StudentDao{
    //接口中的方法返回简单类型
    int countStudent();
}

StudentDao.xml

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="StudentDao.java的全限定类型名称">
    <select id="countStudent" resultType="int">
        select count(*) from student
    </select>
</mapper>

resultType表示sql语句执行结果的数据类型,可以用别名或者全限定名称

一条select标签实际上表示的是下面一段代码

ResultSet rs = pst.execuetQuery(" select id,name,email,age from student where id=#{id}");
while(rs.next){
    Order student = new Order();
    student.setId(rs.getInt("Id"));
    student.setName(rs.getString("name"))
    ...
}

2.返回值是引用类型

   1)返回的字段与表属性相同

        StudentDao.java

public class StudentDao{
    List<Student> selectStudentById(int id);
}

        StudentDao.xml

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="StudentDao.java的全限定类型名称">
    <select id="selectStudentById" resultType="Student的全限定类型名称">
        select id,name,email,age from student where id=#{id}
    </select>
</mapper>

     2)返回的字段与表属性不同,返回类型不同

        Customer.java

public class Customer {
    private Integer cid;
    private String cname;
    private String cemail;
    private Integer cage;
    
    ...set/get...
    ...toString...
}

        StudentDao.java

public Interface StudentDao{
    //返回对象,当对象的属性名和列名不一样
    Customer selectResultTypeStudentById(Integer id);
}

        StudentDao.xml

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="StudentDao.java的全限定类型名称">
    <resultMap id="customMap" type="com.bjpowernode.vo.Customer" >
        <!--数据库表的列和java对象的属性的对应关系-->
        <!--主键列的对应关系
            column:列名
            property:java对象的属性名
        -->
        <id column="id" property="cid" />
        <!--非主键列,使用result-->
        <result column="name" property="cname" />
        <result column="email" property="cemail" />
        <result column="age" property="cage"/>
    </resultMap>
    <select id="selectResultTypeStudentById" resultMap="customMap">
         select id  ,name  ,email ,age  from student where id=#{id}
    </select>
</mapper>

 定义resultMap,给resultMap起个唯一名称
        type:java对象的类型, 一般使用类的全限定名称。sql语句执行结果的ResultSet会转为这个type指定的类型
        id:自定义的唯一名称,表示转换的规则

resultType, 当属性名和列名不一样
       while(rs.next()){
         Customer c = new..
         c.setCId(rs.getInt("id")):
       }

    MyTest.java

public class MyTest{
    @Test
    public void testSelectResultTypeStudentById(){
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        Customer student = studentDao.selectResultTypeStudentById(1002);
        System.out.println("student:"+student);
    }
}

    3)返回字段与属性相同,类型不同

        Order.java

public class Order {
    private Integer id;
    private String name;
    private String email;
    private Integer age;

    ...set/get/toString...
}

        StudentDao.java

public Interface StudentDao{
    Order selectOrderById(Integer id);
}

        StudentDao.xml

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="StudentDao.java的全限定类型名称">
    <select id="selectOrderById" resultType="Order的全限定类型名称">
        select id,name,email,age from student where id=#{id}
    </select>
</mapper>

3.返回值是Map:就是将字段作为key,值作为value存入Map中

        StudentDao.java

public Interface StudentDao{
    //Map<列名,列值)
    Map<String,Object> selectByMap(Integer age);
}

        StudentDao.xml

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="StudentDao.java的全限定类型名称">
    <!--
      sql语句的执行结果转为map
    -->
    <select id="selectByMap" resultType="java.util.HashMap">
        select name, email from student where age = #{age}
    </select>
</mapper>

        MyTest.java

public class MyTest{
    @Test
    public void testSelectMap(){
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        Map<String,Object> map = studentDao.selectByMap(22);
        System.out.println("map:"+map);
    }
}

4.模糊查询

        StudentDao.java

public interface StudentDao {
    List<Student> selectLikeTwo(String name);
}

        StudentDao.xml

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="StudentDao.java全限定名">   
     <select id="selectLikeTwo" resultType="Student.java全限定名">
        select * from student where name like "%" #{name} "%"
    </select>
</mapper>

        MyTest.java

public class MyTest{
    @Test
    public void testSelectLikeTwo(){
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        //在java代码中传入 模糊查询的内容
        //String name="%周%";//直接传入占位符
        String name="李";
        List<Student> students = studentDao.selectLikeTwo(name);
        students.forEach(stu-> System.out.println(stu));
    }
}

5.获取刚记录的主键值

School.java

public class School {
    private Integer id;
    private String name;
    private String address;

    ...set/get/toString...
}

SchoolDao.java

public interface SchoolDao {
    int insertSchool(School school);
}

SchoolDao.xml

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bjpowernode.dao.SchoolDao">
    <insert id="insertSchool">
        insert into school(name,address) values(#{name},#{address})
        <selectKey keyColumn="stuId" keyProperty="id" resultType="int" order="AFTER">
            select last_insert_id() as stuId
        </selectKey>
    </insert>
</mapper>

keyColumn="stuId"    数据库表的主键字段

keyProperty="id"   主键字段对应的类的属性

resultType="int"    返回的结果类型

order="AFTER"    这条select语句在insert之后执行

last_insert_id()     sql函数,查找最后一条插入数据的id

MyTest.java

public class MyTest{
    @Test
    public void testInsertSchool(){
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        SchoolDao dao = sqlSession.getMapper(SchoolDao.class);
        School school = new School();
        school.setName("航空大学");
        school.setAddress("北京的学院路");
        System.out.println("insert之前id:"+school.getId());
        int rows = dao.insertSchool(school);
        System.out.println("rows:"+rows);
        //获取刚刚添加的记录的主键值
        Integer id = school.getId();
        System.out.println("主键id:"+id);
        sqlSession.commit();
        sqlSession.close();
    }
}