码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-02-21 17:44:28      阅读:60      评论:0      收藏:0      [点我收藏+]

标签:Opens   val   puts   rop   end   mys   close   sources   image   

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

技术图片

 

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

标签:Opens   val   puts   rop   end   mys   close   sources   image   

原文地址:https://www.cnblogs.com/lyh233/p/12342245.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!