首页 > 编程知识 正文

mybatis是orm框架吗,mybatis框架优点

时间:2023-05-03 07:34:42 阅读:12905 作者:1865

持久层框架MyBatis 1. mybatis介绍2 .运行流程3 .使用步骤代码实现示例:商品分类CRUD操作4.1对4.2对多5 .参数占位符6 .复杂搜索6.1动态SQL语句6.2集合参数

1 .我的电池介绍

传统框架的缺点:

要使用jdbc,程序员必须创建连接,手写sql,处理结果集,使用mybatis框架,然后创建连接。 所有结果集的处理都是由框架完成的。

mybatis是轻量级持久层的框架,是从ibatis进化而来的。 自动连接到数据库,并将数据库的结果集封装到对象的POJO中。

2 .执行流程Mybatis启动时:

从application.yml读取数据库名称、username和password,读取所有XML@mapperscan(“mapper”),然后为每个接口创建代理对象执行的流程:

在Controller中,通过autowired从spring容器连接到代理对象catagoryMapper.selectByExample ()、my batis invoke )和数据库,然后选择方法名称sellis 通过获取包名称的namespace找到xml文件,通过方法名称找到sql语句执行sql,遍历resultSet,遍历每行数据,class.forname (实体类名),执行sql 使用将对象放置在列表中的return list 3.逆向工程generater项目生成xml、接口、pojo和example,并依赖于web、mybatis和MySQL驱动程序将@mapperscan(「mapper包名称”)添加到要配置yml的日志APP应用程序中时,mybatis框架会为包下的每个接口创建代理对象。 在@Autowried中,按spring容器中的insert (、delete )、update ()、查询example、criteria ()列生成控制器) 例如,商品分类CRUD操作由逆向工程genenerud进行。pojo, example是构成application.yml server : port :8080 spring :数据源: driver-class-name 3360 com.MySQL.CJ .的use ne=GMT8username : rootpassword 3360123456 my batis 3360 mapper locations 01 _ crud.mapper/*.XML logging 3360 path

@MapperScan注释package com.tedu.mybatis01_crud; @ springbootapplication//@ mappers can:my batis框架会自动为mapper包下的接口创建动态代理对象//动态代理对象,并生成数据将resultSet转换为实体@ mappers can (com.tedu.my batis 01 _ crud.mapper ) ) publicclassmybatis 01 crud应用程序{ publicstaticvation } import java.util.List; importorg.spring framework.beans.factory.annotation.auto wired; importorg.spring framework.web.bind.annotation.cross origin; importorg.spring帧web.bind.annotation.request mapping; importorg.spring帧web.bind.annotation.rest controller; import com.tedu.my batis 01 _ crud.mapper.category mapper;

import com.tedu.mybatis01_crud.pojo.Category;import com.tedu.mybatis01_crud.pojo.CategoryExample;@RestController@RequestMapping("/category")public class CategoryController {//接口没有实现类,mybatis框架会自动为mapper包下的所有接口创建代理类//创建一个代理对象,把代理对象放到容器中//从容器中取代理对象@AutowiredCategoryMapper categoryMapper;//添加@RequestMapping("/insert")@CrossOriginpublic String insert(Category category) {return "添加的行="+categoryMapper.insert(category);}//修改@RequestMapping("/update")@CrossOriginpublic String update(Category category) {//:8080/update?catagoryId=1&categoryName=笔记本//:8080/selectDesc//updateByPrimaryKey()//updateByPrimaryKeySelective 属性为空,不生成desc=nullreturn "更新的行:"+categoryMapper.updateByPrimaryKeySelective(category);}//删除@RequestMapping("/delete")@CrossOriginpublic String delete(Integer categoryId) {return "删除的行:"+categoryMapper.deleteByPrimaryKey(categoryId);}//升序@RequestMapping("/selectAsc")@CrossOriginpublic List<Category> selectAsc(){CategoryExample example = new CategoryExample();example.setOrderByClause("category_id asc");return categoryMapper.selectByExample(example);}//降序@RequestMapping("selectDesc")@CrossOriginpublic List<Category> selectDesc(){CategoryExample example = new CategoryExample();example.setOrderByClause("category_id desc");return categoryMapper.selectByExample(example);}//自动生成where@RequestMapping("/selectByName")@CrossOriginpublic List<Category> selectByName(String categoryName){CategoryExample example = new CategoryExample();example.setOrderByClause("category_id desc");//Criteria是CategoryExample中的内部类//产生where语句CategoryExample.Criteria criteria = example.or();criteria.andCategoryNameEqualTo(categoryName);return categoryMapper.selectByExample(example);}@RequestMapping("/selectAll")@CrossOriginpublic List<Category> selectAll(){System.out.println(categoryMapper);return categoryMapper.selectByExample(null);}} 4. 关联关系

Mybatis表现关联关系只有两种association(一对一)、collection(一对多),表现很简洁。

4.1 一对一

分析

代码实现

4.2 一对多

分析

代码实现

5. 参数占位符

mybatis本质就是拼接SQL语句。

#{name}
防止SQL注入;如果参数是一个字符串类型。chen 拼接SQL语句时会根据类型,自动加相关符号。例如字符串类型’chen’。${orderby}
${} 原样输出,很危险,有SQL注入风险。 6. 复杂搜索 6.1 动态SQL语句

举例:

<mapper namespace="com.tedu.mybaits02_multiTableQuery.mapper.ItemMapper"><!-- resultType=集合中数据的类型 --><select id="selectByCategoryIdAndName" parameterType="com.tedu.mybaits02_multiTableQuery.pojo.Item" resultType="com.tedu.mybaits02_multiTableQuery.pojo.Item">SELECT item_id as itemId,item_name as itemName,category_id as categoryIdfrom item<where><if test="categoryId != null">category_id=#{categoryId} </if><if test="itemName != null">AND item_name LIKE concat('%',#{itemName},'%')</if></where></select></mapper> 6.2 集合参数

foreach的主要用在构建sql中的in条件中,它可以在SQL语句中进行迭代一个集合。

属性:

collection:表示传入的参数。该属性是必须指定
主要有以下3种情况: 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可 item:表示集合中每一个元素进行迭代时的别名。open:表示该语句以什么开始close:表示该语句以什么结束separator:表示在每次进行迭代之间以什么符号作为分隔 <mapper namespace="com.tedu.mybaits02_multiTableQuery.mapper.ItemMapper"><select id="selectByList" parameterType="Integer" resultType="com.tedu.mybaits02_multiTableQuery.pojo.Item">SELECT item_id as itemId,item_name as itemName,category_id as categoryIdfrom item<where><!-- itemId in (2,3) -->item_id in <foreach collection="list" item="itemId" open="(" separator="," close=")">#{itemId}</foreach></where> </select></mapper>

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。