0%

使用注解方式

使用注解方式

有些人习惯于用映射文件xml方式配置,有些人习惯于使用注解的方式来编写映射语句,这里来说明一下注解的使用方式

@Insert注解

用于定义insert语句,作用等同于xml配置中标签

1
2
3
@Insert("insert  into classes (name) values (#{name})")
@Options(useGeneratedKeys = true,keyProperty = "id") // 使用@Options来设置主键自增
int insertClasses(Classes classes);

@Update注解

用于定义update语句,作用等同于xml配置中标签

1
2
@Update("update classes set name = #{name} where id = #{id}")
int updateClasses(Classes classes);

@Delete注解

用于定义delete语句,作用等同于xml配置中标签

1
2
@Delete("delete from classes where id = #{id}")
int deleteClasses(int id);

@Select注解

用于定义select语句,作用等同于xml配置中

1
2
@Select("select id,name from classes where id = #{id}")
Classes getById(int id);

@Results

作用等同于xml配置中标签

1
2
3
4
5
6
7
8
@Select("select * from classes where id = #{id}")
@Results(
{
@Result(id = true,column = "id",property = "id"),
@Result(column = "name",property = "name")
}
)
Classes get(int id);

@ResultMap注解

由于使用@Results注解需要重复的写很多相同的配置,所以可以在xml中配置resultMap,然后使用@ResultMap注解引用

1
2
3
4
5
6
<mapper namespace="com.zhanghe.study.mybatis.mapper.ClassesMapper">
<resultMap id="baseResultMap" type="com.zhanghe.study.mybatis.model.Classes">
<id column="id" property="id"/>
<result column="name" property="name"/>
</resultMap>
</mapper>
1
2
3
@Select("select * from classes where id = #{id}")
@ResultMap("com.zhanghe.study.mybatis.mapper.ClassesMapper.baseResultMap")
Classes get(int id);

注意要将该mapper.xml注册到mappers中

缓存使用注解

@CacheNamespace

相当于xml配置中的标签

@CacheNamespaceRef

相当于xml配置中的标签

级联使用注解

@One一对一关联关系

对应于xml配置中的标签

@One中的select属性会将对应column属性的值作为参数传递过去

1
2
3
4
5
6
7
8
9
10
11
@Select("select * from student where id = #{id}")
@Results(
{
@Result(id = true,property = "id",column = "id"),
@Result(property = "name",column = "name"),
@Result(property = "classes",column = "class_id",
one = @One(select = "com.zhanghe.study.mybatis.mapper.ClassesMapper.getById"))
}

)
Student getAssociationById(int id);

当然这种方式不太方便,可以使用xml方式配置resultMap,以便重复使用

1
2
3
4
5
6
7
8
<resultMap id="associaResultMap" type="com.zhanghe.study.mybatis.model.Student">
<id column="id" property="id"/>
<result column="name" property="name"/>
<association property="classes" select="com.zhanghe.study.mybatis.mapper.ClassesMapper.getById" column="class_id">
<id column="id" property="id"/>
<result column="name" property="name"/>
</association>
</resultMap>
1
2
3
 @Select("select * from student where id = #{id}")
@ResultMap("com.zhanghe.study.mybatis.mapper.StudentMapper.associaResultMap")
Student getAssociationById(int id);

@Many一对多关系映射

相当于xml配置中的标签

1
2
3
4
5
6
7
8
@Select("select * from classes where id = #{id}")
@Results({
@Result(id = true,column = "id",property = "id"),
@Result(column = "name",property = "name"),
@Result(property = "studentList",column = "id",
many = @Many(select = "com.zhanghe.study.mybatis.mapper.StudentMapper.getByClassId"))
})
Classes getColectionById(int id);

当然这种方式不太方便,可以使用xml方式配置resultMap,以便重复使用

1
2
3
4
5
6
7
8
<resultMap id="collectionResultMap" type="com.zhanghe.study.mybatis.model.Classes">
<id column="id" property="id"/>
<result column="name" property="name"/>
<collection property="studentList" column="id" select="com.zhanghe.study.mybatis.mapper.StudentMapper.getByClassId">
<id column="id" property="id"/>
<result property="name" column="name"/>
</collection>
</resultMap>
1
2
3
@Select("select * from classes where id = #{id}")
@ResultMap("com.zhanghe.study.mybatis.mapper.ClassesMapper.collectionResultMap")
Classes getColectionById(int id);
1
2
3
4
5
6
7
8
@Select("select * from student where class_id = #{classId}")
@Results(
{
@Result(id = true,column = "id",property = "id"),
@Result(column = "name",property = "name")
}
)
List<Student> getByClassId(int classId);

动态SQL

对于向xml配置中那样使用动态SQL,可以使用@SelectProvider、@DeleteProvider、@InsertProvider、@UpdateProvider来完成

以@SelectProvider为例来说明一下

@SelectProvider

使用一个简单地示例来说明一下

1
2
3
@SelectProvider(type = StudentProvider.class,method = "findById")
@ResultMap("com.zhanghe.study.mybatis.mapper.StudentMapper.baseResultMap")
Student getByIdUseProvider(int id);

在@SelectProvider注解中的type属性为自己写的一个拼接sql的工具类,method为该类所调用的方法

1
2
3
4
5
6
7
8
9
public class StudentProvider {
// 该参数为所mapper方法传过来的
// 可以无参数
// 可以与Mapper方法相同类型的参数
// 可以使用map
public String findById(int id){
return "select * from student where id = "+id;
}
}

可以在该方法中根据参数不同来拼接不同的sql语句,也可以在其中使用SQL生成器

欢迎关注我的其它发布渠道