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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦幻D开始

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值