MyBatis学习之注解式开发

news/2025/3/21 13:39:31/

文章目录

  • @Insert
  • @Delete
  • @Update
  • @Select
  • @Results

mybatis中也提供了注解式开发方式,采用注解可以减少Sql语句的维护带来的成本

原则:简单sql可以注解,复杂sql使用xml

@Insert

// CarMapper.java
@Insert("insert into t_car values(null,#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})")
int insert(Car car);
//test文件
@Test
public void test(){SqlSession sqlSession = SqlSessionUtil.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);Car car = new Car(null,null,null,null,null,null,null);int count = mapper.insert(car);System.out.println(count);sqlSession.commit();sqlSession.close();
}

@Delete

@Delete( "delete from t_car where id = #{id}")
int deleteById(Long id);
//test文件
@Test
public void test2(){SqlSession sqlSession = SqlSessionUtil.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);int count = mapper.deleteById(5L);System.out.println(count);sqlSession.commit();sqlSession.close();
}

@Update

@Update("updeate t_car set car_num=#{carNum} , brand=#{brand} , guide_price=#{guidePrice} produce_time=#{produceTime} , car_type=#(carType} where id=#{id}" )
int update(Car car);
//test文件
@Test
public void test3(){SqlSession sqlSession = SqlSessionUtil.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);Car car = new Car(6L,null,null,null,null,null,null);int count = mapper.update(car);System.out.println(count);sqlSession.commit();sqlSession.close();
}

@Select

@select( "select * from t_car where id =#{id}")
Car selectById(Long id);
//test文件
@Test
public void test4(){SqlSession sqlSession = SqlSessionUtil.openSession();CarMapper mapper = sqlSession.getMapper(CarMapper.class);int count = mapper.select(5L);System.out.println(count);sqlSession.commit();sqlSession.close();
}

@Results

@Select( "select * from t_car where id = #{id}")
@Results({@Result(property = "id" , column = "id"),@Result(property = "carNum",column = "car_num"),@Result(property = "brand" , column = "brand")@Result(property = "guidePrice" , column = "guide_price"),@Result(property = "produceTime" , column = "produce_time"),@Result(property = "carType", column = "car_type ")
})
Car selectById(Long id);

http://www.ppmy.cn/news/985399.html

相关文章

Vue中Router使用History和Hash模式的比较

在Vue Router中,有两种常见的路由模式:History模式和Hash模式。它们都有各自的优势和适用场景,下面将对它们进行比较和解释。 1. Hash模式 在Hash模式下,URL中的hash值(#后面的部分)会被用来模拟路由&…

系统架构设计师 11:未来信息综合技术

本章花了很多笔墨来写各项技术的发展历程,可以了解一下。 一、信息物理系统 信息物理系统(Cyber-Physical Systems,CPS)是控制系统、嵌入式系统的扩展与延伸。 CPS典型的应用场景有:健康管理、智能维护、远程征兆性…

输入一行字符,统计其中的单词个数(标志变量法和数组跳过法)

输入一行字符&#xff0c;统计其中单词的个数&#xff0c;单词之间使用空格分开。 #include<stdio.h> int main() {//输入一行字符&#xff0c;统计其中有多少个单词&#xff0c;单词之间用空格符分隔开char c;int word0,lastchar1;printf("请输入一行字符&#xf…

哈工大计算机网络课程局域网详解之:交换机概念

哈工大计算机网络课程局域网详解之&#xff1a;交换机概念 文章目录 哈工大计算机网络课程局域网详解之&#xff1a;交换机概念以太网交换机&#xff08;switch&#xff09;交换机&#xff1a;多端口间同时传输交换机转发表&#xff1a;交换表交换机&#xff1a;自学习交换机互…

openssl3.1.1关于国标支持的验证笔记

openssl3.1.1关于国标支持的验证笔记 openssl的版本差异日志 openssl虽然有3个大分支&#xff0c;我们就以3.1大分支查看关于国密的差异日志。 Changes between 1.1.0i and 1.1.1 [11 Sep 2018] Changes between 1.1.1b and 1.1.1c [28 May 2019] Changes between 1.1.1k and…

github上下载的vscode extension报找不到“vscode“模块的问题

问题来自于下载的 vscode 扩展源代码包 import * as vscode from "vscode"; 有问题的是这句&#xff0c;找不到 vscode 模块&#xff0c;错误代码 2307 主要是因为下载的源码工程里&#xff0c;没有 node_modules 这个文件夹&#xff0c;因为它本来就是需要被忽略…

Python补充笔记4-面向对象

目录 一、编程思想​ 二、类与对象​ 三、类的创建​ 四、对象的创建​ 五、类属性、类方法、静态方法​ 六、动态绑定属性和方法​ 七、知识点总结 八、面向对象的三大特征 1.封装 2.继承​ 3.多态 九、方法重写 十、object类 十一、特殊方法和特殊属性 1.dict/len/add​ 2.…

vue3-Cannot use ‘in‘ operator to search for ‘path‘ in undefined

在创建vue3的路由时&#xff0c;报了这样的错&#xff1a;Cannot use ‘in’ operator to search for ‘path’ in undefined&#xff0c;经过多次排查发现是我在路由文件里面用错了createWebHashHistory()方法&#xff0c;将它用成了 变量。 一、报错情况 二、报错原因及解…