详见:https://blog.youkuaiyun.com/a772304419/article/details/80791007
Mybatis框架下的CRUD操作,除了常用的Mapper.xml的xml方式外,还可以使用注解的方式。
不需要再写Dao层每个类对应的xml文件,直接在Dao层的方法上添加以下注解:
1. @Select,@Insert,@Update,@Delete完成常见的增删改查sql语句的映射。
2. @Options(useGeneratedKeys=true,keyProperty=”id”)
有时与@Insert注解一起使用,属useGeneratedKeys=true表示使用数据库自动增长的主键,该操作需要底层数据库的支持。keyProperty="id"表示将插入数据生成的主键设置到 user对象的id当中。
3.
@Results(
@Result(id=true,column=”id”,property=”id”),
@Result(column=”name”,property=” name”),
@Result(column=”sex”,property=” sex”),
@Result(column=”age”,property=” age”),
//关联查询@One 和 @Many
)
4. @param注解
import org.apache.ibatis.annotations.Param;
dao层 xxMapper.java
1.采用@Param的方法可有多个参数
public void abc(@Param("userName") String name,@Param("password") String passWord);
而xxMapper.xml中的#{}中的参数则是根据@Param括号中的参数来获取相应的数据
<select>
select * from testTable where username = #{userName}
</select>
2.采用@Param修饰Javabean对象
public void abc (@Param("t") TestTable testTable);
xml中采用对象点属性方式获取数据
<select>
select * from testTable where username = #{t.userName}
</select>
3.不采用@Param修饰参数
public void abc (TestTable testTable);
xml中可以直接写Javabean中的属性来获取参数
<select>
select * from testTable where username = #{userName}
</select>