mybatis常用注解有:@Select、@SelectKey、@Insert、@Update、@Delete。以及结果集三大注解:@Result、@Results、@ResultMap;除此之外还有:@One、@Many等,接下来一一介绍这些注解。
@Select注解:
@Select({"select id,username,phone from db_user where id = #{key}"})User selectUserByPrimaryKey(Long key);
查询相关的SQL写在@Select注解中,花括号里面的内容可以是字符串也可以是字符串数组。
@SelectKey:
@SelectKey(statement = "select last_insert_id()" ,keyProperty = "id",keyColumn = "id",resultType = int.class,before = false)public int insert(User user);
@Insert:
@Insert({"insert into db_user(username, password, nickname, phone, email) values (#{username}, #{password}, #{nickname}, #{phone}, #{email})"})@Options(useGeneratedKeys = true, keyProperty = "id")int insertUser(User user);
添加相关的SQL写在@Insert注解中,花括号里面的内容可以是字符串也可以是字符串数组。当添加操作需要返回自增主键时可以使用@Options注释。添加属性useGeneratedKeys = true和keyProperty = "id"即可在数据添加后获取添加数据的ID值。
@Update:
@Update({"update db_user set name = #{name} where id = #{id}"})int updateUserByPrimaryKey(User user);
修改相关的SQL写在@Update注解中,花括号里面的内容可以是字符串也可以是字符串数组。
@Delete:
@Delete({"delete from db_user where id = #{key}"})int deleteUserByPrimaryKey(Long key);
删除相关的SQL写在@Delete注解中,花括号里面的内容可以是字符串也可以是字符串数组。
结果集注解:
@Select({"select id, name, class_id from student"})@Results(id="studentMap", value={@Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),@Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),@Result(column="class_id ", property="classId", jdbcType=JdbcType.INTEGER)})List<Student> selectAll();引用结果集:@Select({"select id, name, class_id from student where id = #{id}"})@ResultMap(value="studentMap")Student selectById(integer id);
@Results和@Result注解是结合起来用的,@Result注解包含在@Results注解的value属性中。
@Results 注解
@Results 注解有两个属性,分别是id和value。其中id属性对应的是XML配置中resultMap标签的id属性,这样只要在接口中写一次就可以公用一个resultMap了。而value属性对应的是XML配置中resultMap标签下的<id>和<result>标签,<id>标签用id=true属性来确定。
@Result 注解
@Result 注解常用属性id、column和property。id属性是用来确定是否是id的,布尔类型。column属性是对应数据库字段的,字符串类型。property属性是对应JavaBean对象属性的,字符串类型。
@ResultMap 注解就一个作用,使用已经定义好的@Results或XML配置里已经写好的resultMap。里面的value属性即是@Results的id属性值或XML里resultMap的id属性值。
@One,用于一对一的关系映射:
@Select("select * from student")@Results({@Result(id=true,property="id",column="id"),@Result(property="name",column="name"),@Result(property="age",column="age"),@Result(property="address",column="address_id",one=@One(select="com.breivty.mappers.AddressMapper.getAddress"))})public List<Student> getAllStudents();
@Many,用于一对多的关系映射:
@Select("select * from t_class where id=#{id}")@Results({@Result(id=true,column="id",property="id"),@Result(column="class_name",property="className"),@Result(property="students", column="id", many=@Many(select="com.brevity.mappers.StudentMapper.getStudentsByClassId"))})public Class getClass(int id);
上面这些注解分别对应着xml文件的的标签,具体更复杂的写法就可以结合xml文件中的写法了,不过,当SQL有变化时都需要重新编译代码,一般情况下不建议使用注解方式,复杂的SQL还是建议在xml文件中编写,可读性比较强,也便于维护,唯一不足的就是需要切换文件比较麻烦哈。
如何在ruby中模拟类Java注解?(好吧,我有答案了,概括一下http://bens.me.uk/2009/java-style-annotations-in-ruby) 最佳答案 本文改编自apieceofcodeIwroteinananswertoanotherquestion几个星期前,虽然它当然不是原创的。这是一个著名的Ruby习语,毕竟它已经使用了很多年,至少从rakes的desc方法开始.moduleAnnotationsdefannotations(meth=nil)return@__annotations__[me
基于Spring注解+MyBatis+Servlet实现数据库交换的小小Demo第一步创建web项目,这一步省略,有不会的可以参考之前发布的文档第二步配置pom.xml文件dependencies>dependency>groupId>org.springframeworkgroupId>artifactId>spring-contextartifactId>version>5.2.9.RELEASEversion>dependency>dependency>groupId>org.springframeworkgroupId>artifactId>spring-aspectsartifact
特别说明:本次项目整合基于idea进行的,如果使用Eclipse可能操作会略有不同,不过总的来说不影响。springboot整合之如何选择版本及项目搭建springboot整合之版本号统一管理 springboot整合mybatis-plus+durid数据库连接池springboot整合swaggerspringboot整合mybatis代码快速生成springboot整合之统一结果返回springboot整合之统一异常处理springboot整合之Validated参数校验 springboot整合之logback日志配置springboot整合pagehelper分页springboot
我正在做这个教程:https://youtu.be/qs2n_poLarc?list=WL并且正在尝试学习ionic框架。问题是教程(根据我阅读的内容)有点过时了。视频的作者使用了import{HttpModule}from"@angular/http,但我在StackOverflow上读到我应该使用import{HttpClient}from"@angular/common/http";。问题是当我尝试编译代码时出现此错误:模块“AppModule”导入的意外值“HttpClient”。请添加@NgModule注释。。现在我不知道应该在哪里添加它,因为我的app.module.ts看
文章目录前言注意实现测试环境验证自带的注解自定义valid注解自定义注解和处理类创建参数接收类,并增加字段注解接口中使用自测环节正常测试异常测试自定义全局异常监听扩展递归参数下valid不识别的坑前言再项目开发中,针对前端传递的参数信息,有些接口中需要写大量的if判断,导致代码臃肿,不够优雅。此时,可以使用@Valid实现基本的字段校验。注意实现springboot2.3之前,直接进行开发即可,无需引用额外的依赖集成在spring-boot-starter-web中。springboot2.3之后需要额外引入spring-boot-starter-validation依赖信息测试环境sprin
目录@[TOC](目录)Service注解Service用法及示例传统方式是怎么做的呢?@Service注解是怎么体现业务逻辑复用的?总结Service注解@Service注解是SpringFramework中的一种注解,它标识了这个类是一个业务逻辑层的服务Bean。这意味着当Spring应用启动时,该Bean会被自动创建并加入到Spring应用上下文中。简而言之,@Service注解是一种用于标记服务层Bean的注解,是在SpringBoot应用中实现业务逻辑复用的重要方法之一。Service用法及示例使用@Service注解可以将一个类声明为业务逻辑组件,并将其对象存入Spring容器中。
一、环境搭建1.创建一个springboot项目(勾选web)2.导入依赖org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-testtestorg.springframework.bootspring-boot-starterorg.mybatis.spring.bootmybatis-spring-boot-starter1.3.2mysqlmysql-connector-javaruntimecom.alibabadruid1.1.12org.junit.ju
我正在设置一个kubernet集群来部署我们的容器应用程序。应用程序实际上需要所有标签,但标签超过63个字符,我得到一个error.这使我依赖于注释。服务的注释如下所示:com.example.development.london/component.proxy-config.secure-routes.backend.proxy-path。/仅用于绕过RFC域错误。在Golang应用程序中,请求命名空间的所有服务。实际上基于标签。为此,到目前为止我使用了以下代码。func(kc*KubernetesCollector)generateRoutes(errorChannelchan但是
在go中,是否可以检索结构的变量注释?考虑以下结构:typeATypestruct{IDstring`xml:"my_id"`Datestring`xml:"creation_ts"`}如何使用反射检索ID字段的xml:"my_id"部分?以下将打印变量的名称、类型和值,但不打印注释。s:=reflect.ValueOf(&aType).Elem()typeOfT:=s.Type()fori:=0;i谢谢, 最佳答案 它作为StructField.Tag可用,所以f.Tag引用资料:reflect.StructFieldreflec
谁能用简单的英语解释一下下面两段的意思?(取自http://www.ibm.com/developerworks/java/library/j-cwt08025.html)"Annotationsaremoreflexibleintermsofhowyouusethem,withoptionsforwhethertheannotationinformationistobeincludedinclassfilesoutputbythecompilerandmadeavailabletotheapplicationatruntime"不知道这些是什么意思。Annotations是否可以配置