MyBatis_注解式开发

一、注解式开发

mybatis的注解主要替换映射文件。

二、基础语法

  • 注解首字母大写,因为注解与类、接口是同一级别的(类同一层级的:类,接口,注解,枚举)。一个注解,后台对应着一个@interface。
  • 在同一语法单元上,同一注解只能使用一次。

三、示例:

 1 import java.util.List;
 2 import org.apache.ibatis.annotations.Delete;
 3 import org.apache.ibatis.annotations.Insert;
 4 import org.apache.ibatis.annotations.Select;
 5 import org.apache.ibatis.annotations.SelectKey;
 6 import org.apache.ibatis.annotations.Update;
 7 
 8 import com.jmu.bean.Student;
 9 
10 public interface IStudentDao {
11     @Insert(value = { "insert into student(name,age,score) values(#{name},#{age},#{score})" })
12     void insertStudent(Student student);
13 
14     @Insert("insert into student(name,age,score) values(#{name},#{age},#{score})")
15     @SelectKey(statement = "select @@identity", resultType = int.class, keyProperty = "id", before = false)
16     void insertStudentCacheId(Student student);// 插入后获取
17 
18     @Delete(value = "delete from student where id=#{XXX}")
19     void deleteStudentById(int id);
20 
21     @Update("update student set name=#{name},age=#{age},score=#{score} where id=#{id}") // 只用到一个value属性,可以不写
22     void updateStudent(Student student);
23 
24     @Select("select id,name,age,score from student")
25     List<Student> selectAllStudents();// 查询所有
26 
27     @Select("select id,name,age,score from student where id=#{JJJ}")
28     Student selectStudentById(int id); // 根据id查询
29 
30     @Select("select id,name,age,score from student where name like '%' #{XXX} '%'")
31     List<Student> selectStudentsByName(String name);// 模糊查询
32 }
com.jmu.dao.IStudentDao

转载于:https://www.cnblogs.com/hoje/p/8124645.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值