JavaWeb自我学习——MyBatis练习添加

目录

一.简单添加

①直接上代码:

②运行结果:

③But   只是你以为你成功了

④如何解决呢:

⑤补充

 二.添加-主键返回

①上代码

②运行结果:


 

今天呢接着之前进行添加功能的练习。

一.简单添加

①直接上代码:

StudentMapper.xml

<!--添加-->
    <insert id="add">
        insert into student(name,id,age,address,major)
        value (#{name},#{id},#{age},#{address},#{major});
    </insert>
StudentMapper.java
//添加
    void add(Student stu);

MyBatisTest2.java

package Study.test;

import Study.Mapper.StudentMapper;
import Study.test1.Student;
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.Test;

import java.io.InputStream;

public class MyBatisTest2 {
    @Test
    public void test() throws Exception {
        //参数
        String name = "wb";
        int id = 5;
        int age = 20;
        String address = "SiChuan";
        String major = "RG";
        Student stu1 =new Student();
        stu1.setAge(age);
        stu1.setAddress(address);
        stu1.setMajor(major);
        stu1.setId(id);
        stu1.setName(name);
        //1.获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2.获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3.获取Mapper接口代理对象
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);

        //4.执行方法
        //4.添加
        System.out.println("before :");
        System.out.println(studentMapper.selectAll());
        studentMapper.add(stu1);
        System.out.println("after :");
        System.out.println(studentMapper.selectAll());
        //5.释放资源
        sqlSession.close();
    }
}

②运行结果:

 

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5oCO5LmI5omN5Y-v5Lul5Y-Y,size_14,color_FFFFFF,t_70,g_se,x_16

成功!

③But   只是你以为你成功了

数据库中并没有成功插入,因为此时事务的自动提交设置为false

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5oCO5LmI5omN5Y-v5Lul5Y-Y,size_8,color_FFFFFF,t_70,g_se,x_16

④如何解决呢:

在MyBatisTest2.java中加入一段代码

//提交事务
        sqlSession.commit();

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5oCO5LmI5omN5Y-v5Lul5Y-Y,size_11,color_FFFFFF,t_70,g_se,x_16

再次运行

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5oCO5LmI5omN5Y-v5Lul5Y-Y,size_9,color_FFFFFF,t_70,g_se,x_16

大功告成!

 

⑤补充

MyBatis事务:
openSession():默认开启事务进行增删改操作后需要使用sqlSession.commit();来手动提交事务。openSession(true):可以设置为自动提交事务(关闭事务)。


 二.添加-主键返回

在数据添加成功后,需要获取插入数据库数据的主键的值(传参时没有传主键,主键自增长)

方法在insert标签中加两个属性:

useGeneratedKeys="true" keyProperty="id"  

keyProperty对应的值为表的主键名称

①上代码

StudentMapper.xml

<insert id="add2" useGeneratedKeys="true" keyProperty="id">
    insert into student(name,age,address,major)
    value (#{name},#{age},#{address},#{major});
</insert>

StudentMapper.java

    void add2(Student stu);

MyBatisTest2.java

package Study.test;

import Study.Mapper.StudentMapper;
import Study.test1.Student;
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.Test;

import java.io.InputStream;

public class MyBatisTest2 {
    @Test
    public void test() throws Exception {
        //参数
        String name = "hqc";
        int age = 20;
        String address = "SiChuan";
        String major = "RG";
        Student stu2 =new Student();
        stu2.setAge(age);
        stu2.setAddress(address);
        stu2.setMajor(major);
        stu2.setName(name);
        //1.获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2.获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3.获取Mapper接口代理对象
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);

        //4.执行方法
        //4.添加
        System.out.println("before :");
        System.out.println(studentMapper.selectAll());
        studentMapper.add2(stu2);
        Integer id = stu2.getId();
        System.out.println("after :");
        System.out.println(studentMapper.selectAll());
        System.out.println();
        System.out.println("return Key:");
        System.out.println(id);
        //提交事务
        sqlSession.commit();
        //5.释放资源
        sqlSession.close();
    }
}

②运行结果:

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5oCO5LmI5omN5Y-v5Lul5Y-Y,size_15,color_FFFFFF,t_70,g_se,x_16

因我之前测试,id字段为自增长,id=6添加成功,导致现在直接跳过id=6。

 


今天时间不够充裕,所以到此为止,每天一篇坚持不断更!!!冲冲冲!!!

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员Lyle

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

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

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

打赏作者

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

抵扣说明:

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

余额充值