zoukankan      html  css  js  c++  java
  • 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();
        }
  • 相关阅读:
    Visio画出的图,裁剪成固定大小再添加马赛克的方法
    单张PPT转成单张PDF的PDF文件怎么设置打印出一页纸有6页PPT
    MVC下载电子表格到本地((导出表格4-4)
    获取配置文件(导出表格4-3)
    获取随机字符串(导出表格4-2)
    ExcelHelper 电子表格帮助类(导出表格4-1)
    二、操作NPOI_从数据库中导出数据到Excel_支持.xls格式
    一、操作NPOI从Excel中导入数据到SqlServer数据库中_xls格式
    Fetch(Promise微队列) 增删改查
    C#获取枚举的描述
  • 原文地址:https://www.cnblogs.com/excellencesy/p/9138880.html
Copyright © 2011-2022 走看看