`

转载一篇: MyBatis 的 Annotations使用案例

阅读更多

Getting Started with iBatis (MyBatis): Annotations

This tutorial will walk you through how to setup iBatis (MyBatis) in a simple Java project and will present examples using simple insert, update, select and delete statements using annotations.

This is the third tutorial of the iBatis/MyBatis series, you can read the first 2 tutorials on the following links:

  1. Introduction to iBatis (MyBatis), An alternative to Hibernate and JDBC
  2. Getting Started with iBatis (MyBatis): XML Configuration

iBatis/ MyBatis 3 offers a new feature: annotations. But it lacks examples and documentation about annotations. I started to explore and read the MyBatis mailing list archive to write this tutorial.

Another thing you will notice is the limitation related to annotations. I am going to demonstrate some examples that you  can do with annotations in this tutorial. All the power of iBatis is in its XMl configuration.

So let’s play a little bit with annotations. It is much simpler and you can use it for simple queries in small projects. As I already mentioned, if you want something more complex, you will have to use XML configuration.

One more thing before we get started: this tutorial is the same as the previous one, but instead of XML, we are going to use annotations.

Pre-Requisites

For this tutorial I am using:

IDE: Eclipse (you can use your favorite one)
DataBase: MySQL
Libs/jars: MybatisMySQL conector and JUnit (for testing)

This is how your project should look like:

Sample Database

Please run this script into your database before getting started with the project implementation:

I will not post the sample database here again. You can get it from the previous post about iBatis or download this sample project. The files are the same.

1 – Contact POJO

We will create a POJO class first to respresent a contact with id, name, phone number and email address – same as previous post.

2 – ContactMapper

In this file, we are going to set up all the queries using annotations. It is the MyBatis-Interface for the SQLSessionFactory.

 

A mapper class is simply an interface with method definitions that match up against the SqlSession methods.  Mapper interfaces do not need to implement any interface or extend any class. As long as the method signature can be used to uniquely identify a corresponding mapped statement.  Mapper interfaces can extend other interfaces. Be sure that you have the statements in the appropriate namespace when using XML binding to Mapper interfaces. Also, the only limitation is that you cannot have the same method signature in two interfaces in a hierarchy (a bad idea anyway).

A mapperclass is simply an interface with method definitions that match up against the SqlSession methods.

You can create some strings with the SQL code. Remember it has to be the same code as in XML Configuration.

 

01.package com.loiane.data;
02.  
03.import java.util.List;
04.  
05.import org.apache.ibatis.annotations.Delete;
06.import org.apache.ibatis.annotations.Insert;
07.import org.apache.ibatis.annotations.Options;
08.import org.apache.ibatis.annotations.Param;
09.import org.apache.ibatis.annotations.Result;
10.import org.apache.ibatis.annotations.Results;
11.import org.apache.ibatis.annotations.Select;
12.import org.apache.ibatis.annotations.Update;
13.  
14.import com.loiane.model.Contact;
15.  
16.public interface ContactMapper {
17.  
18.    final String SELECT_ALL = "SELECT * FROM CONTACT";
19.    final String SELECT_BY_ID = "SELECT * FROM CONTACT WHERE CONTACT_ID = #{id}";
20.    final String UPDATE = "UPDATE CONTACT SET CONTACT_EMAIL = #{email}, CONTACT_NAME = #{name}, CONTACT_PHONE = #{phone} WHERE CONTACT_ID = #{id}";
21.    final String UPDATE_NAME = "UPDATE CONTACT SET CONTACT_NAME = #{name} WHERE CONTACT_ID = #{id}";
22.    final String DELETE = "DELETE FROM CONTACT WHERE CONTACT_ID = #{id}";
23.    final String INSERT = "INSERT INTO CONTACT (CONTACT_EMAIL, CONTACT_NAME, CONTACT_PHONE) VALUES (#{name}, #{phone}, #{email})";
24.  
25.    /**
26.     * Returns the list of all Contact instances from the database.
27.     * @return the list of all Contact instances from the database.
28.     */
29.    @Select(SELECT_ALL)
30.    @Results(value = {
31.        @Result(property="id", column="CONTACT_ID"),
32.        @Result(property="name", column="CONTACT_NAME"),
33.        @Result(property="phone", column="CONTACT_PHONE"),
34.        @Result(property="email", column="CONTACT_EMAIL")
35.    })
36.    List<Contact> selectAll();
37.  
38.    /**
39.     * Returns a Contact instance from the database.
40.     * @param id primary key value used for lookup.
41.     * @return A Contact instance with a primary key value equals to pk. null if there is no matching row.
42.     */
43.    @Select(SELECT_BY_ID)
44.    @Results(value = {
45.        @Result(property="id"),
46.        @Result(property="name", column="CONTACT_NAME"),
47.        @Result(property="phone", column="CONTACT_PHONE"),
48.        @Result(property="email", column="CONTACT_EMAIL")
49.    })
50.    Contact selectById(int id);
51.  
52.    /**
53.     * Updates an instance of Contact in the database.
54.     * @param contact the instance to be updated.
55.     */
56.    @Update(UPDATE)
57.    void update(Contact contact);
58.  
59.    /**
60.     * Updates an instance of Contact in the database.
61.     * @param name name value to be updated.
62.     * @param id primary key value used for lookup.
63.     */
64.    void updateName(@Param("name") String name, @Param("id") int id);
65.  
66.    /**
67.     * Delete an instance of Contact from the database.
68.     * @param id primary key value of the instance to be deleted.
69.     */
70.    @Delete(DELETE)
71.    void delete(int id);
72.  
73.    /**
74.     * Insert an instance of Contact into the database.
75.     * @param contact the instance to be persisted.
76.     */
77.    @Insert(INSERT)
78.    @Options(useGeneratedKeys = true, keyProperty = "id")
79.    void insert(Contact contact);
80.}

@Select

The @Select annotation is very simple. Let’s take a look at the first select statment of this class: selectAll. Note that you don’t need to use the mapping if your database table columns mach the name of the class atributes. I used different names to use the @Result annotation. If table columns match with atribute names, you don’t need to use the @Result annotation.

Not let’s take a look on the second method: selectById. Notice that we have a parameter. It is a simple parameter – easy to use when you have a single parameter.

@Update

Let’s say you want to update all the columns. You can pass the object as parameter and iBatis will do all the magic for you. Remember to mach the parameter name with the atribute name, otherwise iBatis can get confused,

Now let’s say you want to use 2 or 3 paramaters, and they don’t belong to an object. If you take a look at iBatis XML configuration you will see you have to set the option parameterType (remember?) and specify you parameter type in it, in another words, you can use only one parameter. If you are using annotation, you can use more than one parameter using the @Param annotation.

@Delete

The @Delete annotation is also very simple. It follows the previous rules related to parameters.

@Insert

The @Insert annotation also follows the rules related to parameters. You can use an object or use the @Param annotation to specify more than one parameter.

What about the generation key? Well, if your database supports auto generation key, you can set it up using annotations with the @Options annotation. You will need to specify the option useGeneratedKeys and keyProperty. If your database does not support auto generation key, sorry, but I still did not figure out how to do it (in last case, use can do it manually and then pass as parameter to your insert query).

3 – MyBatisConnectionFactory

Every MyBatis application centers around an instance of SqlSessionFactory. A SqlSessionFactory instance can be acquired by using the SqlSessionFactoryBuilder. SqlSessionFactoryBuilder can build a SqlSessionFactory instance from an XML configuration file, of from a custom prepared instance of the Configuration class.

An observation about this file: on the previous example, we set the mappers on the iBatis Configuration XML file. Using annotations, we will set the mapper manually. In a future example, I’ll show you how to work with XML and Annotations together.

 

01.package com.loiane.dao;
02.  
03.import java.io.FileNotFoundException;
04.import java.io.IOException;
05.import java.io.Reader;
06.  
07.import org.apache.ibatis.io.Resources;
08.import org.apache.ibatis.session.SqlSessionFactory;
09.import org.apache.ibatis.session.SqlSessionFactoryBuilder;
10.  
11.import com.loiane.data.ContactMapper;
12.  
13.public class MyBatisConnectionFactory {
14.  
15.    private static SqlSessionFactory sqlSessionFactory;
16.  
17.    static {
18.  
19.        try {
20.  
21.            String resource = "SqlMapConfig.xml";
22.            Reader reader = Resources.getResourceAsReader(resource);
23.  
24.            if (sqlSessionFactory == null) {
25.                sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
26.  
27.                sqlSessionFactory.getConfiguration().addMapper(ContactMapper.class);
28.            }
29.        }
30.  
31.        catch (FileNotFoundException fileNotFoundException) {
32.            fileNotFoundException.printStackTrace();
33.        }
34.        catch (IOException iOException) {
35.            iOException.printStackTrace();
36.        }
37.    }
38.  
39.    public static SqlSessionFactory getSqlSessionFactory() {
40.  
41.        return sqlSessionFactory;
42.    }
43.  
44.}

4 – ContactDAO

Now that we set up everything needed, let’s create our DAO. To call the sql statments, we need to do one more configuration, that is to set and get the mapper from the SqlSessionFactory. Then we just need to call the mapper method. It is a little bit different, but it does the same thing.

001.package com.loiane.dao;
002.  
003.import java.util.List;
004.  
005.import org.apache.ibatis.session.SqlSession;
006.import org.apache.ibatis.session.SqlSessionFactory;
007.  
008.import com.loiane.data.ContactMapper;
009.import com.loiane.model.Contact;
010.  
011.public class ContactDAO {
012.  
013.    private SqlSessionFactory sqlSessionFactory;
014.  
015.    public ContactDAO(){
016.        sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory();
017.    }
018.  
019.    /**
020.     * Returns the list of all Contact instances from the database.
021.     * @return the list of all Contact instances from the database.
022.     */
023.    public List<Contact> selectAll(){
024.  
025.        SqlSession session = sqlSessionFactory.openSession();
026.  
027.        try {
028.  
029.            ContactMapper mapper = session.getMapper(ContactMapper.class);
030.            List<Contact> list = mapper.selectAll();
031.  
032.            return list;
033.        } finally {
034.            session.close();
035.        }
036.    }
037.  
038.    /**
039.     * Returns a Contact instance from the database.
040.     * @param id primary key value used for lookup.
041.     * @return A Contact instance with a primary key value equals to pk. null if there is no matching row.
042.     */
043.    public Contact selectById(int id){
044.  
045.        SqlSession session = sqlSessionFactory.openSession();
046.  
047.        try {
048.  
049.            ContactMapper mapper = session.getMapper(ContactMapper.class);
050.            Contact list = mapper.selectById(id);
051.  
052.            return list;
053.        } finally {
054.            session.close();
055.        }
056.    }
057.  
058.    /**
059.     * Updates an instance of Contact in the database.
060.     * @param contact the instance to be updated.
061.     */
062.    public void update(Contact contact){
063.  
064.        SqlSession session = sqlSessionFactory.openSession();
065.  
066.        try {
067.  
068.            ContactMapper mapper = session.getMapper(ContactMapper.class);
069.            mapper.update(contact);

相关推荐

    技巧:MyBatis 中的trim标签,好用!.zip

    mybatis 技巧:MyBatis 中的trim标签,好用!.zip技巧:MyBatis 中的trim标签,好用!.zip技巧:MyBatis 中的trim标签,好用!.zip技巧:MyBatis 中的trim标签,好用!.zip技巧:MyBatis 中的trim标签,好用!.zip...

    mybatis-plus最新代码生成器项目源码 :mybatis-plus-generator.zip

    mybatis-plus最新代码生成器项目源码 :mybatis-plus-generator.zip mybatis-plus最新代码生成器项目源码 :mybatis-plus-generator.zip mybatis-plus最新代码生成器项目源码 :mybatis-plus-generator.zip ...

    mybatis-paginator-1.2.15-API文档-中文版.zip

    赠送jar包:mybatis-paginator-1.2.15.jar; 赠送原API文档:mybatis-paginator-1.2.15-javadoc.jar; 赠送源代码:mybatis-paginator-1.2.15-sources.jar; 赠送Maven依赖信息文件:mybatis-paginator-1.2.15.pom;...

    MyBatisDemo:MyBatis 框架知识案例

    1.创建第一个 MyBatis 程序案例 2.MyBatis 配置文件案例 3.MyBatis 关联查询与懒加载案例 4.MyBatis 缓存案例 5.MyBatis 动态 SQL 标签案例 6.MyBatis Generator 、批量操作与分页查询案例 7.MyBatis 自定义 type...

    eclipse中使用xml自动提示功能所需文件:mybatis-3-mapper.dtd

    eclipse中使用xml自动提示功能所需文件:mybatis-3-mapper.dtd eclipse中使用xml自动提示功能所需文件:mybatis-3-mapper.dtd eclipse中使用xml自动提示功能所需文件:mybatis-3-mapper.dtd

    mybatis-3.4.5

    MyBatis 3.4.5 是一个流行的Java持久层框架,它简化了数据库操作,提供了灵活的SQL映射和对象关系映射(ORM)功能。这个版本是MyBatis系列的一个稳定版本,包含了对先前版本的改进和新特性。 在MyBatis 3.4.5中,...

    探索MyBatis的增强神器:MyBatis-Plus的神秘面纱

    MyBatis 是一款流行的持久层框架,用于简化数据库操作。它允许开发者通过简单的 XML 或注解来配置和映射 SQL 语句,将 Java 对象(POJO)映射到数据库记录。MyBatis 的主要特点包括: 1. **自定义 SQL 支持**:...

    mybatis-plus-boot-starter-3.5.1-API文档-中文版.zip

    赠送jar包:mybatis-plus-boot-starter-3.5.1.jar; 赠送原API文档:mybatis-plus-boot-starter-3.5.1-javadoc.jar; 赠送源代码:mybatis-plus-boot-starter-3.5.1-sources.jar; 赠送Maven依赖信息文件:mybatis-...

    mybatis-3.5.9-API文档-中文版.zip

    Maven坐标:org.mybatis:mybatis:3.5.9; 标签:mybatis、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,...

    Mybatis课程实验报告及源码,实验名称:Mybatis完成一对一关系映射(可不用修改直接使用)

    “Mybatis课程实验报告及源码-实验名称:Mybatis完成一对一关系映射(可不用修改直接使用).zip” 是一份关于Mybatis框架实验的报告,附带了完整的源代码和示例项目。以下是对这份实验报告的详细描述: 这份实验...

    mybatis-3.5.9.zip

    MyBatis是一个优秀的Java持久层框架,它支持定制化SQL、存储过程以及高级映射。MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。MyBatis可以使用简单的XML或注解进行配置和原始映射,将接口和Java的...

    mybatis 3.2.6

    标签"mybatis"进一步确认了该压缩包与MyBatis框架有关,这可能是一个开发人员为了方便下载和使用MyBatis而创建的。 压缩包内的文件名称"mybatis-3.2.6.zip"表示这个文件是MyBatis 3.2.6的压缩文件。要使用这个文件...

    mybatis-spring-2.0.0-API文档-中文版.zip

    Maven坐标:org.mybatis:mybatis-spring:2.0.0; 标签:mybatis、spring、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码...

    mybatis-3.5.3-API文档-中文版.zip

    Maven坐标:org.mybatis:mybatis:3.5.3; 标签:mybatis、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,...

    mybatis-3.5.7-API文档-中文版.zip

    Maven坐标:org.mybatis:mybatis:3.5.7; 标签:mybatis、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,...

    MyBatis和Spring整合jar:mybatis-3.2.7.jar等

    MyBatis和Spring的整合是Java开发中常见的一种技术组合,它们可以无缝集成,使得数据库操作更为便捷。这里提到的两个jar包——mybatis-3.2.7.jar和mybatis-spring-1.2.2.jar,是实现这种整合的关键组件。 1. **...

    mybatis-3.4.6-API文档-中文版.zip

    Maven坐标:org.mybatis:mybatis:3.4.6; 标签:mybatis、jar包、java、API文档、中文版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构...

    mybatis-3.4.5-API文档-中英对照版.zip

    Maven坐标:org.mybatis:mybatis:3.4.5; 标签:mybatis、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持...

    mybatis-3.5.7-API文档-中英对照版.zip

    Maven坐标:org.mybatis:mybatis:3.5.7; 标签:mybatis、jar包、java、中英对照文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持...

    mybatis-generator-core-1.3.7-API文档-中文版.zip

    赠送jar包:mybatis-generator-core-1.3.7.jar; 赠送原API文档:mybatis-generator-core-1.3.7-javadoc.jar; 赠送源代码:mybatis-generator-core-1.3.7-sources.jar; 赠送Maven依赖信息文件:mybatis-generator-...

Global site tag (gtag.js) - Google Analytics