Skip to content

Commit

Permalink
feat: MyBatis demo of TypeHandler insert
Browse files Browse the repository at this point in the history
  • Loading branch information
Planeswalker23 committed May 7, 2021
1 parent bfcd88e commit a0ee102
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.walkers.planes.pandora.mybatis.common;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

/**
Expand All @@ -15,4 +16,7 @@ public interface UserMapper {

@Select("select * from user where name=#{name}")
User selectUserByName(String name);

@Insert("insert into user(id, name, region) values (#{id}, #{name}, #{region, typeHandler=io.walkers.planes.pandora.mybatis.typehandler.RegionTypeHandler})")
int insert(User user);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.walkers.planes.pandora.mybatis.typehandler;

import io.walkers.planes.pandora.mybatis.common.Region;
import io.walkers.planes.pandora.mybatis.common.User;
import io.walkers.planes.pandora.mybatis.common.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
* MyBatis demo of TypeHandler insert
*
* @author planeswalker23
*/
public class MyBatisTypeHandlerInsertDemo {

public static void main(String[] args) throws IOException {
// 加载配置文件
String resource = "quickstart/quickstart-mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 基于构建者模式构建 SqlSessionFactory 对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 基于工厂模式构建 SqlSession 对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// way2: getMapper
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setId(2);
user.setName("test_name" + user.getId());
Region region = new Region();
region.setCountry("China");
region.setArea("ZheJiang");
user.setRegion(region);
int result = mapper.insert(user);
sqlSession.commit();
System.out.println("MyBatisTypeHandlerInsertDemo result of way2: " + result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import java.io.InputStream;

/**
* MyBatis demo of TypeHandler
* MyBatis demo of TypeHandler select
*
* @author planeswalker23
*/
public class MyBatisTypeHandlerDemo {
public class MyBatisTypeHandlerSelectDemo {

public static void main(String[] args) throws IOException {
// 加载配置文件
Expand All @@ -26,7 +26,7 @@ public static void main(String[] args) throws IOException {
// 基于工厂模式构建 SqlSession 对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// way1: 调用 sqlSession.selectOne 传入 statementId(mapper全路径+.方法名) + 参数查询数据库
User user = sqlSession.selectOne("io.walkers.planes.pandora.mybatis.common.UserMapper.selectUserById", 1);
System.out.println("MyBatisTypeHandlerDemo result: " + user);
User user = sqlSession.selectOne("io.walkers.planes.pandora.mybatis.common.UserMapper.selectUserById", 2);
System.out.println("MyBatisTypeHandlerSelectDemo result: " + user);
}
}

0 comments on commit a0ee102

Please sign in to comment.