JavaWeb自我学习——MyBatis练习修改删除

目录

一.修改

1)修改全部字段

①上代码

运行结果:

2)修改动态字段(不多说就是用if标签)

①问题:(问题代码我就不演示了,之前的文章里有讲解,看了就明白)

②解决方法

③代码

④运行结果:

二.删除

1)删除一个

①代码

②运行结果:

2)批量删除(foreach标签)

①代码

② 运行结果:


今天一定要把MyBatis练习给完结了。

一.修改

1)修改全部字段

①上代码

 StudentMapper.java

//修改
    int update(Student stu);

StudentMapper.xml

<!--    修改-->
    <update id="update">
        update student
        set name    = #{name},
            age     = #{age},
            address = #{address},
            major   = #{major}
        where id = #{id};
    </update>

MyBatisTest3.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 MyBatisTest3 {
    @Test
    public void test() throws Exception {
        //参数
        String name = "nnn";
        int age = 21;
        String address = "HuNan";
        String major = "JK";
        int id = 2;
        Student stu3 =new Student();
        stu3.setId(id);
        stu3.setAge(age);
        stu3.setAddress(address);
        stu3.setMajor(major);
        stu3.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());
        int count = studentMapper.update(stu3);
        System.out.println("after :");
        System.out.println(studentMapper.selectAll());
        System.out.println();
        //输出影响行数
        System.out.println("Effected row number:");
        System.out.println(count);
        //提交事务
        sqlSession.commit();
        //5.释放资源
        sqlSession.close();
    }
}

运行结果:

2)修改动态字段(不多说就是用if标签)

①问题:(问题代码我就不演示了,之前的文章里有讲解,看了就明白)

1.set中最后一个参数没有设置,以逗号结束而报错

2.一个参数也没有设置时,set多余

②解决方法

使用同查询时where标签功能类似的set标签

③代码

StudentMapper.xml

<!--    修改-->
    <update id="update">
        update student
        <set>
            <if test="name != null and name !=''">
                name = #{name},
            </if>
            <if test="age != null">
                age = #{age},
            </if>
            <if test="address != null and address !=''">
                address = #{address},
            </if>
            <if test="major != null and major !=''">
                major = #{major}
            </if>
        </set>
        where id = #{id};
    </update>

StudentMapper.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 MyBatisTest3 {
    @Test
    public void test() throws Exception {
        //参数
        String name = "nnn";
        int age = 20;
        int id = 2;
        Student stu3 =new Student();
        stu3.setId(id);
        stu3.setName(name);
        stu3.setAge(age);
        //stu3.setAddress(address);
        //stu3.setMajor(major);
        //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());
        int count = studentMapper.update(stu3);
        System.out.println("after :");
        System.out.println(studentMapper.selectAll());
        System.out.println();
        System.out.println("Effected row number:");
        System.out.println(count);
        //提交事务
        sqlSession.commit();
        //5.释放资源
        sqlSession.close();
    }
}

④运行结果:


二.删除

1)删除一个

①代码

 StudentMapper.java

//删除
    void deleteById(int id);

StudentMapper.xml

<delete id="deleteById">
    delete from student where id = #{id}
</delete>

MyBatisTest3.java

package Study.test;

import Study.Mapper.StudentMapper;
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 MyBatisTest3 {
    @Test
    public void test() throws Exception {
        //参数
        int id = 2;

        //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.deleteById(id);
        System.out.println("after :");
        System.out.println(studentMapper.selectAll());
        //提交事务
        sqlSession.commit();
        //5.释放资源
        sqlSession.close();
    }
}

②运行结果:

2)批量删除(foreach标签)

foreach标签

        mybatis会将数组参数,封装为一个Map集合。
                默认:array = 数组

                <foreach collection="array " 
                在接口定义方法时使用@Param注解改变map集合的默认key的名称

                就是要么写array,要么写@Param定义的名称。

        其他属性:item="id" separator="," open="(" close=")"

                以id为元素,"("开始,")"结束,以”,“为分隔符

                组成(id,id,id...)的语句

①代码

 StudentMapper.java

//批量删除
    void deleteByIds(@Param("ids") int[] ids);

 StudentMapper.xml

<delete id="deleteByIds">
    delete from student
    where id in
    <foreach collection="ids" item="id" separator="," open="(" close=")">
        #{id}
    </foreach>
</delete>

MyBatisTest3.java

package Study.test;

import Study.Mapper.StudentMapper;
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 MyBatisTest3 {
    @Test
    public void test() throws Exception {
        //参数
        int[] ids = {1,4};
        //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.deleteByIds(ids);
        System.out.println("after :");
        System.out.println(studentMapper.selectAll());
        //提交事务
        sqlSession.commit();
        //5.释放资源
        sqlSession.close();
    }
}

② 运行结果:

大功告成!

 啊哈哈哈,MyBatis增删改查练习到此结束!!!

明天见!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员Lyle

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

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

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

打赏作者

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

抵扣说明:

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

余额充值