Mybatis注解主要分为三大类:SQL语句映射,结果集映射和关系映射。
详情可参考:MyBatis常用11种注解
其中SQL语句映射包含:@Select @Insert @Updata @Delete @SelectKey
源码如下:
通过源码发现,@Select @Insert @Updata @Delete 这4个其实是一样的
(1)@Select注解只能修饰方法
(2)@Select注解的值是字符数组。
具体实现如下:
@Update("update user set name= #{name},sex = #{sex},age =#{age} where id = #{id}")
void updateUserById(User user);
如果要想实现复杂的逻辑判断,则需要使用标签,如下所示
@Select("<script> select * from t_person where id = #{id}
<when test='address !=null'> and address = #{address}
</when> </script>")
Person selectPersonById(Integer id);
@SelectKey注解:插入后,获取id的值
以mysql为例,mysql在插入一条数据后,如何能获得到这个自增id的值呢?使用select last_insert_id() 可以取到最后生成的主键。具体实现如下:
@Insert("insert into user(id,name) values(#{id},#{name})")
@Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
@SelectKey(statement = "select last_insert_id()" ,keyProperty = "id",keyColumn = "id",resultType = int.class,before = false)
public int insert(User user);
备注:before属性,默认是true,在执行插入语句之前,执行select last_insert_id()。如果设置为flase,则在插入这个语句之后,执行select last_insert_id()
神奇的是:以下4个结果都一样,就是说,不管注解哪一个,都会正确执行括号内的SQL
@Update("update user set name= #{name},sex = #{sex},age =#{age} where id = #{id}")
void updateUserById(User user);
@Insert("update user set name= #{name},sex = #{sex},age =#{age} where id = #{id}")
void updateUserById(User user);
@Delete("update user set name= #{name},sex = #{sex},age =#{age} where id = #{id}")
void updateUserById(User user);
@Selete("update user set name= #{name},sex = #{sex},age =#{age} where id = #{id}")
void updateUserById(User user);
针对这几个注解源码解析路径:
mybatis-3.5.0.jar!\org\apache\ibatis\builder\annotation\MapperAnnotationBuilder.class
中的
void parseStatement(Method method) 方法
void parseStatement(Method method) {
Class<?> parameterTypeClass = this.getParameterType(method);
LanguageDriver languageDriver = this.getLanguageDriver(method);
SqlSource sqlSource = this.getSqlSourceFromAnnotations(method, parameterTypeClass, languageDriver);
if (sqlSource != null) {
Options options = (Options)method.getAnnotation(Options.class);
String mappedStatementId = this.type.getName() + "." + method.getName();
Integer fetchSize = null;
Integer timeout = null;
StatementType statementType = StatementType.PREPARED;
ResultSetType resultSetType = null;
SqlCommandType sqlCommandType = this.getSqlCommandType(method);
boolean isSelect = sqlCommandType == SqlCommandType.SELECT;
boolean flushCache = !isSelect;
boolean useCache = isSelect;
String keyProperty = null;
String keyColumn = null;
Object keyGenerator;
if (!SqlCommandType.INSERT.equals(sqlCommandType) && !SqlCommandType.UPDATE.equals(sqlCommandType)) {
keyGenerator = NoKeyGenerator.INSTANCE;
} else {
SelectKey selectKey = (SelectKey)method.getAnnotation(SelectKey.class);
if (selectKey != null) {
keyGenerator = this.handleSelectKeyAnnotation(selectKey, mappedStatementId, this.getParameterType(method), languageDriver);
keyProperty = selectKey.keyProperty();
} else if (options == null) {
keyGenerator = this.configuration.isUseGeneratedKeys() ? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE;
} else {
keyGenerator = options.useGeneratedKeys() ? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE;
keyProperty = options.keyProperty();
keyColumn = options.keyColumn();
}
}
if (options != null) {
if (FlushCachePolicy.TRUE.equals(options.flushCache())) {
flushCache = true;
} else if (FlushCachePolicy.FALSE.equals(options.flushCache())) {
flushCache = false;
}
useCache = options.useCache();
fetchSize = options.fetchSize() <= -1 && options.fetchSize() != -2147483648 ? null : options.fetchSize();
timeout = options.timeout() > -1 ? options.timeout() : null;
statementType = options.statementType();
resultSetType = options.resultSetType();
}
String resultMapId = null;
ResultMap resultMapAnnotation = (ResultMap)method.getAnnotation(ResultMap.class);
if (resultMapAnnotation == null) {
if (isSelect) {
resultMapId = this.parseResultMap(method);
}
} else {
String[] resultMaps = resultMapAnnotation.value();
StringBuilder sb = new StringBuilder();
String[] var22 = resultMaps;
int var23 = resultMaps.length;
for(int var24 = 0; var24 < var23; ++var24) {
String resultMap = var22[var24];
if (sb.length() > 0) {
sb.append(",");
}
sb.append(resultMap);
}
resultMapId = sb.toString();
}
this.assistant.addMappedStatement(mappedStatementId, sqlSource, statementType, sqlCommandType, fetchSize, timeout, (String)null, parameterTypeClass, resultMapId, this.getReturnType(method), resultSetType, flushCache, useCache, false, (KeyGenerator)keyGenerator, keyProperty, keyColumn, (String)null, languageDriver, options != null ? this.nullOrEmpty(options.resultSets()) : null);
}
}
根据源码来看,最总都会执行SQL语句,只是在执行之前,不同的注解解析的方式方法不一样。