<commit_before>---
layout: post
title:  "Tutorial: MyBatis using Java"
date:   2015-03-04 11:21:00
categories: java
---

[MyBatis](https://mybatis.github.io/mybatis-3/) is a widely used light-weight ORM tool. MyBatis can use XML or Java Annotations for configure all Jdbc, sql mapping and [POJOs](http://en.wikipedia.org/wiki/Plain_Old_Java_Object) to database.

![mybatis]({{ site.url }}/assets/mybatis-logo.png)

In this tutorial, I will create a simple program to demonstrate how to setup a java project with MyBatis.


## Create a MyBaits project

The tools used:

- [jdk 8.0](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html)
- [postgres 9.3](http://www.postgresql.org/download/)
- [Maven 3](http://maven.apache.org/download.cgi)
- [mybatis 3.2.8](https://mybatis.github.io/mybatis-3/)

<ol>
<li> Create database:
{% highlight sql%}
CREATE TABLE userinfo
(
  uid serial NOT NULL,
  username character varying(100) NOT NULL,
  password character varying(500) NOT NULL,
  created date,
  CONSTRAINT user_pkey PRIMARY KEY (uid),
  CONSTRAINT unq_username UNIQUE (username)
)
{% endhighlight %}
</li>
<li>
Create a maven project and setup the dependencies:
{% highlight xml %}
...
 	<dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
        </dependency>

        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.1-901.jdbc4</version>
        </dependency>
...
{% endhighlight %}
</li>
<li>
Create the <b>jdbc.properties</b> in <b>main/resources</b>
{% highlight java %}
jdbc.driverClassName=org.postgresql.Driver
jdbc.url=jdbc:postgresql://your_db_hosting_address/your_db_name
jdbc.username=yourusername
jdbc.password=<PASSWORD>
{% endhighlight%}
</li>

<li>
Setup the mybatis configuration file <b>mybatis-config.xml</b> in <b>main/resources</b>
{% highlight xml %}
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE configuration
        PUBLIC '-//mybatis.org//DTD Config 3.0//EN'
        'http://mybatis.org/dtd/mybatis-3-config.dtd'>
<configuration>
    <properties resource='jdbc.properties'/>
    <typeAliases>
        <typeAlias type='com.learnBatis.UserInfo' alias='User'></typeAlias>
    </typeAliases>
    <environments default='development'>
        <environment id='development'>
            <transactionManager type='JDBC'/>
            <dataSource type='POOLED'>
                <property name='driver' value='${jdbc.driverClassName}'/>
                <property name='url' value='${jdbc.url}'/>
                <property name='username' value='${jdbc.username}'/>
                <property name='password' value='<PASSWORD>}'/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource='mappers/UserMapper.xml'/>
    </mappers>
</configuration>
{% endhighlight %}
</li>


<li><commit_msg>Update 2015-03-03-start-mybatis.markdown<commit_after>---
layout: post
title:  "Tutorial: MyBatis using Java"
date:   2015-03-04 11:21:00
categories: java
---

[MyBatis](https://mybatis.github.io/mybatis-3/) is a widely used light-weight ORM tool. MyBatis can use XML or Java Annotations for configure all Jdbc, sql mapping and [POJOs](http://en.wikipedia.org/wiki/Plain_Old_Java_Object) to database.

![mybatis]({{ site.url }}/assets/mybatis-logo.png)

In this tutorial, I will create a simple program to demonstrate how to setup a java project with MyBatis.


## Create a MyBaits project

The tools used:

- [jdk 8.0](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html)
- [postgres 9.3](http://www.postgresql.org/download/)
- [Maven 3](http://maven.apache.org/download.cgi)
- [mybatis 3.2.8](https://mybatis.github.io/mybatis-3/)

<ol>
<li> Create database:
{% highlight sql%}
CREATE TABLE userinfo
(
  uid serial NOT NULL,
  username character varying(100) NOT NULL,
  password character varying(500) NOT NULL,
  created date,
  CONSTRAINT user_pkey PRIMARY KEY (uid),
  CONSTRAINT unq_username UNIQUE (username)
)
{% endhighlight %}
</li>
<li>
Create a maven project and setup the dependencies:
{% highlight xml %}
...
 	<dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
        </dependency>

        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.1-901.jdbc4</version>
        </dependency>
...
{% endhighlight %}
</li>
<li>
Create the <b>jdbc.properties</b> in <b>main/resources</b>
{% highlight java %}
jdbc.driverClassName=org.postgresql.Driver
jdbc.url=jdbc:postgresql://your_db_hosting_address/your_db_name
jdbc.username=yourusername
jdbc.password=<PASSWORD>
{% endhighlight%}
</li>

<li>
Setup the mybatis configuration file <b>mybatis-config.xml</b> in <b>main/resources</b>
<b>Note: we have set a alias which map UserInfo.java to User.</b>
{% highlight xml %}
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE configuration
        PUBLIC '-//mybatis.org//DTD Config 3.0//EN'
        'http://mybatis.org/dtd/mybatis-3-config.dtd'>
<configuration>
    <properties resource='jdbc.properties'/>
    <typeAliases>
        <typeAlias type='com.learnBatis.UserInfo' alias='User'></typeAlias>
    </typeAliases>
    <environments default='development'>
        <environment id='development'>
            <transactionManager type='JDBC'/>
            <dataSource type='POOLED'>
                <property name='driver' value='${jdbc.driverClassName}'/>
                <property name='url' value='${jdbc.url}'/>
                <property name='username' value='${jdbc.username}'/>
                <property name='password' value='<PASSWORD>}'/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource='mappers/UserMapper.xml'/>
    </mappers>
</configuration>
{% endhighlight %}
</li>


<li>
{'fraction_non_alphanumeric': 0.15309012160586374, 'fraction_numerical': 0.01899050474762619, 'mean_word_length': 3.3163191948238677, 'pattern_counts': {'":': 0, '<': 123, '<?xml version=': 2, '>': 123, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7136687', 'n_tokens_mistral': 2128, 'n_tokens_neox': 1928, 'n_words': 427}