You are previewing Java Persistence with MyBatis 3.
O'Reilly logo
Java Persistence with MyBatis 3

Book Description

Getting started with the MYBatis framework is greatly simplified with this step-by-step guide that’s packed with practical exercises. Now you can quickly and confidently take your Java capabilities into the arena of data persistence.

  • Detailed instructions on how to use MyBatis with XML and Annotation-based SQL Mappers

  • An in-depth discussion on how to map complex SQL query results such as One-To-Many and Many-To-Many using MyBatis ResultMaps

  • Step-by-step instructions on how to integrate MyBatis with a Spring framework

  • In Detail

    Storing and retrieving data to and from relational databases is a very common requirement, and is a crucial part of many applications. Even though Java provides a JDBC API for database access, it is not very effective and involves writing the boilerplate code again and again. Getting data out of a database, populating into Java objects, and persisting data from Java objects into a database using JDBC involves a lot of repetitive coding and is a very tedious process. MyBatis takes the simplest approach of leveraging the existing knowledge and power of Java and SQL yet provides powerful features to make data persistence implementation easy.

    Java Persistence with MyBatis 3 is a practical, hands-on guide that provides you with a number of clear step-by-step exercises, which will help you to understand how MyBatis works and how to use it for your real application needs. With MyBatis, you will learn how to use the MyBatis framework effectively through simple instructions.

    Java Persistence with MyBatis 3 will highlight the cumbersome process of data persistence using plain JDBC and will also show you how easy it is to implement the same using MyBatis. You will also take a deeper look into the implementation of MyBatis with XML and Annotation-based Mappers, and finally learn how to integrate MyBatis with a Spring framework.

    You will learn how to map Complex SQL query results to Java beans using XML and Annotation-based Mappers, including One-To-Many and Many-To-Many relationships. You will also learn how to use TypeAliases and Custom Type Handlers, and build dynamic queries using XML and SqlProvider annotations. It progresses to detailed instructions on integrating MyBatis with Spring and leveraging Spring’s Annotation-based transaction handling mechanism, which further simplifies the usage of MyBatis.With Implementing MyBatis, you will learn how to use the MyBatis framework effectively through simple instructions.

    Table of Contents

    1. Java Persistence with MyBatis 3
      1. Table of Contents
      2. Java Persistence with MyBatis 3
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers and more
          1. Why Subscribe?
          2. Free Access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Getting Started with MyBatis
        1. What is MyBatis?
        2. Why MyBatis?
          1. Eliminates a lot of JDBC boilerplate code
          2. Low learning curve
          3. Works well with legacy databases
          4. Embraces SQL
          5. Supports integration with Spring and Guice frameworks
          6. Supports integration with third-party cache libraries
          7. Better performance
        3. Installing and configuring MyBatis
          1. Creating a STUDENTS table and inserting sample data
          2. Creating a Java project and adding mybatis-3.2.2.jar to the classpath
          3. Creating the mybatis-config.xml and StudentMapper.xml configuration files
          4. Creating the MyBatisSqlSessionFactory singleton class
          5. Creating the StudentMapper interface and the StudentService classes
          6. Creating a JUnit test for testing StudentService
            1. How it works
        4. Sample domain model
        5. Summary
      9. 2. Bootstrapping MyBatis
        1. Configuring MyBatis using XML
          1. Environment
          2. DataSource
          3. TransactionManager
          4. Properties
          5. typeAliases
          6. typeHandlers
          7. Settings
          8. Mappers
        2. Configuring MyBatis using Java API
          1. Environment
          2. DataSource
          3. TransactionFactory
          4. typeAliases
          5. typeHandlers
          6. Settings
          7. Mappers
        3. Customizing MyBatis logging
        4. Summary
      10. 3. SQL Mappers Using XML
        1. Mapper XMLs and Mapper interfaces
        2. Mapped statements
          1. The INSERT statement
            1. Autogenerated keys
          2. The UPDATE statement
          3. The DELETE statement
          4. The SELECT statement
        3. ResultMaps
          1. Simple ResultMaps
          2. Extending ResultMaps
          3. One-to-one mapping
          4. One-to-one mapping using nested ResultMap
          5. One-to-one mapping using nested Select
          6. One-to-many mapping
          7. One-to-many mapping with nested ResultMap
          8. One-to-many mapping with nested select
        4. Dynamic SQL
          1. The If condition
          2. The choose, when, and otherwise conditions
          3. The where condition
          4. The trim condition
          5. The foreach loop
          6. The set condition
        5. MyBatis recipes
          1. Handling enumeration types
          2. Handling the CLOB/BLOB types
          3. Passing multiple input parameters
          4. Multiple results as a map
          5. Paginated ResultSets using RowBounds
          6. Custom ResultSet processing using ResultSetHandler
          7. Cache
        6. Summary
      11. 4. SQL Mappers Using Annotations
        1. Mapper interfaces using annotations
        2. Mapped statements
          1. @Insert
            1. Autogenerated keys
          2. @Update
          3. @Delete
          4. @Select
        3. Result maps
          1. One-to-one mapping
          2. One-to-many mapping
        4. Dynamic SQL
          1. @InsertProvider
          2. @UpdateProvider
          3. @DeleteProvider
        5. Summary
      12. 5. Integration with Spring
        1. Configuring MyBatis in a Spring application
          1. Installation
          2. Configuring MyBatis beans
        2. Working with SqlSession
        3. Working with mappers
          1. <mybatis:scan/>
          2. @MapperScan
        4. Transaction management using Spring
        5. Summary
      13. Index