Mybatis添加&&刪除&&更新


mapper

<?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.songyan.mapper.Customer">
    <insert id="insertCustomer" parameterType="customer">
        insert into tb_customer(id,username,job,phone)
        values (#{id},#{username},#{job},#{phone})
    </insert>
    <delete id="deleteCustomer" parameterType="String">
        delete from tb_customer where id=#{value}
    </delete>
    <update id="updateCustomer" parameterType="customer">
        update tb_customer set name= #{username} where id=#{id}
    </update>
</mapper>

test

    @Test 
    public void insert() throws IOException
    {
        //讀取配置信息
        String resource="applicationContext.xml";
        //根據配置文件構建sqlsessionFactory
        InputStream in=Resources.getResourceAsStream(resource);
        //通過sqlsessionFactory創建sqlsession
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession =sqlSessionFactory.openSession(); 
        //sqlsession執行sql並返回執行結果
        Customer customer=new Customer();
        customer.setId(4);
        customer.setJob("job3");
        customer.setPhone("22222");
        customer.setUsername("zhangsan");
        int num=sqlSession.insert("com.songyan.mapper.Customer.insertCustomer",customer);
        //提交事務
        sqlSession.commit();
        //關閉sqlsession
        sqlSession.close();
    }
    
    @Test 
    public void delete() throws IOException
    {
        //讀取配置信息
        String resource="applicationContext.xml";
        //根據配置文件構建sqlsessionFactory
        InputStream in=Resources.getResourceAsStream(resource);
        //通過sqlsessionFactory創建sqlsession
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession =sqlSessionFactory.openSession(); 
        //sqlsession執行sql並返回執行結果
        int num=sqlSession.delete("com.songyan.mapper.Customer.deleteCustomer",1);
        //提交事務
        sqlSession.commit();
        //關閉sqlsession
        sqlSession.close();
    }

    
    @Test 
    public void update() throws IOException
    {
        //讀取配置信息
        String resource="applicationContext.xml";
        //根據配置文件構建sqlsessionFactory
        InputStream in=Resources.getResourceAsStream(resource);
        //通過sqlsessionFactory創建sqlsession
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession =sqlSessionFactory.openSession(); 
        //sqlsession執行sql並返回執行結果
        Customer customer=new Customer();
        customer.setId(4);
        customer.setJob("job3");
        customer.setPhone("22222");
        customer.setUsername("zhaan");
        System.out.println("1111");
        int num=sqlSession.update("com.songyan.mapper.Customer.updateCustomer",customer);
        System.out.println("222");
        //提交事務
        sqlSession.commit(true);
        //關閉sqlsession
        sqlSession.close();
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM