JavaEE——Mybatis(3)–增删改查,以及获得自加主键的价

studentMapper.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
        <!DOCTYPE mapper
                PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
                "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="test.dao.StudentMapper">

查
<select id="getById" resultType="test.person.Students">
    select * from student where id = #{id}
</select>

插入
    <insert id="addStu" parameterType="test.person.Students">
        INSERT INTO student(name, sex, age) VALUES(#{name},#{sex},#{age})
    </insert>


更新
    <!--public void updateStu(Students student);-->
    <update id="updateStu">
        update student
        set name=#{name},sex=#{sex},age=#{age}
        where id=#{id}
    </update>

删除
    <!--public void deleteStuById(Integer id);-->
    <delete id="deleteStuById">
        delete from student where id=#{id}
    </delete>

</mapper>

  测试增删改查MyBatis.Java

  1、mybatis允许增删改直接定义以下种类返回值
    * Integer、Long、Boolean、void
  * 2、我们得手动提交数据
    * sqlSessionFactory.openSession();===》手动提交
    * sqlSessionFactory.openSession(true);===》自动提交

public void test2() throws IOException {
        String resource = "conf/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream( resource );

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build( inputStream );
        SqlSession openSession = sqlSessionFactory.openSession();

        try {

            StudentMapper studentMapper = openSession.getMapper( StudentMapper.class );

            Students student = new Students(3, "hei", "girl", 22);

            //studentMapper.addStu(student);

            //studentMapper.updateStu(student);

            studentMapper.deleteStuById(2);

            //手动提交数据
            openSession.commit();
        } finally {
            openSession.close();
        }
    }

  mysql获取自加主键的值

<!-- public void addEmp(Employee employee); -->
    <!-- parameterType:参数类型,可以省略, 
        获取自增主键的值:
        mysql支持自增主键,自增主键值的获取,mybatis也是利用statement.getGenreatedKeys();
        useGeneratedKeys="true";使用自增主键获取主键值策略
        keyProperty;指定对应的主键属性,也就是mybatis获取到主键值以后,将这个值封装给javaBean的哪个属性
    -->
    <insert id="addEmp" parameterType="com.atguigu.mybatis.bean.Employee"
        useGeneratedKeys="true" keyProperty="id" databaseId="mysql">
        insert into tbl_employee(last_name,email,gender) 
        values(#{lastName},#{email},#{gender})
    </insert>

   Oracle获取插入数据的主键

<!-- 
    获取非自增主键的值:
        Oracle不支持自增;Oracle使用序列来模拟自增;
        每次插入的数据的主键是从序列中拿到的值;如何获取到这个值;
     -->
    <insert id="addEmp" databaseId="oracle">
        <!-- 
        keyProperty:查出的主键值封装给javaBean的哪个属性
        order="BEFORE":当前sql在插入sql之前运行
               AFTER:当前sql在插入sql之后运行
        resultType:查出的数据的返回值类型

        BEFORE运行顺序:
            先运行selectKey查询id的sql;查出id值封装给javaBean的id属性
            在运行插入的sql;就可以取出id属性对应的值
        AFTER运行顺序:
            先运行插入的sql(从序列中取出新值作为id);
            再运行selectKey查询id的sql;
         -->
        <selectKey keyProperty="id" order="BEFORE" resultType="Integer">
            <!-- 编写查询主键的sql语句 -->
            <!-- BEFORE-->
            select EMPLOYEES_SEQ.nextval from dual 
            <!-- AFTER:
             select EMPLOYEES_SEQ.currval from dual -->
        </selectKey>

        <!-- 插入时的主键是从序列中拿到的 -->
        <!-- BEFORE:-->
        insert into employees(EMPLOYEE_ID,LAST_NAME,EMAIL) 
        values(#{id},#{lastName},#{email<!-- ,jdbcType=NULL -->}) 
        <!-- AFTER:
        insert into employees(EMPLOYEE_ID,LAST_NAME,EMAIL) 
        values(employees_seq.nextval,#{lastName},#{email}) -->
    </insert>

  

网站地图xml地图