mybatis常用5种注解
mybatis 最常用的 5 种注解主要围绕 crud 操作及参数命名,分别是 @select、@insert、@update、@delete 和 @param。
- @select:用于映射查询 sql 语句,替代 xml 中的
<select>标签 。 - @insert:用于映射插入 sql 语句,替代 xml 中的
<insert>标签 。 - @update:用于映射更新 sql 语句,替代 xml 中的
<update>标签 。 - @delete:用于映射删除 sql 语句,替代 xml 中的
<delete>标签 。 - @param:用于给方法参数命名,使 sql 能通过参数名正确获取值,多参数时必用 。
此外,@results 和 @result 也较常用,用于结果集映射,但核心 crud 操作以上述 5 种最为基础 。
mybatis和mybatis-plus常用注解
一、mybatis常用注解
1. @select
注解说明:标记查询语句,用于定义查询操作的sql语句。
代码示例:
@select("select * from users where id = #{id}")
user getuserbyid(@param("id") long id);注解处理类:由org.apache.ibatis.builder.annotation.mapperannotationbuilder类负责处理,它会解析@select注解并创建相应的sqlsource和mappedstatement对象。
2. @insert
注解说明:标记插入语句,用于定义插入操作的sql语句。
代码示例:
@insert("insert into users(name, age) values(#{name}, #{age})")
int adduser(user user);
注解处理类:由org.apache.ibatis.builder.annotation.mapperannotationbuilder类负责处理,它会解析@insert注解并创建相应的sqlsource和mappedstatement对象。
3. @update
注解说明:标记更新语句,用于定义更新操作的sql语句。
代码示例:
@update("update users set name = #{name}, age = #{age} where id = #{id}")
int updateuser(user user);
注解处理类:由org.apache.ibatis.builder.annotation.mapperannotationbuilder类负责处理,它会解析@update注解并创建相应的sqlsource和mappedstatement对象。
4. @delete
注解说明:标记删除语句,用于定义删除操作的sql语句。
代码示例:
@delete("delete from users where id = #{id}")
int deleteuserbyid(@param("id") long id);
注解处理类:由org.apache.ibatis.builder.annotation.mapperannotationbuilder类负责处理,它会解析@delete注解并创建相应的sqlsource和mappedstatement对象。
5. @results
注解说明:用于指定多个@result注解,定义查询结果集的映射关系。
代码示例:
@select("select * from users where id = #{id}")
@results(id = "userresultmap", value = {
@result(property = "id", column = "id"),
@result(property = "name", column = "name"),
@result(property = "age", column = "age")
})
user getuserbyid(@param("id") long id);
注解处理类:由org.apache.ibatis.builder.annotation.mapperannotationbuilder类负责处理,它会解析@results注解并创建resultmap对象,保存到configuration中。
6. @result
注解说明:用于指定单个属性与结果集中列的映射关系。
代码示例:
@result(property = "userid", column = "id")
注解处理类:由org.apache.ibatis.builder.annotation.mapperannotationbuilder类负责处理,它会解析@result注解并创建resultmapping对象,作为resultmap的组成部分。
7. @resultmap
注解说明:用于引用已定义的结果映射集。
代码示例:
@select("select * from users where id = #{id}")
@resultmap("userresultmap")
user getuserbyid(@param("id") long id);
注解处理类:由org.apache.ibatis.builder.annotation.mapperannotationbuilder类负责处理,它会解析@resultmap注解并引用已定义的resultmap对象。
8. @param
注解说明:用于为sql语句中的参数指定名称,便于在sql中引用。
代码示例:
list<user> findbynameandage(@param("name") string name, @param("age") integer age);
注解处理类:由org.mybatis.spring.mapper.mappermethod类负责处理,它会解析@param注解并创建参数映射,用于sql语句的参数绑定。
9. @options
注解说明:提供了一系列的选项配置,如是否使用自动生成的键、是否使用缓存等。
代码示例:
@insert("insert into users(name, age) values(#{name}, #{age})")
@options(usegeneratedkeys = true, keyproperty = "id")
int adduser(user user);
注解处理类:由org.apache.ibatis.builder.annotation.mapperannotationbuilder类负责处理,它会解析@options注解并配置mappedstatement的相关属性。
10. @one
注解说明:用于一对一关联查询。
代码示例:
@select("select * from orders where id = #{id}")
@results({
@result(property = "id", column = "id"),
@result(property = "user", column = "user_id",
one = @one(select = "com.example.mapper.usermapper.getuserbyid"))
})
order getorderbyid(@param("id") long id);
注解处理类:由org.apache.ibatis.builder.annotation.mapperannotationbuilder类负责处理,它会解析@one注解并创建一对一关联的resultmapping对象。
11. @many
注解说明:用于一对多关联查询。
代码示例:
@select("select * from users where id = #{id}")
@results({
@result(property = "id", column = "id"),
@result(property = "orders", column = "id",
many = @many(select = "com.example.mapper.ordermapper.getordersbyuserid"))
})
user getuserwithorders(@param("id") long id);注解处理类:由org.apache.ibatis.builder.annotation.mapperannotationbuilder类负责处理,它会解析@many注解并创建一对多关联的resultmapping对象。
二、mybatis-plus常用注解
1. @tablename
注解说明:用于标记实体类对应的数据库表名,解决实体类名与表名不一致的问题。
代码示例:
@data
@tablename("t_user")
public class user {
private long id;
private string name;
private integer age;
private string email;
}
注解处理类:由com.baomidou.mybatisplus.core.mybatisconfiguration和com.baomidou.mybatisplus.core.metadata.tableinfohelper类负责处理,它们会解析@tablename注解并建立实体类与数据库表的映射关系。
2. @tableid
注解说明:用于标记实体类主键字段,可以指定主键类型和生成策略。
代码示例:
@tableid(value = "id", type = idtype.auto) private long id;
注解处理类:由com.baomidou.mybatisplus.core.metadata.tableinfohelper类负责处理,它会解析@tableid注解并确定主键字段及其生成策略。
idtype枚举值说明:
- auto:数据库自增
- none:不设置主键类型
- input:用户输入id
- assign_id:分配id (主要是雪花算法)
- assign_uuid:分配uuid
3. @tablefield
注解说明:用于标记实体类中的非主键字段,解决实体类属性名与表字段名不一致的问题,或者指定字段的一些属性。
代码示例:
@tablefield(value = "name", insertstrategy = fieldstrategy.not_empty) private string username;
注解处理类:由com.baomidou.mybatisplus.core.metadata.tableinfohelper类负责处理,它会解析@tablefield注解并建立实体类属性与数据库字段的映射关系。
4. @version
注解说明:用于标记乐观锁版本号字段,实现乐观锁机制。
代码示例:
@version @tablefield(value = "version", fill = fieldfill.insert) private integer version;
注解处理类:由com.baomidou.mybatisplus.extension.plugins.optimisticlockerinterceptor类负责处理,它会拦截sql执行并自动增加版本号条件,实现乐观锁机制。
5. @tablelogic
注解说明:用于标记逻辑删除字段,实现逻辑删除功能。
代码示例:
@tablelogic(value = "0", delval = "1") @tablefield(value = "deleted") private integer deleted;
注解处理类:由com.baomidou.mybatisplus.extension.plugins.inner.logicdeleteinnerinterceptor类负责处理,它会拦截sql执行并自动替换删除语句为更新逻辑删除字段的语句。
6. @enumvalue
注解说明:用于标记枚举类中的属性,指定枚举字段存储的值。
代码示例:
public enum genderenum implements ienum<integer> {
male(1, "男"),
female(2, "女");
@enumvalue
private final integer value;
private final string desc;
genderenum(integer value, string desc) {
this.value = value;
this.desc = desc;
}
@override
public integer getvalue() {
return value;
}
}注解处理类:由com.baomidou.mybatisplus.extension.handlers.enumtypehandler类负责处理,它会在java枚举类型与数据库字段值之间进行转换。
7. @keysequence
注解说明:用于指定序列生成器的名称,适用于oracle、postgresql等支持序列的数据库。
代码示例:
@keysequence(value = "seq_user", clazz = long.class)
@tablename("t_user")
public class user {
@tableid(value = "id", type = idtype.input)
private long id;
// 其他字段
}
注解处理类:由com.baomidou.mybatisplus.extension.incrementer.oraclekeygenerator或com.baomidou.mybatisplus.extension.incrementer.postgrekeygenerator等类负责处理,根据数据库类型选择相应的序列生成器。
8. @sqlparser
注解说明:用于指定sql解析器,实现sql拦截和增强功能。
代码示例:
@mapper
public interface usermapper extends basemapper<user> {
@sqlparser(using = mylogicsqlparser.class)
list<user> selectall();
}
注解处理类:由com.baomidou.mybatisplus.extension.parser.jsqlparsersupport类负责处理,它会解析sql并应用自定义的sql解析逻辑。
9. @interceptorignore
注解说明:用于指定忽略特定的拦截器,灵活控制拦截器的使用。
代码示例:
@mapper
public interface usermapper extends basemapper<user> {
@interceptorignore(tenantline = "true")
list<user> selectall();
}
注解处理类:由com.baomidou.mybatisplus.extension.plugins.mybatisplusinterceptor类负责处理,它会根据注解配置决定是否跳过特定的拦截器。
10. @mapperscan
注解说明:用于扫描mapper接口所在的包,将其注册为spring bean。
代码示例:
@springbootapplication
@mapperscan("com.example.mapper")
public class application {
public static void main(string[] args) {
springapplication.run(application.class, args);
}
}
注解处理类:由org.mybatis.spring.mapper.mapperscannerconfigurer和org.mybatis.spring.annotation.mapperscannerregistrar类负责处理,它们会扫描指定包下的mapper接口并注册为spring bean。
11. @mapper
注解说明:用于标记接口为mybatis的mapper接口,将其注册为spring bean。
代码示例:
@mapper
@repository
public interface usermapper extends basemapper<user> {
// 自定义方法
}
注解处理类:由org.apache.ibatis.annotations.mapper类负责处理,它会识别@mapper注解并将接口注册为mybatis的mapper。
12. @ds
注解说明:用于多数据源切换,指定使用的数据源。
代码示例:
@ds("slave")
@mapper
public interface usermapper extends basemapper<user> {
// 方法将使用slave数据源
}
注解处理类:由com.baomidou.dynamic.datasource.annotation.ds类负责处理,它会在方法执行前后切换数据源。
到此这篇关于mybatis常用5种注解的文章就介绍到这了,更多相关mybatis常用5种注解内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论