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

posted @ 2017-12-27 10:52  honghj  阅读(532)  评论(0编辑  收藏  举报