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();
	}
}

猜你喜欢

转载自blog.csdn.net/qq_37759895/article/details/85841558