这种开发方式只需要写好Mapper.xml和对应的Interface就可以了。

1.编写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.mavenTest.mybatis_mapper.StudentMapper">
<select id="getStudentById" parameterType="int" resultType="com.mavenTest.mybatis_test.po.Student">
select * from students_table where id = #{id}
</select>
<insert id="insertStudent" parameterType="com.mavenTest.mybatis_test.po.Student">
insert into students_table(name,student_code,createTime,class_id,userId) values(#{name},#{student_code},#{createTime},#{class_id},#{userId})
</insert>
</mapper>

2.编写interface

package com.mavenTest.mybatis_mapper;

import com.mavenTest.mybatis_test.po.Student;

public interface StudentMapper {
public Student getStudentById(int id) throws Exception;
public void insertStudent(Student s) throws Exception;
}

就这两步其实就已经完成了开发,但是值得注意的是:

1.xml中namespace要和interface的全名一致,如上面的“com.mavenTest.mybatis_mapper.StudentMapper”

2.xml中的查询标签的Id要和interface的方法名一致,如上面的“getStudentById”和“insertStudent”

3.xml中parameterType和interface方法的传参要一致,如上面的"int id"和parameterType="int"

4.xml中resultType和interface方法的返回值类型要一致,如上面的"Student"和resultType="com.mavenTest.mybatis_test.po.Student"


单元测试:

public class StudentMapperTest {

    private SqlSessionFactory ssf;

    @Before
public void before() throws IOException {
String resources = "SqlMapConfig.xml";
InputStream is = Resources.getResourceAsStream(resources);
this.ssf = new SqlSessionFactoryBuilder().build(is);
} @Test
public void test() throws Exception {
SqlSession ss = this.ssf.openSession();
StudentMapper sm = ss.getMapper(StudentMapper.class);
Student s = sm.getStudentById(869);
System.out.println(s.getName());
} }

值得注意的是,怎么生成我们想要的mapper(可以直接调用它的方法来操作数据库)呢?

我们其实还是要走:SqlSessionFactoryBuilder--build()-->SqlSessionFactory--openSession()-->SqlSession--getMapper()-->mapper

得到mapper我们就可以直接使用之前interface定义好的方法了,不再需要我们直接使用SqlSession下面的方法了

Java_myBatis_xml代理写法的更多相关文章

  1. Java_Mybatis_注解代理写法

    Mybatis的开发方式其实有3种: 1. 原始Dao开发(就是把mapper接口.映射文件和实现类都一并开发) 2. xml代理(就是只实现mapper接口和映射文件) 3.注解代理(就是只实现ma ...

  2. Java_myBatis_XML代理_动态SQL

    主要是设计到映射文件的编写: SELECT: <sql id="query_user_where"> <!-- test里面可以编写OGNL表达式 --> ...

  3. Mybatis项目中不使用代理写法【我】

    首先 spring 配置文件中引入 数据源配置 <?xml version="1.0" encoding="UTF-8"?> <beans x ...

  4. Java_myBatis_XML代理_延迟加载

    使用mybatis的延迟加载,需要两个步骤: 1.在全局配置文件中添加一下语句(lazyLoadingEnabled默认为false,aggressiveLazyLoading默认为true) < ...

  5. ES3、ES5、ES6对象代理的写法差异

    ES3的对象代理写法: console.log('定义私有变量ES3写法:') // ES3 var Person = function (){ var data = { name:'ES3', ag ...

  6. Java设计模式9:代理模式

    代理模式 代理模式的定义很简单:给某一对象提供一个代理对象,并由代理对象控制对原对象的引用. 代理模式的结构 有些情况下,一个客户不想活着不能够直接引用一个对象,可以通过代理对象在客户端和目标对象之间 ...

  7. urllib,request 设置代理

     通常防止爬虫被反主要有以下几个策略: 1.动态设置User-Agent(随机切换User-Agent,模拟不同用户的浏览器信息) 2.使用IP地址池:VPN和代理IP,现在大部分网站都是根据IP来b ...

  8. 前端 | Nuxt.js axios baseURL,proxy 代理

    平时用 Vue 写前端时,对于 axios 请求的常规操作一般是 统一定义好一个 axios 对象,使用 axios.defaults.baseURL 设置 baseURL 也不是不能直接把服务器地址 ...

  9. java 代理模式 总结

    1.前言 最近舍友去面试遇到了关于java代理模式的问题. 我虽然知道怎么使用,但是没有做过正经的总结,因此有了这篇随笔,好好总结一下三大代理模式底层原理. 事实上,在开发项目的时候,基本用不上代理, ...

随机推荐

  1. 第六周分析Linux内核创建一个新进程的过程

    潘恒 原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 task_struct结构: ...

  2. 使用Junit进行单元测试

    使用Junit进行单元测试 一.目的和要求 JUnit是一款由Erich Gamma(<设计模式>的作者)和Kent Beck(极限编程的提出者)编写的开源的回归测试框架,供Java编码人 ...

  3. MySQL中wait_timeout的坑

    今天遇到了一个问题,一个项目,放到服务器(tomcat)下面的跑,但第二天,总是报错,项目还不能跑 com.mysql.jdbc.exceptions.jdbc4.CommunicationsExce ...

  4. 第二个spring

    由于第一个spring已经完成,我们现在进去第二个spring! 陈志棚:成绩的统筹 李天麟:界面音乐 徐侃:代码算法   plan好布局,分配任务,控制时间!

  5. 总结 推广app

    扫一扫二维码即可安装使用我们的app,方便快捷. 电脑端下载地址:http://pan.baidu.com/s/1bocWPPX http://a.app.qq.com/o/simple.jsp?pk ...

  6. HDU 2053 Switch Game

    http://acm.hdu.edu.cn/showproblem.php?pid=2053 Problem Description There are many lamps in a line. A ...

  7. Android控件第6类——杂项控件

    1.Toast Toast用于显示提示信息. Toast不会获得焦点,没法关闭,过段时间会自动消失. 使用方法:Toast.makeText获得Toast,并设置相关属性.调用Toast对象的show ...

  8. Linux基础学习(2)--Linux系统安装

    第二章——Linux系统安装 一.VMware虚拟机安装与使用 1.VMware简介:            VMware是一个虚拟PC的软件,可以在现有的操作系统上虚拟出一个新的硬件环境,相当于模拟 ...

  9. js regex variable & Set, Map

    js regex variable & Set, Map regex, variable, Set, Map, 交集, 差集, 并集, https://stackoverflow.com/qu ...

  10. s-axis-config-tdata