13.MyBatis注解式开发

  mybatis 的注解,主要是用于替换映射文件。而映射文件中无非存放着增、删、改、查  SQL 映射标签。所以,mybatis 注解,就是要替换映射文件中的 SQL 标签。

mybatis 官方文档中指出,若要真正想发挥 mybatis 功能,还是要用映射文件。即 mybatis官方并不建议通过注解方式来使用 mybatis。

 

  注解的基础知识:

    注解的基础语法

A、注解后是没有分号的。

B、注解首字母是大写的,因为注解与类、接口是同一级别的。一个注解,后台对应着 一个@interface 类。

C、在同一语法单元上,同一注解只能使用一次。

D、在注解与语法单元之间可以隔若干空行、注释等非代码内容。

    注解的注解

打@Deprecated 源码,看到其定义上还有三个注解:@Documented、@Retention、@Target。

这三个注解的意义是:

@Target:用于指定该注解可以标注的语法类型。CONSTRUCTOR(构造器) LOCAL_VARIABLE(局部变量)、METHOD(方法)、FIELD(属性)、PACKAGE(包)、PARAMETER(参数)、TYPE(类型)。

  注解的属性注意,对于 TYPE 常量,其意义有两个:一是指该注解可以用在类、接口、枚举等类上;

  二是指该注解可以作为其它注解的属性值。例如,后面要学到的@Result@InterceptorRef 就属性第二个意思.

 

 

@Documented:用于指定该注解定义时的注释信息能否显示在 javaAPI 说明文档上。没 有添加的话,使用 javadoc 生成 API 文档时就不会该注解的信息添加到文档。

 

 

@RetentionPolicy:这是一个 enum 类型,共有三个值:SOURCE,CLASS  和 RUNTIME。

 

  SOURCE:代表这个 Annotation 类型的信息只会保留在程序源码里,源码如果经过了编 译之后,Annotation 的数据就会消失,并不会保留在编译好的.class 文件中。

 

   ClASS:代表这个 Annotation 类型的信息除了保留在程序源码里外,同时也会保留在编 译好的.class 文件里。但在执行时,并不会把这一些信息加载到虚拟机(JVM)中去。这是 Retention 的默认值。

 

   RUNTIME:表示在源码、编译好的.class  文件中保留信息,同时在执行时还会把这些信 息加载到 JVM 中。

 

  举例:@Override 中的 Retention 值为 SOURCE,编译成功了就不要这一些检查的信息。 相反@Deprecated Retention 的值为 RUNTIME,表示除了在编译时会警告我们使用了 哪个被 Deprecated 的方法,在执行的时候也可以查出该方法是否被 Deprecated。

 

  

  MyBatis注解:

  @Insert

  @SelectKey

  @Delete

  @Update

  @Select

  这些注解见名知意,用于替换原先对应的在mapper.xml中的各种标签,

  使用这写标签之后,就不需要mapper.xml映射文件了

 1 /*使用 注解 进行开发*/
 2 /*mybatis 注解 主要用于替换 映射文件(mapper.xml)  */
 3 /*官方不推荐使用 注解式开发,推荐使用配置文件的方式*/
 4 public interface StudentDAO {
 5     @Insert(value="{insert into student (age,name,score) values (#{age},#{name},#{score})}")
 6     public void insertStudent(Student s);
 7     
 8     @Insert("insert into student (age,name,score) values (#{age},#{name},#{score})")
 9     @SelectKey(statement="select @@identity",resultType=Integer.class,keyProperty="id",before=false)
10     //<selectKey resultType="int" keyProperty="id" order="AFTER">
11     public void insertStudentCatchId(Student s);   
12     
13     @Delete(value="delete from student where id = #{id}")
14     public void delecteStudent(int id);
15     
16     @Update("update student set name = #{name},age = #{age},score = #{score} where id = #{id}")
17     public void updateStudent(Student s);
18     
19     @Select("select * from student")
20     public List<Student> selectAllStudent();
21     public Map<String,Student> selectStudentMap(); 
22     
23     @Select("select * from student where id = #{id}")
24     public Student selectStudentById(int id);
25     
26     @Select("select * from student where name like '%' #{name} '%' ")
27     public List<Student> selectStudentsByName(String name);
28 
29     
30 }

 

修改主配置文件:

由于没有了映射文件,所以主配置文件中不能使用<mapper/>注册 mapper 的位置了。 需要使用<package/>标签

 

1 <!-- 注册映射文件 -->
2     <mappers>
3     <!-- 扫描指定包下,看是否有xml 文件,如果没有,看在DAO中是否有注解 -->
4         <package name="com.mybatis.dao"/>
5     </mappers>
6 </configuration>

 

posted @ 2017-08-24 20:05  白日梦想家12138  阅读(231)  评论(0编辑  收藏  举报