测试添加功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| package com.sympa.mybatis.test;
import com.sympa.mybatis.mapper.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 org.junit.jupiter.api.Test;
import java.io.IOException; import java.io.InputStream;
public class MybatisTest {
@Test public void testMybatis() throws IOException { InputStream is = Resources.getResourceAsStream("mybatis.config.xml"); SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is); SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); int result = userMapper.insertUser(); System.out.println("result:" + result); sqlSession.commit(); } }
|
1 2 3
| <insert id="insertUser"> insert into t_user values(null, '张三', '123456', 18, '男', '123@qq.com') </insert>
|
测试查询功能
查询功能标签必须设置resultType或resultMap
- resultType:设置默认映射关系
- resultMap:设置自定义的映射关系
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package com.sympa.mybatis.test;
import com.sympa.mybatis.mapper.UserMapper; import com.sympa.mybatis.pojo.User; 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 org.junit.jupiter.api.Test;
import java.io.IOException; import java.io.InputStream; import java.util.*;
public class MybatisTest {
@Test public void testMybatis() throws IOException { InputStream is = Resources.getResourceAsStream("mybatis.config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is); SqlSession sqlSession = sqlSessionFactory.openSession(true); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> result = userMapper.getUserById(); result.forEach(user -> System.out.println(user)); } }
|
设置结果映射
1 2 3 4
| <select id="getUserById" resultType="com.sympa.mybatis.pojo.User"> select * from t_user </select>
|
封装SqlSessionUtils工具类,获取SqlSession对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.sympa.mybatis.utils;
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;
public class SqlSessionUtils {
public static SqlSession getSqlSession(){ InputStream is = null; try { is = Resources.getResourceAsStream("mybatis.config.xml"); } catch (IOException e) { e.printStackTrace(); } SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession = sqlSessionFactory.openSession(true); return sqlSession; } }
|