mybatis三种映射方式

您所在的位置:网站首页 项目的开发方式有三种 mybatis三种映射方式

mybatis三种映射方式

2024-07-11 04:28:34| 来源: 网络整理| 查看: 265

mybatis有三种映射ORM的方式:纯xml文件映射,基于xml的接口映射以及基于注解的接口映射

以一个例子来说明:这里有一个商品类Product(映射到表product_),类均放在包com.pojo下

Product类 public class Product { int id; String name; float price; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } } product_表 create table product_( id int primary key auto_increment, name varchar(20), price float ); 纯xml文件映射

首先在项目的src路径下建立文件mybatis-config.xml作为mybatis的映射文件

然后在com.pojo包下新建映射文件Product.xml,在文件里定义相关操作的sql语句,ps:记得在mybatis-config.xml文件中配置这个xml文件

Product.xml select * from product_ select * from product_ where id = #{id}

CRUD操作方法

String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = sqlSessionFactory.openSession(); //查询所有商品信息 List products = session.selectList("listProduct");//参数为Product.xml文件里相应sql语句的id //查询id为1d的商品信息 Product product = session("selectOneProduct",1);//第二个参数的类型为Product.xml文件里相应sql语句的parameterType 基于xml的接口映射

这种方式是将接口与xml结合起来,创建接口,然后通过xml实现接口,即将接口的具体实现放在xml文件中,而我们不再是直接调用xml文件去操作CRUD,而是通过调用接口的方式访问。参考此处.

首先依旧是在项目的src路径下建立文件mybatis-config.xml映射文件,这一步与纯xml文件映射完全相同。

接着需要创建类的接口,接口放在com.mapper包下

ProductMapper.java public interface ProductMapper { List listProduct(); Product get(int id); }

然后创建映射文件ProductMapper.xml,放在com.pojo包下

select * from product_ select * from product_ where id = #{id}

这里有几点是关键,ProductMapper.xml文件里这里namespace属性必须填写所要映射的接口完全限定名;这一句的id相当于接口的某个函数的方法体,id属性对应接口中的方法名,parameterType属性对应方法的参数类型,resultType属性对应方法的返回值类型。 最后别忘了在mybatis-config.xml文件中配置ProductMapper.xml

CRUD操作方法

String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = sqlSessionFactory.openSession(); //获得一个ProductMapper实例 ProductMapper productMapper = session.getMapper(ProductMapper.class); //查询所有商品信息 List products = productMapper.listProduct(); //查询id为1d的商品信息 Product product = productMapper.get(1); 基于注解的接口映射

这种方式将不再需要创建除了mybatis-config.xml之外的映射文件,采用接口调用的方式操作CRUD

首先依旧是在项目的src路径下建立文件mybatis-config.xml映射文件

接着创建接口ProductMapper.java

@Mapper public interface ProductMapper { @Select("select * from product_") List listProduct(); @Select("select * from product_ where id=#{id}") Product get(int id); }

最后记得在mybatis-config.xml文件中配置接口ProductMapper或者

CRUD操作方法

String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = sqlSessionFactory.openSession(); //获得一个ProductMapper实例 ProductMapper productMapper = session.getMapper(ProductMapper.class); //查询所有商品信息 List products = productMapper.listProduct(); //查询id为1d的商品信息 Product product = productMapper.get(1);


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭