http://www.javacodegeeks.com/2012/11/mybatis-tutorial-crud-operations-and-mapping-relationships-part-1.html

CRUD Operations
MyBatis is an SQL Mapper tool which greatly simplifies the database programing when compared to using JDBC directly.

Step1: Create a Maven project and configure MyBatis dependencies.

<project xmlns='http://maven.apache.org/POM/4.0.0'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <groupId>com.sivalabs</groupId>
<artifactId>mybatis-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>mybatis-demo</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>

Step#2: Create the table USER and a Java domain Object User as follows:

CREATE TABLE  user (
user_id int(10) unsigned NOT NULL auto_increment,
email_id varchar(45) NOT NULL,
password varchar(45) NOT NULL,
first_name varchar(45) NOT NULL,
last_name varchar(45) default NULL,
PRIMARY KEY (user_id),
UNIQUE KEY Index_2_email_uniq (email_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
package com.sivalabs.mybatisdemo.domain;
public class User
{
private Integer userId;
private String emailId;
private String password;
private String firstName;
private String lastName; @Override
public String toString() {
return 'User [userId=' + userId + ', emailId=' + emailId
+ ', password=' + password + ', firstName=' + firstName
+ ', lastName=' + lastName + ']';
}
//setters and getters
}

Step#3: Create MyBatis configuration files.

a) Create jdbc.properties file in src/main/resources folder

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis-demo
jdbc.username=root
jdbc.password=admin

b) Create mybatis-config.xml file in src/main/resources folder

 
<?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.sivalabs.mybatisdemo.domain.User' 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='${jdbc.password}'/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource='com/sivalabs/mybatisdemo/mappers/UserMapper.xml'/>
</mappers>
</configuration>

Step#4: Create an interface UserMapper.java in src/main/java folder in com.sivalabs.mybatisdemo.mappers package.

package com.sivalabs.mybatisdemo.mappers;

import java.util.List;
import com.sivalabs.mybatisdemo.domain.User; public interface UserMapper
{ public void insertUser(User user); public User getUserById(Integer userId); public List<User> getAllUsers(); public void updateUser(User user); public void deleteUser(Integer userId); }
 

Step#5: Create UserMapper.xml file in src/main/resources folder in com.sivalabs.mybatisdemo.mappers package.

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE mapper PUBLIC '-//mybatis.org//DTD Mapper 3.0//EN'
'http://mybatis.org/dtd/mybatis-3-mapper.dtd'> <mapper namespace='com.sivalabs.mybatisdemo.mappers.UserMapper'> <select id='getUserById' parameterType='int' resultType='com.sivalabs.mybatisdemo.domain.User'>
SELECT
user_id as userId,
email_id as emailId ,
password,
first_name as firstName,
last_name as lastName
FROM USER
WHERE USER_ID = #{userId}
</select>
<!-- Instead of referencing Fully Qualified Class Names we can register Aliases in mybatis-config.xml and use Alias names. -->
<resultMap type='User' id='UserResult'>
<id property='userId' column='user_id'/>
<result property='emailId' column='email_id'/>
<result property='password' column='password'/>
<result property='firstName' column='first_name'/>
<result property='lastName' column='last_name'/>
</resultMap> <select id='getAllUsers' resultMap='UserResult'>
SELECT * FROM USER
</select> <insert id='insertUser' parameterType='User' useGeneratedKeys='true' keyProperty='userId'>
INSERT INTO USER(email_id, password, first_name, last_name)
VALUES(#{emailId}, #{password}, #{firstName}, #{lastName})
</insert> <update id='updateUser' parameterType='User'>
UPDATE USER
SET
PASSWORD= #{password},
FIRST_NAME = #{firstName},
LAST_NAME = #{lastName}
WHERE USER_ID = #{userId}
</update> <delete id='deleteUser' parameterType='int'>
DELETE FROM USER WHERE USER_ID = #{userId}
</delete> </mapper>

Step#6: Create MyBatisUtil.java to instantiate SqlSessionFactory.

 
package com.sivalabs.mybatisdemo.service;

import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MyBatisUtil
{
private static SqlSessionFactory factory; private MyBatisUtil() {
} static
{
Reader reader = null;
try {
reader = Resources.getResourceAsReader('mybatis-config.xml');
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
factory = new SqlSessionFactoryBuilder().build(reader);
} public static SqlSessionFactory getSqlSessionFactory()
{
return factory;
}
}

Step#7: Create UserService.java in src/main/java folder.

package com.sivalabs.mybatisdemo.service;

import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.sivalabs.mybatisdemo.domain.User;
import com.sivalabs.mybatisdemo.mappers.UserMapper; public class UserService
{ public void insertUser(User user) {
SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
try{
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.insertUser(user);
sqlSession.commit();
}finally{
sqlSession.close();
}
} public User getUserById(Integer userId) {
SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
try{
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
return userMapper.getUserById(userId);
}finally{
sqlSession.close();
}
} public List<User> getAllUsers() {
SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
try{
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
return userMapper.getAllUsers();
}finally{
sqlSession.close();
}
} public void updateUser(User user) {
SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
try{
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.updateUser(user);
sqlSession.commit();
}finally{
sqlSession.close();
} } public void deleteUser(Integer userId) {
SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
try{
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.deleteUser(userId);
sqlSession.commit();
}finally{
sqlSession.close();
} } }
 

Step#8: Create a JUnit Test class to test UserService methods.

package com.sivalabs.mybatisdemo;

import java.util.List;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test; import com.sivalabs.mybatisdemo.domain.User;
import com.sivalabs.mybatisdemo.service.UserService; public class UserServiceTest
{
private static UserService userService; @BeforeClass
public static void setup()
{
userService = new UserService();
} @AfterClass
public static void teardown()
{
userService = null;
} @Test
public void testGetUserById()
{
User user = userService.getUserById(1);
Assert.assertNotNull(user);
System.out.println(user);
} @Test
public void testGetAllUsers()
{
List<User> users = userService.getAllUsers();
Assert.assertNotNull(users);
for (User user : users)
{
System.out.println(user);
} } @Test
public void testInsertUser()
{
User user = new User();
user.setEmailId('test_email_'+System.currentTimeMillis()+'@gmail.com');
user.setPassword('secret');
user.setFirstName('TestFirstName');
user.setLastName('TestLastName'); userService.insertUser(user);
Assert.assertTrue(user.getUserId() != 0);
User createdUser = userService.getUserById(user.getUserId());
Assert.assertNotNull(createdUser);
Assert.assertEquals(user.getEmailId(), createdUser.getEmailId());
Assert.assertEquals(user.getPassword(), createdUser.getPassword());
Assert.assertEquals(user.getFirstName(), createdUser.getFirstName());
Assert.assertEquals(user.getLastName(), createdUser.getLastName()); } @Test
public void testUpdateUser()
{
long timestamp = System.currentTimeMillis();
User user = userService.getUserById(2);
user.setFirstName('TestFirstName'+timestamp);
user.setLastName('TestLastName'+timestamp);
userService.updateUser(user);
User updatedUser = userService.getUserById(2);
Assert.assertEquals(user.getFirstName(), updatedUser.getFirstName());
Assert.assertEquals(user.getLastName(), updatedUser.getLastName());
} @Test
public void testDeleteUser()
{
User user = userService.getUserById(4);
userService.deleteUser(user.getUserId());
User deletedUser = userService.getUserById(4);
Assert.assertNull(deletedUser); }
}
 

Now, I will explain how to perform CRUD operations using MyBatis Annotation support without need of Queries configuration in XML mapper files.
 
Step#1: Create a table BLOG and a java domain Object Blog.

CREATE TABLE  blog (
blog_id int(10) unsigned NOT NULL auto_increment,
blog_name varchar(45) NOT NULL,
created_on datetime NOT NULL,
PRIMARY KEY (blog_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
package com.sivalabs.mybatisdemo.domain;

import java.util.Date;

public class Blog {

 private Integer blogId;
private String blogName;
private Date createdOn; @Override
public String toString() {
return 'Blog [blogId=' + blogId + ', blogName=' + blogName
+ ', createdOn=' + createdOn + ']';
}
//Seeters and getters
}
 

Step#2: Create UserMapper.java interface with SQL queries in Annotations.

package com.sivalabs.mybatisdemo.mappers;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update; import com.sivalabs.mybatisdemo.domain.Blog; public interface BlogMapper
{
@Insert('INSERT INTO BLOG(BLOG_NAME, CREATED_ON) VALUES(#{blogName}, #{createdOn})')
@Options(useGeneratedKeys=true, keyProperty='blogId')
public void insertBlog(Blog blog); @Select('SELECT BLOG_ID AS blogId, BLOG_NAME as blogName, CREATED_ON as createdOn FROM BLOG WHERE BLOG_ID=#{blogId}')
public Blog getBlogById(Integer blogId); @Select('SELECT * FROM BLOG ')
@Results({
@Result(id=true, property='blogId', column='BLOG_ID'),
@Result(property='blogName', column='BLOG_NAME'),
@Result(property='createdOn', column='CREATED_ON')
})
public List<Blog> getAllBlogs(); @Update('UPDATE BLOG SET BLOG_NAME=#{blogName}, CREATED_ON=#{createdOn} WHERE BLOG_ID=#{blogId}')
public void updateBlog(Blog blog); @Delete('DELETE FROM BLOG WHERE BLOG_ID=#{blogId}')
public void deleteBlog(Integer blogId); }

Step#3: Configure BlogMapper in mybatis-config.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'/>
<environments default='development'>
<environment id='development'>
<transactionManager type='JDBC'/>
<dataSource type='POOLED'>
<!-- <property name='driver' value='com.mysql.jdbc.Driver'/>
<property name='url' value='jdbc:mysql://localhost:3306/mybatis-demo'/>
<property name='username' value='root'/>
<property name='password' value='admin'/> -->
<property name='driver' value='${jdbc.driverClassName}'/>
<property name='url' value='${jdbc.url}'/>
<property name='username' value='${jdbc.username}'/>
<property name='password' value='${jdbc.password}'/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper class='com.sivalabs.mybatisdemo.mappers.BlogMapper'/>
</mappers>
</configuration>

Step#4: Create BlogService.java

 
package com.sivalabs.mybatisdemo.service;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.sivalabs.mybatisdemo.domain.Blog;
import com.sivalabs.mybatisdemo.mappers.BlogMapper; public class BlogService
{ public void insertBlog(Blog blog) {
SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
try{
BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
blogMapper.insertBlog(blog);
sqlSession.commit();
}finally{
sqlSession.close();
}
} public Blog getBlogById(Integer blogId) {
SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
try{
BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
return blogMapper.getBlogById(blogId);
}finally{
sqlSession.close();
}
} public List<Blog> getAllBlogs() {
SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
try{
BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
return blogMapper.getAllBlogs();
}finally{
sqlSession.close();
}
} public void updateBlog(Blog blog) {
SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
try{
BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
blogMapper.updateBlog(blog);
sqlSession.commit();
}finally{
sqlSession.close();
}
} public void deleteBlog(Integer blogId) {
SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
try{
BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
blogMapper.deleteBlog(blogId);
sqlSession.commit();
}finally{
sqlSession.close();
} } }

Step#5: Create JUnit Test for BlogService methods

package com.sivalabs.mybatisdemo;

import java.util.Date;
import java.util.List; import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test; import com.sivalabs.mybatisdemo.domain.Blog;
import com.sivalabs.mybatisdemo.service.BlogService; public class BlogServiceTest
{
private static BlogService blogService; @BeforeClass
public static void setup()
{
blogService = new BlogService();
} @AfterClass
public static void teardown()
{
blogService = null;
} @Test
public void testGetBlogById()
{
Blog blog = blogService.getBlogById(1);
Assert.assertNotNull(blog);
System.out.println(blog);
} @Test
public void testGetAllBlogs()
{
List<Blog> blogs = blogService.getAllBlogs();
Assert.assertNotNull(blogs);
for (Blog blog : blogs)
{
System.out.println(blog);
} } @Test
public void testInsertBlog()
{
Blog blog = new Blog();
blog.setBlogName('test_blog_'+System.currentTimeMillis());
blog.setCreatedOn(new Date()); blogService.insertBlog(blog);
Assert.assertTrue(blog.getBlogId() != 0);
Blog createdBlog = blogService.getBlogById(blog.getBlogId());
Assert.assertNotNull(createdBlog);
Assert.assertEquals(blog.getBlogName(), createdBlog.getBlogName()); } @Test
public void testUpdateBlog()
{
long timestamp = System.currentTimeMillis();
Blog blog = blogService.getBlogById(2);
blog.setBlogName('TestBlogName'+timestamp);
blogService.updateBlog(blog);
Blog updatedBlog = blogService.getBlogById(2);
Assert.assertEquals(blog.getBlogName(), updatedBlog.getBlogName());
} @Test
public void testDeleteBlog()
{
Blog blog = blogService.getBlogById(4);
blogService.deleteBlog(blog.getBlogId());
Blog deletedBlog = blogService.getBlogById(4);
Assert.assertNull(deletedBlog);
}
}
 

MyBatis Tutorial – CRUD Operations and Mapping Relationships – Part 1---- reference的更多相关文章

  1. Tutorial - Deferred Rendering Shadow Mapping 转

    http://www.codinglabs.net/tutorial_opengl_deferred_rendering_shadow_mapping.aspx Tutorial - Deferred ...

  2. 【转载】Apache Jena TDB CRUD operations

    Apache Jena TDB CRUD operations June 11, 2015 by maltesander http://tutorial-academy.com/apache-jena ...

  3. [转]Enabling CRUD Operations in ASP.NET Web API 1

    本文转自:https://docs.microsoft.com/en-us/aspnet/web-api/overview/older-versions/creating-a-web-api-that ...

  4. CRUD Operations In ASP.NET MVC 5 Using ADO.NET

    Background After awesome response of an published by me in the year 2013: Insert, Update, Delete In ...

  5. CRUD Operations in MVC4 Using AngularJS and WCF REST Services

    Now in this article I will show how to do Create, Retrieve, Update and Delete (CRUD) operations in M ...

  6. MongoDB - MongoDB CRUD Operations

    CRUD operations create, read, update, and delete documents. Create Operations Create or insert opera ...

  7. MyBatis:CRUD功能

    在前面已经自动生成了mapper和pojo,接下来我们实现mybatis的CRUD功能,先新建service.controller层的方法. 这里的sid是一个开源的id生成类,写完后,我们还需要在启 ...

  8. Mybatis的CRUD案例

    一.Mybatis增删改查案例 上一节<Mybatis入门和简单Demo>讲了如何Mybatis的由来,工作流程和一个简单的插入案例,本节主要继上一讲完整的展示Mybatis的CRUD操作 ...

  9. 【MyBatis】MyBatis实现CRUD操作

    1.实现基本CRUD功能 使用MyBatis对数据完整的操作,也就是CRUD功能的实现.根据之前的内容,要想实现CRUD,只需要进行映射文件的配置. 范例:修改EmpMapper.xml文件,实现CR ...

随机推荐

  1. 工欲善其事必先利其器-Notepad++使用小记(Python)

    大学开始就一直使用Notepad++ 作为代码编辑器,喜欢它的简洁明了,喜欢它的个性,也喜欢它各种各样骚气的插件. 今天闲来无事,写篇文章记录一下平时使用的种种,包括但不限于个性化使用一些宏,快捷键, ...

  2. 在oj平台上练习的一些总结【转】

    程序书写过程中的一些小技巧:1. freopen(“1.txt”,”r”,stdin); //程序运行后系统自动输入此文档里面的内容(不需要进行手动输入)freopen(“1.txt”,”w”,std ...

  3. ASP.NET MVC轻教程 Step By Step 13——页面布局

    一般在一个网站中页面会使用相同的结构和元素,如果每个页面都要重复添加这些元素,不仅繁琐更会给我们后期维护带来大麻烦.所以我们采用网页模板之类的技术,将固定不变的元素放入模板,同时留下一些占位符供页面各 ...

  4. Laravel框架——Session操作

    use Session;//session的永久保存(在不过期范围内) Session::put('key', 'value'); //等同于PHP的原生session $_SESSION['key' ...

  5. Python中lstrip使用心得

    lstrip方法用来去除字符串从首位开始与之相匹配的字符.例如: a = 'c' b = 'calendar' print(b.lstrip(a)) 输出结果是 'alendar'. 之前我也一直是这 ...

  6. Arduino语言学习记录(持续更新)

    几天前某宝买了一套,这几天没工夫.今天开始学学这个“玩具”. 1.Arduino的变量数据类型: 数据类型  数据类型 RAM 范围 void keyword N/A N/A boolean 1 by ...

  7. In machine learning, is more data always better than better algorithms?

    In machine learning, is more data always better than better algorithms? No. There are times when mor ...

  8. 详解 Android 的 Activity 组件

    Activity 的生命周期 和 J2ME 的 MIDlet 一样,在 android 中,Activity 的生命周期交给系统统一管理.与 MIDlet 不同的是安装在 android 中的所有的 ...

  9. bzoj2165

    类似于bzoj1706,考虑到楼层是等价的我们令f[p,i,j]为用了2^p次电梯,从房间i到j最多上升了多少层然后从2^p很容易推到2^(p+1),类似于floyd的转移即可下面我们要用最小的电梯次 ...

  10. Jira在linux上安装与部署

    Where should JIRA 6.0.1 be installed? [/opt/atlassian/jira] /usr/local/jira Default location for JIR ...