本文已参与「新人创作礼」活动,一起开启掘金创作之路。
一、前言
这篇主要介绍在项目中使用mybatis遇到的一些注解,虽然不是最全的,但基本上开发够用了。
二、Mybatis注解
1、@Results
作用: 用来映射查询结果集到实体类属性。当数据库字段名与实体类对应的属性名不一致时,可以使用@Results
映射来将其对应起来。column
为数据库字段名,porperty
为实体类属性名,jdbcType
为数据库字段数据类型,id为是否为主键。
应用代码:
@Select({"select id, username, password from book"})
@Results({
@Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
@Result(column="username", property="userName", jdbcType=JdbcType.VARCHAR),
@Result(column="password ", property="passWord", jdbcType=JdbcType.VARCHAR)
})
List<Book> findAllBook();
注: 数据库字段名username与实体类属性名userName,就通过这种方式建立了映射关系。
2、 @Result
作用: @Result中常用的属性是column和property,用于配置数据库中的列名和类中的属性名之间的映射关系。
应用代码:
@Select({"select id, username, password from book"})
@Results(id="studentMap", value={
@Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
@Result(column="username", property="userName", jdbcType=JdbcType.VARCHAR),
@Result(column="password ", property="passWord", jdbcType=JdbcType.VARCHAR)
})
List<Book> findAllBook();
@Select({"select id, username, password from book where id = #{id}"})
@ResultMap(value="studentMap")
Book selectById(integer id);
3、@SelectKey
作用: 这个注解的功能与 <selectKey>
标签完全一致。该注解只能在 @Insert
或 @InsertProvider
或 @Update
或 @UpdateProvider
标注的方法上使用,否则将会被忽略。如果标注了 @SelectKey
注解,MyBatis 将会忽略掉由 @Options
注解所设置的生成主键或设置(configuration)属性。
应用代码:
这个例子展示了如何使用 @SelectKey
注解来在插入前读取数据库序列的值:
keyProperty
: 指定作为参数传入的对象对应属性的名称,该属性将会更新成新的值,
before
: 可以指定为 true
或 false
以指明 SQL 语句应被在插入语句的之前还是之后执行。
resultType
: 则指定 keyProperty
的 Java 类型。
@Insert("insert into table3 (id, name) values(#{nameId}, #{name})")
@SelectKey(statement="call next value for TestSequence", keyProperty="nameId", before=true, resultType=int.class)
int insertTable3(Name name);
4、@MapperScan
作用: 指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类,是在Springboot启动类上面添加。
应用代码:
@SpringBootApplication
@MapperScan("com.winter.Book")
public class SpringbootMybatisDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisDemoApplication.class, args);
}
}
5、@Result
作用: 在列和属性或字段之间的单个结果映射。属性:id
、column
、javaType
、jdbcType
、typeHandler
、one
、many
。id 属性和 XML 元素 <id>
相似,它是一个布尔值,表示该属性是否用于唯一标识和比较对象。one 属性是一个关联,和 <association>
类似,而 many 属性则是集合关联,和 <collection>
类似。这样命名是为了避免产生名称冲突。
代码应用:
@Results(id = "userResult", value = {
@Result(property = "id", column = "uid", id = true),
@Result(property = "firstName", column = "first_name"),
@Result(property = "lastName", column = "last_name")
})
@Select("select * from users where id = #{id}")
User getUserById(Integer id);
6、@Mapper
作用: 添加了@Mapper注解之后这个接口在编译时会生成相应的实现类,不需要在spring配置中设置扫描地址,通过mapper.xml里面的namespace属性对应相关的mapper类,spring将动态的生成Bean后注入到ServiceImpl中,一般用在查询数据的接口上,标记是一个映射接口。
代码应用:
@Mapper
public interface UserDAO {
public User findAll(); //其中findAll为xml文件中数据查询语句的id名
}
7、@Param
作用: 如果你的映射方法接受多个参数,就可以使用这个注解自定义每个参数的名字。否则在默认情况下,除 RowBounds
以外的参数会以 "param" 加参数位置被命名。例如 #{param1}
, #{param2}
。如果使用了 @Param("person")
,参数就会被命名为 #{person}
。
应用代码:
@Mapper
public interface UserDAO {
public int selectColumn(@Param("userid") int userid);
}
8、@Insert
9、@Update
10、@Delete
注:8、9、10合起来说明,增加、修改、删除。
作用: 每个注解分别代表将会被执行的 SQL 语句。它们用字符串数组(或单个字符串)作为参数。如果传递的是字符串数组,字符串数组会被连接成单个完整的字符串,每个字符串之间加入一个空格。这有效地避免了用 Java 代码构建 SQL 语句时产生的“丢失空格”问题。当然,你也可以提前手动连接好字符串。属性:value
,指定用来组成单个 SQL 语句的字符串数组。
应用代码:
@Select("select * from user where id = #{id}")
User findById(@Param("id") long id);
11、@ResultMap
作用: 这个注解为 @Select
或者 @SelectProvider
注解指定 XML 映射中 <resultMap>
元素的 id。这使得注解的 select 可以复用已在 XML 中定义的 ResultMap。如果标注的 select 注解中存在 @Results
或者 @ConstructorArgs
注解,这两个注解将被此注解覆盖。
应用代码:
@Results(id="userMap", value={
@Result(column="id", property="id", id=true),
@Result(column="user_name", property="userName"),
@Result(column="user_password ", property="userPassword"),
})
@Select({"select * from sys_user"})
@ResultMap("userMap")
List<SysUser> selectUsers();
上一篇:Lombok核心注解总结(二)
下一篇:Swagger核心注解总结(四)
记得给我三连哦!有什么问题欢迎和我交流。或者关注我的微信公众号码上言
获得更多的学习。