Posts

Quick Start with mybatis-spring-boot-starter

  Let's create a MyBatis Spring Boot Application quickly using the   SPRING INITIALIZR . Create a project Create a Spring Boot standalone application for MyBatis + H2 Database using following command (or the  SPRING INITIALIZR UI ). $ curl -s https://start.spring.io/starter.tgz\ -d name=mybatis-sample\ -d artifactId=mybatis-sample\ -d dependencies=mybatis,h2\ -d baseDir=mybatis-sample\ -d type=maven-project\ | tar -xzvf - Create sql files Create a sql file( src/main/resources/schema.sql ) to generate the city table. CREATE TABLE city ( id INT PRIMARY KEY auto_increment, name VARCHAR , state VARCHAR , country VARCHAR ); Create a domain class Create the  City  class( src/main/java/com/example/mybatissample/City.java ). package com . example . mybatissample ; public class City { private Long id ; private String name ; private String state ; private String country ; public Lo...

Getting started

  Installation To use MyBatis you just need to include the  mybatis-x.x.x.jar  file in the classpath. If you are using Maven just add the following dependency to your pom.xml: <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>x.x.x</version> </dependency> Building SqlSessionFactory from XML 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, or from a custom prepared instance of the Configuration class. Building a SqlSessionFactory instance from an XML file is very simple. It is recommended that you use a classpath resource for this configuration, but you could use any InputStream instance, including one created from a literal file path or a  file://  URL. MyBatis includes a...