学习就完事儿。
@Result
使数据库中表的字段和实体类的字段名相对应。
@Select({"select id, name, class_id from my_student"})
@Results
(id="studentMap", value=
//依次对应column="数据表字段",property="实体类字段",jdbcType="数据表字段的类型" id=true确定是主键
{@Result(column="id",property="id",jdbcType=JdbcType.INTEGER,id=true),
@Result(column="name", property="name",jdbcType=JdbcType.VARCHAR),
@Result(column="class_id ", property="classId", jdbcType=JdbcType.INTEGER)})
List selectAll();
```java
@Select({"select id, name, class_id from my_student where id = #{id}"})
//根据上面代码块里的代码可以确定 ResultMap的名字 可简化重复操作
@ResultMap(value="studentMap")Student selectById(integer id);
@Param
注解单一属性
用#{}的方式把@Param注解括号内的参数进行引用
注意:
当@param声明的是参数的时候
使用@param 用#{}或者${}都可以
当@param不是声明参数的时候
不使用@param 只能用#{}
@Select("select column from table where userid = #{userid} ")
public int selectColumn(@Param("userid") int userid);
不使用@Param注解
只有一个参数,而且还是javabean
在SQL语句里可以引用JavaBean的属性,而且只能引用JavaBean的属性。
@Options
设置缓存时间,让对象生成自增的主键值
@Insert("insert into user(username,password) values(#{username},#{password})")
//加入该注解可以保存对象后,查看对象插入id
@Options(useGeneratedKeys = true,keyProperty = "uid",keyColumn = "uid")
void regist(User user);
设置缓存暂时还没用到