MyBatis教程之六注解使用详解
2017-08-10 11:08
525 查看
MyBatis支持注解的使用,可以在接口中直接使用注解进行SQL的操作。
1、@Insert
编写insert语句,实现数据的新增
2、@Update
编写update语句,实现数据的修改
3、@Delete
编写delete语句,实现数据的删除
4、@Select
编写select语句,实现数据的查询
5、@Options
参数设置 常用属性: keyProperty useGeneratedKeys statementType keyColumn timeout
6、@ResultType
标记结果集的数据类型,Class<?>,等价于xml中的resulttype
7、@Results
标记结果集的数据类型,等价于xml中的<resultmap>
8、@Result
结果集的具体的数据对应 常用属性: id property column one:一对一或多对一 many:集合,一对多 javaType:对应的java中数据类型
9、@One
实现嵌套查询,一般用于一对一或多对一的关系中 常用属性: select fetchType:抓取策略 取值: FetchType.EAGER FetchType.LAZY FetchType.EAGER
10、@Many
实现嵌套查询,一般用于一对多或多对多 常用属性: select fetchType:抓取策略 取值: FetchType.EAGER FetchType.LAZY FetchType.EAGER
代码
接口中使用注解的代码://注解式实现MyBatis public interface EmpMapper { //新增数据 @Insert("insert into emp(name,address) values(#{name},#{address})") @Options(keyProperty="id")//参数设置主要设置xml中标签中的属性 int save(Emp emp); //修改 @Update("update emp set name=#{name},address=#{address} where id=#{id}") int update(Emp emp); //删除 @Delete("delete from emp where id=#{id}") int delete(int id); //查询 @Select("select * from emp") @ResultType(Emp.class)//每行记录的数据类型 List<Emp> query(); //查询,属性和字段单独标记 @Select("select * from emp where id=#{id}") @Results(value={ @Result(id=true,property="id", column="id"), @Result(property="name",column="name"), @Result(property="address",column="address") }) Emp querySingle(int id); }
相关文章推荐
- Spring Boot教程(三十九)使用MyBatis注解配置详解(2)
- Spring Boot教程(三十八)使用MyBatis注解配置详解(1)
- DataBinding使用教程(三):各个注解详解
- Mybatis中注解@MapKey的使用详解
- Spring Boot中使用MyBatis注解配置详解
- Mybatis中动态SQL,if,where,foreach的使用教程详解
- 企业分布式微服务云SpringCloud SpringBoot mybatis (十四)Spring Boot中使用MyBatis注解配置详解
- Spring Boot中使用MyBatis注解配置详解
- Spring Boot中使用MyBatis注解配置详解
- Spring Cloud Spring Boot mybatis分布式微服务云架构(二十七)使用MyBatis注解配置详解(2)
- Spring Boot学习(九)之Spring Boot中使用MyBatis注解配置详解
- Android 注解开发 ButterKnife使用详解及教程
- 企业分布式微服务云SpringCloud SpringBoot mybatis (十四)Spring Boot中使用MyBatis注解配置详解
- MyBatis中OGNL的使用教程详解
- SpringBoot使用Mybatis注解开发教程-分页-动态sql
- JAVAWEB开发之SpringMVC详解(一)——SpringMVC的框架原理、架构简介、与mybatis整合和注解方式的使用、
- SpringCloud SpringBoot mybatis 分布式微服务(十五)Spring Boot中使用MyBatis注解配置详解
- JAVAWEB开发之SpringMVC详解(一)——SpringMVC的框架原理、架构简介、与mybatis整合和注解方式的使用、
- Mybatis用法详解(配置和注解的使用)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(二十六)使用MyBatis注解配置详解(1)