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();
        }
    }
posted @ 2019-12-15 15:44  大风北吹  阅读(241)  评论(2编辑  收藏  举报