mybatis中注解映射SQL示例代码

本文深入解析MyBatis中的注解使用,包括@Insert、@Update、@Delete、@Select等注解的详细说明及示例代码。同时介绍了如何通过注解实现动态SQL和结果映射,以及如何定义结果映射文件来避免重复编写查询方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注解@Insert @Update @Select @ Delete
public interface StudentMapper
{
@Insert("insert into student (stud_id, name, email, addr_id, phone)values(#{studId},#{name},#{email},#{address.addrId},#{phone})")
int insertStudent(Student student);
}
public interface StudentMapper
{
@Insert("insert into student (name,email,addr_id,phone)values(#{name},#{email},#{address.addrId},#{phone})")
@Options(useGeneratedKeys=true,keyProperty="studId")
int insertStudent(Student student);
}
public interface StudentMapper
{
@Insert("insert into student (name,email,addr_id,phone)values(#{name},#{email},#{address.addrId},#{phone})")
@SelectKey(statement="select stud_id_seq.nextval from dual",keyProperty="studId",resultType=int.calss,before=true)
int insertStudent(Student student);
}
@Update("update students set name=#{name},email=#{email}")
int updateStudent(Student student);
@Delete("delete form students where stud_id=#{studId}")
int deleteStudent(int studId)
@Select("select name,email,phone from students where stud_id=#{studId}")
Student findStudentById(Integer studId);


结果注解

@Select("select name,email,phone from students where stud_id=#{studId}")
@Results({
@Result(id=true,column="stud_id",property="studId"),
@Result(column="name",property="name"),
@Result(column="email",property="email"),
@Result(column="phone",property="phone")
})

Student findStudentById(Integer studId);

结果注解有一个缺点,就是在一个查询方法前面都要写一遍,不能重用。解决这个问题方案是:

定义一份结果映射文件如下所示:


<mapper namespace="com.mybatis3.mappers.StudentMapper">
<resultMap type="Student" id="StudentResult">
.......
</resultMap>
@Select("select name,email,phone from students where stud_id=#{studId}")
@ResultMap("com.mybatis3.mappers.StudentMapper.StudentResult")
Student findStudentById(Integer studId);


动态Sql的注解

对于动态sql,mybatis提供了不同的注解,@InsertProvider @UpdateProvider @DeleteProvider @SelectProvider
用法如下所示:

首先创建一个provider类:


public class SqlProvider
{
public String findTutorById(int tutorId)
{
return "select tutorId,name,email from tutors where tutorId="+tutorId;
}
}


使用provider类:


@SelectProvider(type=SqlProvider.class,method="findTutorById")

Tutor findTutorById(int tutorId);

但是使用字符串连接创建sql语句容易出现问题,所以mybatis提供了一个SQL工具,简化了构建动态Sql的方式;


public class SqlProvider
{
public String findTutorById(int tutorId)
{
return new SQL(){{
SELECT("tutorid,name,email")
FROM("tutors")
WHERE("tutorid="+tutorId)
}}.toString();
}

}

或者

public class SqlProvider
{
public String findTutorById()
{
return new SQL(){{
SELECT("tutorid,name,email")
FROM("tutors")
WHERE("tutorid=#{tutorId}")
}}.toString();
}}


文章摘自:

www.jb51.net/article/121…



转载于:https://juejin.im/post/5c0408f5f265da617573816c

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值