MyBatis2

目录

主键回填

在utils文件夹下创建mybatisUtils工具类

三.事务管理

1.第一种手动提交事务,适用于操作复杂关联操作比较多

2.第二种自动提交事务,适用于简单操作(仅一个增加删除等)


主键回填

mapper中在添加数据时返回id值,useGeneratedKeys="true"表示需要回填生成的主键(即返回数据),keyProperty="stuID"表示回填的字段赋值给的属性

<insert id="insertStudent" parameterType="entity.Student" useGeneratedKeys="true" keyProperty="stuID">
    insert into tb_students(stu_num, stu_name, stu_gender, stu_age)
    values (#{stuNum}, #{stuName}, #{stuGender}, #{stuAge})
</insert>

测试: 虽然添加时id=0,但是添加后自动将生成的主键赋值给stuID属性,而不是0

 public void insertStudent() {
        try {
            //加载mybatis配置文件
            InputStream is = Resources.getResourceAsStream("mybatis_config.xml");

            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            //会话工厂
            SqlSessionFactory factory = builder.build(is);

            //与数据库的连接即mybatis的操作对象,会话
            SqlSession sqlSession = factory.openSession();

            StudentDAO studentDAO = sqlSession.getMapper(StudentDAO.class);

            Student student=new Student(0,"Y8025","悟空","女",20);
            int i=studentDAO.insertStudent(student);
//            手动提交事务
            sqlSession.commit();
            if (i==1){
                System.out.println("添加成功");
                System.out.println(student);
            }else {
                System.out.println("添加失败");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

在utils文件夹下创建mybatisUtils工具类

public class mybatisUtils {
    private static SqlSessionFactory factory;
    private static final ThreadLocal<SqlSession> local=new ThreadLocal<>();

    static {
        try {
            //加载mybatis配置文件
            InputStream is = Resources.getResourceAsStream("mybatis_config.xml");

            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            //会话工厂
            factory = builder.build(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSessionFactory getFactory(){
        return factory;
    }

    public static SqlSession getSqlSession(boolean isAutoCommit){
        SqlSession sqlSession=local.get();
        if (sqlSession==null){
            sqlSession= factory.openSession(isAutoCommit);
            local.set(sqlSession);
        }
        return sqlSession;
    }

    public static <T>T getMapper(Class<T> c){
        SqlSession sqlSession=getSqlSession(true);
        return sqlSession.getMapper(c);
    }
}

三.事务管理

SqlSession对象

  • 通过getMapper(DAO.class)方法获取mapper(DAO接口的实例)
  • 事务管理

1.第一种手动提交事务,适用于操作复杂关联操作比较多

SqlSession sqlSession = factory.openSession();创建SqlSession对象如果不声明默认为false非自动提交

  1. 每获取SqlSession对象时,就默认开启了事务,然后进行操作
  2. 操作完成后需要手动提交sqlSession.commit();
  3. 在catch中添加sqlSession.rollback();,出现异常回滚
 public void insertStudent() {

        //与数据库的连接即mybatis的操作对象,会话
        SqlSession sqlSession = mybatisUtils.getSqlSession();

        try {
            StudentDAO studentDAO = sqlSession.getMapper(StudentDAO.class);

            Student student=new Student(0,"Y8025","悟空","女",20);
            int i=studentDAO.insertStudent(student);
            //手动提交事务
            sqlSession.commit();
            if (i==1){
                System.out.println("添加成功");
                System.out.println(student);
            }else {
                System.out.println("添加失败");
            }
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
        }

    }

2.第二种自动提交事务,适用于简单操作(仅一个增加删除等)

SqlSession sqlSession = factory.openSession(true);创建SqlSession对象声明为true自动提交

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值