1. 程式人生 > >mapper動態代理方式的crud(MyBatis介面的開發)

mapper動態代理方式的crud(MyBatis介面的開發)

二、mapper動態代理方式的crud(MyBatis介面的開發):
原則:約定優於配置 abc.java name
配置方式:abc.xml 
        <name>myproject</name>
硬編碼方式: abc.java
        Configuration conf = new Configuration();
        con.setName("myproject");
約定:預設值就是myproject


具體實現步驟:
1.基礎環境:mybatis.jar,mysql.jar 兩個xml
2.(不同之處)
     約定的目標:省略掉statement(sql),即,根據約定,直接可以定位sql語句
     a.介面:
         1.方法名和mapper.xml檔案中標籤的id值相同
        2.方法的輸入引數和mapper,xml檔案標籤的parameterType型別一致
        3.方法的返回值和mapper.xml檔案標籤的resultType型別一致
除了以上約定,要實現介面中的方法和Mapper.xml中SQL標籤一一對應,還需要以下1點:
    namespace的值就是介面的全類名(介面-mapper.xml)

匹配的過程:(約定的過程)
1.根據 介面名 找到 mapper.xml檔案(根據的是namespace=介面全類名)
2.根據 介面的方法名 找到 mapper.xml檔案的SQL標籤(方法名=sql標籤的Id值)
以上2點可以保證:當我呼叫介面中的方法時,程式能自動定位到 某一個Mapper。xml檔案中的sql標籤

習慣:SQL對映檔案(Mapper.xml)和介面放在同一個包中

以上,可以根據介面的方法->sQL語句

執行:
    StudentMapper stu = session.getMapper(StudentMapper.class);
    Student stu1 = stu.StudentById("1508080117");
mapper.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.mapper.StudentMapper">
	 <select id="StudentById" resultType="com.user.Student" parameterType="String">
	 	<!-- 輸入輸出:如果是簡單型別,可以以使用任何佔位符,#{xxx}
	 		如果是物件,只能是屬性
	 	 -->
	 	select * from student where stu_id = #{id}
	 </select>
	 <insert id="insertStu" parameterType="com.user.Student">
	 	insert into student(stu_id,stu_name,stu_birth,stu_sex) values(#{stu_id},#{stu_name},#{stu_birth},#{stu_sex})
	 </insert>
	 <delete id="deleteStuByNo" parameterType="String">
	 	delete from student where stu_id=#{xxx}
	 </delete>
	 <update id="updateStudentByStuNo" parameterType="com.user.Student">
	 	update student set stu_name=#{stu_name},stu_birth=#{stu_birth},stu_sex=#{stu_sex} where stu_id = #{stu_id}
	 </update>
	 <!-- 如果返回值型別是一個物件,則無論返回一個還是多個,resultType都寫成類com.user.Student -->
	 <select id="queryAllStu" resultType="com.user.Student">
	 	select * from student
	 </select>
</mapper>

介面

package com.mapper;

import com.user.Student;

//操作Mybatis介面
public interface StudentMapper {
	/*
	 * 1.方法名和mapper.xml檔案中標籤的id值相同
	 * 2.方法的輸入引數和mapper,xml檔案標籤的parameterType型別一致
	 * 3.方法的返回值和mapper.xml檔案標籤的resultType型別一致
	 */
	Student StudentById(String xxx);
}

 

測試:

package com.user;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.mapper.StudentMapper;
import com.user.Student;

public class TestMybatis3 {
	public static void main(String[] args) throws IOException {
		queryall();
//		addStu();
//		queryall();
	}
	//查詢所有學生
	public static void queryall() throws IOException {
		Reader reader = Resources.getResourceAsReader("config.xml");
		//build第二個引數可以指定該環境id
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
		SqlSession session = sqlSessionFactory.openSession();
		StudentMapper stu = session.getMapper(StudentMapper.class);
		Student stu1 = stu.StudentById("1508080117");
		System.out.println(stu1);
		session.close();
	}
}