首页 > 其他分享 >MyBtis 七 ——添加&&主键返回

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

时间:2022-09-05 20:23:38浏览次数:62  
标签:status MyBtis ordered String brand sqlSession && 主键 description

配置文件完成添加功能

    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>

 

标签:status,MyBtis,ordered,String,brand,sqlSession,&&,主键,description
From: https://www.cnblogs.com/zhaolei0419/p/16659439.html

相关文章