7、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接口中添加相应方法

7、mybatis学习——基础增删改

 增删改测试

@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();
    }

7、mybatis学习——基础增删改

相关推荐