7mybatis学习——基础增删改

Posted Arbitrary233

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了7mybatis学习——基础增删改相关的知识,希望对你有一定的参考价值。

一、mybatis基础增删改

sqlmapper中配置

    <insert id="addEmp" parameterType="employee">
        insert into employee(name,gender) values(#{name},#{gender})
    </insert>
    
    <update id="updateEmp">
        update employee set name=#{name},gender=#{gender}
        where id=#{id}
    </update>
    
    <delete id="deleteEmp">
        delete from employee where id = #{id}
    </delete>

mapper接口中添加相应方法

 增删改测试

    @Test
    public void testAdd() throws IOException {
        String source = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(source);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        
        //获取sqlSession实例,能直接执行映射的sql语句,获取的session不会自动提交
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        Employee employee = new Employee(null, "tom", "0");
        employeeMapper.addEmp(employee);
        sqlSession.commit();
        sqlSession.close();
    }
    
    @Test
    public void testUpdate() throws IOException {
        String source = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(source);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        
        //获取sqlSession实例,能直接执行映射的sql语句,获取的session不会自动提交
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        Employee employee = employeeMapper.selectEmpById(1);
        employee.setName("xiaohon");
        employeeMapper.updateEmp(employee);
        sqlSession.commit();
        sqlSession.close();
    }
    
    @Test
    public void testDelete() throws IOException {
        String source = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(source);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        
        //获取sqlSession实例,能直接执行映射的sql语句,获取的session不会自动提交
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        employeeMapper.deleteEmp(2);
        sqlSession.commit();
        sqlSession.close();
    }

二、mybatis获取自增主键

    <!-- 获取自增主键的值:
            mysql支持自增主键,自增主键的获取
            mybatis也是通过配置useGeneratedKeys="true"使用自增主键获取主键值策略
             keyProperty=""指定对应的主键属性,也就是mybatis获取到主键值以后,将这个值封装到javabean的属性中-->
    <insert id="addEmp" parameterType="employee" 
        useGeneratedKeys="true" keyProperty="id">
        insert into employee(name,gender) values(#{name},#{gender})
    </insert>

测试方法中查看添加对象后的属性

    @Test
    public void testAdd() throws IOException {
        String source = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(source);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        
        //获取sqlSession实例,能直接执行映射的sql语句,获取的session不会自动提交
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        Employee employee = new Employee(null, "tom", "0");  //此时id为null
        employeeMapper.addEmp(employee);
        System.out.println(employee);    //此时有id
        sqlSession.commit();
        sqlSession.close();
    }

 

以上是关于7mybatis学习——基础增删改的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis 框架学习指南 ❤️

Express实战 - 应用案例- realworld-API - 路由设计 - mongoose - 数据验证 - 密码加密 - 登录接口 - 身份认证 - token - 增删改查API(代码片段

python实现员工信息表增删改查

TP5学习基础一:增删改查小demo

增删改查学习

MySQL增删改查基础知识