mapleins

gay

<MyBatis>入门三 sqlMapper文件

增加

  1.增删改在接口中的返回值

    Integer、Long、Boolean、void 返回影响多少行 或 true | false

  2.mapper 中 增删改没有返回值 (resultType或resultMap)

  3.mysql支持自增主键,自增主键的值的获取,mybatis利用statement.getGeneratedKey();
  userGeneratedKeys="true" :使用自增主键获取主键的策略
  keyProperty:指定对应的主键属性,也就是mybatis获取到主键值以后,将这个值封装到JavaBean中哪个属性中

  获取则是通过插入后,在查询一遍插入的对象,就可以拿到主键值
  4.sqlSessionFactory.openSession()需要手动提交事务,或者采用sqlSessionFactory.openSession(true)

public interface EmployeeMapper {


    Integer addEmp(Employee employee);

}
    <!--
        mysql支持自增主键,自增主键的值的获取,mybatis利用statement.getGeneratedKey();
        userGeneratedKeys="true" :使用自增主键获取主键的策略
        keyProperty:指定对应的主键属性,也就是mybatis获取到主键值以后,将这个值封装到JavaBean中哪个属性中
    -->
    <insert id="addEmp" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO tbl_employee(last_name, email, gender)
        VALUES (#{name}, #{email}, #{gender})
    </insert>
    @Test
    public void addEmp() throws IOException {
        //读取配置文件
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        //创建sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //获取sqlSession实例,能直接执行已经映射的sql语句
     //不会自动提交,除非在openSession(true) SqlSession sqlSession = sqlSessionFactory.openSession(); //执行查询方法 try { EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class); Employee employee = new Employee(null,"麦克雷",'1',"mapleins@qq.com"); //i判断是否插入成功 Integer i = mapper.addEmp(employee); System.out.println(i); //可以获取插入后的主键值 System.out.println(employee.getId()); sqlSession.commit(); }finally { //关闭sqlSession sqlSession.close(); } }

删除和修改

public interface EmployeeMapper {

    Long updateEmp(Employee employee);

    Boolean deleteEmpById(Integer id);

}
    <update id="updateEmp">
        UPDATE tbl_employee
        SET last_name=#{name},
            email=#{email},
            gender=#{gender}
        WHERE id = #{id}
    </update>

    <delete id="deleteEmpById">
        delete from tbl_employee where id = #{id}
    </delete>
    @Test
    public void updateEmp() throws IOException {
        //读取配置文件
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        //创建sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //获取sqlSession实例,能直接执行已经映射的sql语句
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //执行查询方法
        try {
            EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
            Employee employee = new Employee(17,"天使",'0',"mapleins@qq.com");
            Long l = mapper.updateEmp(employee);
            System.out.println(l);
            sqlSession.commit();
        }finally {
            //关闭sqlSession
            sqlSession.close();
        }
    }

    @Test
    public void deleteEmpById() throws IOException {
        //读取配置文件
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        //创建sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //获取sqlSession实例,能直接执行已经映射的sql语句
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //执行查询方法
        try {
            EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
            Boolean b = mapper.deleteEmpById(17);
            System.out.println(b);
            sqlSession.commit();
        }finally {
            //关闭sqlSession
            sqlSession.close();
        }
    }

 

posted @ 2018-12-13 10:58  mapleins  阅读(1560)  评论(0编辑  收藏  举报