Foreword
By Bowen in front of a few already know how MyBatis query data in the database, now to introduce by (insert) delete (delete) change (update)
increase
Interface defines a method for increasing the binding file, the method returns the value of long, MyBatis is returned in the affected rows.
public interface MusicImp { public Long getinsertMusic(Music music); }
SQL mapping file
<?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>
Test category
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 = newMusic (9, "Shen Ning", "how many more years", null ); Long addlong = musicImp.getinsertMusic (Music); System.out.println (addlong); addSession.commit (); } a finally { / / the TODO: handle the finally clause addSession.close (); } }
delete
Interface binding document defines a delete method, the return value Boolean methods, only two Boolean value is either true or false, use this return value can determine whether the deleted successfully.
public interface MusicImp { public boolean getdelectMusic(int id); }
SQL mapping file
<?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>
Test category
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(); } }
modify
Interface binding document defines a modification method, the return value Boolean methods, only two Boolean value is either true or false, use this return value can determine whether the deleted successfully.
public interface MusicImp { public boolean getupdateMusic(@Param("musicurl")String musicurl,@Param("id")String id); }
SQL mapping file
<?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>
Test category
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(); } }