Java_myBatis_xml代理写法
这种开发方式只需要写好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代理写法的更多相关文章
- Java_Mybatis_注解代理写法
Mybatis的开发方式其实有3种: 1. 原始Dao开发(就是把mapper接口.映射文件和实现类都一并开发) 2. xml代理(就是只实现mapper接口和映射文件) 3.注解代理(就是只实现ma ...
- Java_myBatis_XML代理_动态SQL
主要是设计到映射文件的编写: SELECT: <sql id="query_user_where"> <!-- test里面可以编写OGNL表达式 --> ...
- Mybatis项目中不使用代理写法【我】
首先 spring 配置文件中引入 数据源配置 <?xml version="1.0" encoding="UTF-8"?> <beans x ...
- Java_myBatis_XML代理_延迟加载
使用mybatis的延迟加载,需要两个步骤: 1.在全局配置文件中添加一下语句(lazyLoadingEnabled默认为false,aggressiveLazyLoading默认为true) < ...
- ES3、ES5、ES6对象代理的写法差异
ES3的对象代理写法: console.log('定义私有变量ES3写法:') // ES3 var Person = function (){ var data = { name:'ES3', ag ...
- Java设计模式9:代理模式
代理模式 代理模式的定义很简单:给某一对象提供一个代理对象,并由代理对象控制对原对象的引用. 代理模式的结构 有些情况下,一个客户不想活着不能够直接引用一个对象,可以通过代理对象在客户端和目标对象之间 ...
- urllib,request 设置代理
通常防止爬虫被反主要有以下几个策略: 1.动态设置User-Agent(随机切换User-Agent,模拟不同用户的浏览器信息) 2.使用IP地址池:VPN和代理IP,现在大部分网站都是根据IP来b ...
- 前端 | Nuxt.js axios baseURL,proxy 代理
平时用 Vue 写前端时,对于 axios 请求的常规操作一般是 统一定义好一个 axios 对象,使用 axios.defaults.baseURL 设置 baseURL 也不是不能直接把服务器地址 ...
- java 代理模式 总结
1.前言 最近舍友去面试遇到了关于java代理模式的问题. 我虽然知道怎么使用,但是没有做过正经的总结,因此有了这篇随笔,好好总结一下三大代理模式底层原理. 事实上,在开发项目的时候,基本用不上代理, ...
随机推荐
- 《Linux内核设计》第17章学习笔记
- warning C4996: 'strcpy': This function or variable may be unsafe.
mkdir 写成 _mkdir strcpy 写成为 strcpy_s 或是在项目处右击-->属性-->C/C++-->预处理器-->在预处理器定义后添加";_CR ...
- 现代程序设计 homework-01
搞了6个小时individual project...看看博客做一做第一次现代程序设计作业 1) 建立 GitHub 账户, 把课上做的 “最大子数组之和” 程序签入 我的github地址是https ...
- Apache+php安装和配置 windows
Apache+php安装和配置 windows Apache 安装 1.官网网址:http://httpd.apache.org/ 2.Download 3.点击链接Files for Microso ...
- docker+redis 持久化配置(AOF)
RDB持久化与AOF持久化简单描述 RDB:RDB使用快照的方式存储数据库中的内容,直接将所有键值对数据全部存入二进制文件.建议使用BGSAVE来进行备份,整个过程会新fork一个子进程来执行,不影响 ...
- org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unexpected failure during bean definition parsing Offending resource: class path resource [applicationC
这个错误是 org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration proble ...
- Highcharts之饼图
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- c++ 为自定义类添加stl遍历器风格的遍历方式
为仿照stl的遍历风格,实现对自定义类型的遍历. 1. 需要遍历的基础结构: struct ConnectionPtr { int id_; int port_; string addr_; //st ...
- php-编译模块1
在路径中找到phpize 在需要编译的模块文件夹中执行 如: /usr/bin/phpize Configuring for: PHP Api Version: 20131106 Zend Modul ...
- xml和对象 转换
//测试数据 static List<User> list = new List<User>() { new User(){id=1001 ,name="语文&quo ...