MyBatis系列 MyBatis 增删改

Posted yogouo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis系列 MyBatis 增删改相关的知识,希望对你有一定的参考价值。

前言

通过前几张的博文已经知道MyBatis是如何查询数据库中的数据,现在来介绍增(insert)删(delete)改(update)

增加

接口绑定文件定义一个增加方法,方法的返回值为long,在MyBatis中返回的是受影响行数。

public interface MusicImp {
    public Long getinsertMusic(Music music);
}

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="com.mybatis.dao.MusicImp">
    
    <insert id="getinsertMusic" parameterType="com.mybatis.bean.Music">
        INSERT INTO test
        (id,name,music,musicurl)
        VALUES
        (#{id},#{name},#{music},#{musicurl});
    </insert>
    
</mapper>

测试类

public void test() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //获取SqlSession对象
        SqlSession addSession = sqlSessionFactory.openSession();
        try {
            MusicImp musicImp = addSession.getMapper(MusicImp.class);
            Music music = new Music(9,"沈宁","还有多少个十年",null);
            long addlong =musicImp.getinsertMusic(music);
            System.out.println(addlong);
            addSession.commit();
        } finally {
            // TODO: handle finally clause
            addSession.close();
        }
    }

 

删除

接口绑定文件定义一个删除方法,方法的返回值为Boolean,Boolean只有两个值要么为真要么为假,使用此返回值可以判断是否删除成功。

public interface MusicImp {
    public boolean getdelectMusic(int id);
}

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="com.mybatis.dao.MusicImp">
    
    <delete id="getdelectMusic" parameterType="com.mybatis.bean.Music">
        delete from test
        where id = #{id};
    </delete>
    
</mapper>

测试类

    public void test() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //获取SqlSession对象
        SqlSession delectSession = sqlSessionFactory.openSession();
        try {
            MusicImp musicImp = delectSession.getMapper(MusicImp.class);
            boolean deleteboolean = musicImp.getdelectMusic(7);
            System.out.println(deleteboolean);
            delectSession.commit();
        } finally {
            // TODO: handle finally clause
            delectSession.close();
        }
    }

 

修改

接口绑定文件定义一个修改方法,方法的返回值为Boolean,Boolean只有两个值要么为真要么为假,使用此返回值可以判断是否删除成功。

public interface MusicImp {
    public boolean getupdateMusic(@Param("musicurl")String musicurl,@Param("id")String id);
}

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="com.mybatis.dao.MusicImp">
    
    <update id="getupdateMusic" parameterType="com.mybatis.bean.Music">
        update test set musicurl = #{musicurl}
        where id = #{id};
    </update>
    
</mapper>

测试类

    public void test() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //获取SqlSession对象
        SqlSession updateSession = sqlSessionFactory.openSession();
        try {
            MusicImp musicImp = updateSession.getMapper(MusicImp.class);
            boolean updatemusic = musicImp.getupdateMusic
                    ("http://music.163.com/song/media/outer/url?id=392897.mp3", "5");
            if (updatemusic) {
                System.out.println("更新成功");
            } else {
                System.out.println("更新失败");
            }
            
            updateSession.commit();
        } finally {
            updateSession.close();
        }
    }

以上是关于MyBatis系列 MyBatis 增删改的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis增删改查(步骤详细,由浅入深,适合初学者,只看这一篇就够了)

Mybatis从入门到精通系列 13——基于注解配置的增删改查

springboot&mybatis 增删改查系列

springboot&mybatis 增删改查系列

Java之MyBatisIDEA版(一篇文章精通系列)增删改查 - 所有知识点(大全)

mybatis怎么进行增删改查