• 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();
        }
  • 相关阅读:
    Linux2.6X内核中文件相关结构体总结
    Linux 内核文件系统与设备操作流程分析
    在linux下删除的共享文件怎么恢复
    fedora17的U盘安装和硬盘安装
    cakephp
    【25.00%】【vijos P1907】飞扬的小鸟
    【14.36%】【codeforces 614C】Peter and Snow Blower
    【14.67%】【codeforces 615D】Multipliers
    【records】10.24..10.30
    【非常高%】【codeforces 733A】Grasshopper And the String
  • 原文地址:https://www.cnblogs.com/excellencesy/p/9138880.html
Copyright © 2020-2023  润新知