Mybatis Dao层注解及XML组合Dao的开发方式
mybatis可以用xml进行数据操作,也可以在dao层用注解的方式,也可以采取xml和dao层接口组合使用的方法。显然 ,后者更加简单。
实体类Student
package com.zhao.entity; /**
*
* @author: zhao
* @time: 2016年5月31日
*
* @description:学生
*/
public class Student {
private int stuId;
private String stuName;
private String stuClass; public int getStuId() {
return stuId;
} public void setStuId(int stuId) {
this.stuId = stuId;
} public String getStuName() {
return stuName;
} public void setStuName(String stuName) {
this.stuName = stuName;
} public String getStuClass() {
return stuClass;
} public void setStuClass(String stuClass) {
this.stuClass = stuClass;
} @Override
public String toString() {
return "Student [stuId=" + stuId + ", stuName=" + stuName + ", stuClass=" + stuClass + "]";
} }
1: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.zhao.dao.StudentDao">
<select id="queryById" parameterType="int" resultType="Student">
select * from student where stu_id=#{stuId}
</select>
</mapper>
先进行测试
private String resource="mybatis-config.xml";
private InputStream inputStream;
private SqlSessionFactory sqlSessionFactory;
private SqlSession sqlSession; @Before
public void before(){
inputStream=StudentTest.class.getClassLoader().getResourceAsStream(resource);
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
sqlSession=sqlSessionFactory.openSession();
}
@After
public void after(){
sqlSession.close();
} @Test
public void testXmlQueryById() {
Student student=(Student)sqlSession.selectOne("com.zhao.dao.StudentDao.queryById", 1);
System.out.println(student);
}
xml的方式操作数据库,用了SqlSession的selectOne方法。
public abstract <T> T selectOne(String paramString, Object paramObject);
当然,我们在mybatis的配置文件中,定义了类的别名、StudentDao.xml 以及数据库
<mappers>
<mapper resource="com/zhao/mapper/StudentDao.xml"/>
</mappers>
现在我们能查到结果
Student [stuId=1, stuName=ZHAO, stuClass=Java10班]
2:在dao层使用注解
public interface StudentDao {
@Select("select * from student where stu_id=#{stuId}")
public Student queryById(int stuId);
}
为了避免混淆,再修改一下配置文件
<mappers>
<mapper class="com.zhao.dao.StudentDao"/>
</mappers>
然后再进行测试
@Test
public void testAnnotationQueryById(){
StudentDao studentDao=sqlSession.getMapper(StudentDao.class);
Student student=studentDao.queryById(1);
System.out.println(student);
}
我们可以看到,是用了SqlSession的getMapper方法得到了一个Dao层接口对象,然后调用了其中的queryById方法查到的结果。
目前来看:
xml和dao层注解之间并没有什么联系,是两个不同的查询方式。
但是xml的配置比较简单,但是使用起来比较繁琐。而dao层注解需要在代码上进行操作,看起来也不舒服。
3:xml+dao
并不需要修改测试类
@Test
public void testAnnotationQueryById(){
StudentDao studentDao=sqlSession.getMapper(StudentDao.class);
Student student=studentDao.queryById(1);
System.out.println(student);
}
这里跟用注解是一样的。不过Dao层接口中注解已经被我删除了
public interface StudentDao {
public Student queryById(int stuId);
}
现在需要把xml和dao 联系起来
<?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.zhao.dao.StudentDao">
<select id="queryById" parameterType="int" resultType="Student">
select * from student where stu_id=#{stuId}
</select>
</mapper>
其实我并没有修改这个mapper文件,我们可以看到 mapper便签的namespace属性就是Dao层接口的全路径,select的id属性就是Dao层接口的相应方法,这些名字都是一样的。当然 也必须是一样的。
然后修改配置文件
<mappers>
<mapper resource="com/zhao/mapper/StudentDao.xml"/>
</mappers>
这样做就是为了让xml和dao能组合起来。配置文件中配置的是xml。但是这个xml指向了一个接口。我们在用的时候通过接口来进行相应操作,会更加清晰明了。在xml中修改sql代码也很舒服。
Mybatis Dao层注解及XML组合Dao的开发方式的更多相关文章
- Struts2+DAO层实现实例02——搭建DAO基本框架并与Struts2组合
实例内容 创建DAO(Data Access Oject)接口:BaseDAO 创建其实例化类:UserDAO 用于获取数据库struts中的userinfo表中的内容 创建User的Java Bea ...
- MyBatis框架的XML数据访问Dao层接口的组合使用
MyBatis 的前生为Apache的开源项目iBatis.其优势在于灵活,几乎可以替代JDBC,同时提供了编程接口.目前MyBatis的数据访问Dao层不需要实现类,也不需要像JDBC那样拼接Hql ...
- 基于Mybatis的Dao层的开发
基于Mybatis的Dao层开发 SqlSessionFactoryBuilder用于创建SqlSessionFacoty,SqlSessionFacoty一旦创建完成就不需要SqlSessionFa ...
- Mybatis进阶学习笔记——动态代理方式开发Dao接口、Dao层(推荐第二种)
1.原始方法开发Dao Dao接口 package cn.sm1234.dao; import java.util.List; import cn.sm1234.domain.Customer; pu ...
- MyBatis开发Dao层的两种方式(原始Dao层开发)
本文将介绍使用框架mybatis开发原始Dao层来对一个对数据库进行增删改查的案例. Mapper动态代理开发Dao层请阅读我的下一篇博客:MyBatis开发Dao层的两种方式(Mapper动态代理方 ...
- mybatis dao 层开发简易版 非整合 spring
同样老习惯,先上项目结构截图 首先 补充上篇文中缺失的 mysql demo 用的 小脚本 drop database if exists mybatis; CREATE DATABASE `myba ...
- 基于Mybatis的Dao层开发
转自:https://www.cnblogs.com/rodge-run/p/6528398.html 基于Mybatis的Dao层开发 SqlSessionFactoryBuilder用于创建 Sq ...
- SpringBoot整合Mybatis使用注解或XML的方式开发
2018-6-4 补充mybatis-spring-boot注解的使用 1.导包 只需要再导入mysql+mybatis两个包 <dependency> <groupId>or ...
- [转]JAVA中Action层, Service层 ,modle层 和 Dao层的功能区分
首先这是现在最基本的分层方式,结合了SSH架构.modle层就是对应的数据库表的实体类.Dao层是使用了Hibernate连接数据库.操作数据库(增删改查).Service层:引用对应的Dao数据库操 ...
随机推荐
- js屏蔽/过滤 特殊字符,输入就删除掉,实时删除,
原文:http://niuxiaoxia870563296.iteye.com/blog/1828189 1.替换方法: <input type="text" class=& ...
- jvm运行时内存模式
jvm内存模型 内存模型粗略划分为:堆和栈 详细划分为:堆,虚拟机栈,方法区,本地方法区,程序计数器 程序计数器: 为了线程切换后能恢复到正确的执行位置,每条线程都需要有一个独立的程序计数器,各条线程 ...
- [1] YOLO 图像检测 及训练
YOLO(You only look once)是流行的目标检测模型之一, 原版 Darknet 使用纯 C 编写,不需要安装额外的依赖包,直接编译即可. CPU环境搭建 (ubuntu 18.04) ...
- 史上最简单的springboot国际化多语言切换实现方案
每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code 前提: 在resources目录下建立 messages_en_US.properti ...
- KazaQ's Socks (找规律)
#include<iostream> using namespace std; #define ll long long ll n, m; ll t; int main(){ while ...
- 【转】Win10开机密码忘了?教你破解Win10开机密码
[PConline 技巧]Win10开机密码忘记了怎么办?这个问题很多人都遇到过,如何破解一台Win10电脑的开机密码呢(非BIOS开机密码)?其实很简单,利用下面这个方法,分分钟就能搞定. 一招破解 ...
- 【window】git安装教程
相关链接:https://blog.csdn.net/nly19900820/article/details/73379854 作者:smile.轉角 QQ:493177502
- ESP32:mdns协议
mdns(即多播dns,Multicast DNS)主要实现了在没有传统DNS服务器的情况下使局域网内的主机实现相互发现和通信,使用的端口为5353,遵从dns协议,使用现有的DNS信息结构.语法和资 ...
- postman简介及基本用法
从分层测试角度来说,接口测试是相对来说性价比最高的,且作为功能测试进阶的必备技能,接口测试值得大家都去学习掌握. 工欲善其事,必先利其器,好的工具能更好的帮助工程师更高效率的完成工作. 常见的接口测试 ...
- 使用Windows Live Writer撰写的第一篇博文
一直没有时间,在自己的电脑上配置起来Windows Live Writer. 今天抽时间搞起来后,感觉果然比在Web版写作不知道爽多少倍哦. 还安装了代码插件,上传代码和图片也方便了很多,霸气. 先上 ...