MyBatis核心配置文件及mapper配置等

本文档详细介绍了MyBatis的核心配置文件,包括环境设置、数据源配置以及Mapper映射。展示了如何获取SqlSession对象,以及映射SQL配置文件的写法。同时,给出了测试案例,涵盖了增删查改操作,强调了事务的管理和提交。
摘要由CSDN通过智能技术生成

MyBatis核心配置文件

<?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>
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="org/mybatis/example/BlogMapper.xml"/>
  </mappers>
</configuration>

获取SqlSession对象

public class MybatisUtil {
    private static SqlSessionFactory sqlSessionFactory ;
    static{
        try {
            //MyBatis核心配置文件路径
            String resource = "mybatis-config.xml";
            //文件流
            InputStream inputStream = Resources.getResourceAsStream(resource);
            //获取sqlSessionFactory对象
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //通过sqlSessionFactory对象获取SqlSession
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }


}

映射的 SQL 配置文件

<?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="org.mybatis.example.BlogMapper">
  <select id="selectBlog" resultType="Blog">
    select * from Blog where id = #{id}
  </select>
</mapper>

测试案例

public class UserDaoTest {
    @Test
    public void test(){
        //第一步获取sqlSession对象
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        try {
            //执行sql
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            //获取结果
            List<User> userList = mapper.getUserList();
            //遍历结果
            for (User user : userList) {
                System.out.println(user);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //关闭sqlSession
            sqlSession.close();
        }
    }

    @Test
    public void getUserById(){
        //第一步获取sqlSession对象
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        try {
            //执行sql
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            //获取结果
            User userById = mapper.getUserById(1);
            //输出结果
            System.out.println(userById);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //关闭sqlSession
            sqlSession.close();
        }
    }

    @Test
    public void addUser(){
        //第一步获取sqlSession对象
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        try {
            //执行sql
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            //获取结果
            int conut = mapper.addUser(new User(4, "牛牛", "1212"));
            if(conut >0)System.out.println("添加成功");
            //提交事务 ,这里必须手动提交事务,不然添加不成功 ,增删改都需要提交事务
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {

            //关闭sqlSession
            sqlSession.close();
        }
    }

    @Test
    public void updateUser(){
        //第一步获取sqlSession对象
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        try {
            //执行sql
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            //获取结果
            int conut = mapper.updateUser(new User(4, "龙龙", "9999"));
            if(conut >0)System.out.println("修改OK");
            //提交事务 ,这里必须手动提交事务,不然添加不成功
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //关闭sqlSession
            sqlSession.close();
        }
    }

    @Test
    public void deleteUser(){
        //第一步获取sqlSession对象
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        try {
            //执行sql
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            //获取结果
            int conut = mapper.deleteUser(4);
            if(conut >0)System.out.println("删除成功");
            //提交事务 ,这里必须手动提交事务,不然添加不成功
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {

            //关闭sqlSession
            sqlSession.close();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卢同学908

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值