MyBtis 七 ——添加&&主键返回

配置文件完成添加功能

    1、编写接口方法:Mapper接口

        参数:除了id之外的所有数据

        结果:void

 void add(Brand brand);

 

    2、编写SQL语句:SQL映射文件;

    

    <insert id="add" >
insert into tb_brand( brand_name, company_name, ordered, description, status) 
VALUES (#{brandName},#{companyName},#{ordered},#{description},#{status})     
    </insert>

 

    3:执行方法,测试

复制代码
    //添加
    @Test
    public  void testAdd() throws IOException {

        int status =1;
        String companyName = "波导手机";
        String brandName = "波导";
        String description = "手机中的战斗机";
        String ordered = "1000";

        Brand brand = new Brand();
         brand.setStatus(status);
          brand.setBrandName(brandName);
         brand.setCompanyName(companyName);
         brand.setDescription(description);
         brand.setOrdered(ordered);

        //1获取sqlSessionFactory
        String resource = "mybatis-config.xml";                                 //配置文件
        InputStream inputStream = Resources.getResourceAsStream(resource);      //传入流
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);    //返回对象

        //2 获取sqlSession 对象
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //3 获取Mapper接口的代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //4 执行方法

        brandMapper.add(brand);

//        //提交事务 或者第二步参数为true
//        sqlSession.commit();
        //5 释放资源
        sqlSession.close();
    }
复制代码

主键返回

添加的同时获取返回主键的值

测试代码:

复制代码
    //添加并返回主键的值
    @Test
    public  void testAdd2() throws IOException {

        int status =1;
        String companyName = "波导手机";
        String brandName = "波导";
        String description = "手机中的战斗机";
        String ordered = "1000";

        Brand brand = new Brand();
        brand.setStatus(status);
        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);
        brand.setDescription(description);
        brand.setOrdered(ordered);

        //1获取sqlSessionFactory
        String resource = "mybatis-config.xml";                                 //配置文件
        InputStream inputStream = Resources.getResourceAsStream(resource);      //传入流
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);    //返回对象

        //2 获取sqlSession 对象
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //3 获取Mapper接口的代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //4 执行方法

        brandMapper.add(brand);

        Integer id = brand.getId();
        System.out.println(id);

        //5 释放资源
        sqlSession.close();
    }
复制代码

SQL代码,修改useGeneratedKeys和keyProperty

    <insert id="add" useGeneratedKeys="true" keyProperty="id">
insert into tb_brand( brand_name, company_name, ordered, description, status)
VALUES (#{brandName},#{companyName},#{ordered},#{description},#{status})

    </insert>

 

posted @   信2005-2赵磊  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示