zoukankan      html  css  js  c++  java
  • MyBatis映射文件1(增删改、insert获取自增主键值)

    增删改

        Mybatis为我们提供了<insert>、<update>、<delete>标签来对应增删改操作

    在接口中写增删改的抽象方法

    1. void addEmp(Employee e);  
    2.     
    3. void updateEmp(Employee e);  
    4.     
    5. void deleteEmp(Employee e);  

    在映射文件中写sql语句

    1. <insert id="addEmp" parameterType="com.figsprite.bean.Employee">  
    2.     insert into tb_employee(last_name,email,gender)  
    3.     values(#{lastName},#{email},#{gender})  
    4. </insert>  
    5.     
    6. <update id="updateEmp" parameterType="com.figsprite.bean.Employee">  
    7.     update tb_empolyee  
    8.       set last_name=#{lastName},email=#{email},gender=#{genser}  
    9.       where id=#{id}  
    10. </update>  
    11.     
    12. <delete id="deleteEmp" parameterType="com.figsprite.bean.Employee">  
    13.     delete from tb_employee where id=#{id}  
    14. </delete>

    顾名思义,parameterType就是参数类型,而我们通过#{字段名}的方式来传递对象中的属性,resultType允许使用String、Boolean等基本类型包作为返回值

    测试

    1. @Test  
    2.     public void test3() throws IOException{  
    3.         SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();  
    4.         SqlSession openSession = sqlSessionFactory.openSession();  
    5.         Employee e = new Employee();  
    6.         e.setGender("1");  
    7.         e.setLastName("Hello");  
    8.         e.setEmail("qwewqeqw");  
    9.         e.toString();  
    10.         try {  
    11.             EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);  
    12.             mapper.addEmp(e);  
    13.             openSession.commit();  
    14.         }finally {  
    15.             openSession.close();  
    16.         }  
    17.     
    18.     }  
    1. @Test  
    2.     public void test4() throws IOException{  
    3.         SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();  
    4.         SqlSession openSession = sqlSessionFactory.openSession();  
    5.         Employee e = new Employee();  
    6.         e.setId(1);  
    7.         e.setGender("0");  
    8.         e.setEmail("qwq");  
    9.         e.setLastName("Jerry");  
    10.         try {  
    11.             EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);  
    12.             mapper.updateEmp(e);  
    13.             openSession.commit();  
    14.         }finally {  
    15.             openSession.close();  
    16.         }  
    17.     }  

    这里我们要自动提交数据,我们也可以选择SqlSession openSession = sqlSessionFactory.openSession(true); 这样就不用手动提交了。

    1. @Test  
    2. public void test5 ()throws IOException{  
    3.     SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();  
    4.     SqlSession openSession = sqlSessionFactory.openSession();  
    5.     Employee e = new Employee();  
    6.     e.setId(1);  
    7.     try{  
    8.         EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);  
    9.         mapper.deleteEmp(e);  
    10.         openSession.commit();  
    11.     }finally {  
    12.         openSession.close();  
    13.     }  

    insert获取自增主键值

        我们在上面的例子中可以看到,我们插入时候并没有指定id,因为我们id是通过自增方式,JDBC里面提供了这样的方法

    ResultSet getGeneratedKeys()

    MyBatis也支持,当然它的底层,其实就是这个函数,我们仅需在<insert>标签里添加属性即可

    useGeneratedKeys="true"

    使用自增主键获取主键值策略,接着用keyProperty这个属性,指定对应主键属性,也就是Mybatis获取到逐渐使以后将它赋给JavaBean的对应属性

    1. <insert id="addEmp" parameterType="com.figsprite.bean.Employee"  
    2.     useGeneratedKeys="true" keyProperty="id">  

    让我们做个测试

    @Test  

    public void test3() throws IOException{  

        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();  

        SqlSession openSession = sqlSessionFactory.openSession();  

        Employee e = new Employee();  

        e.setGender("1");  

        e.setLastName("Hello");  

        e.setEmail("qwewqeqw");  

        e.toString();  

        try {  

            EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);  

            mapper.addEmp(e);  

            openSession.commit();  

            System.out.println(e.getId());  

        }finally {  

            openSession.close();  

        }  

    如果我们没有写useGenerateKeys和KeyProperty,那么上述代码打印出来的将会是null

  • 相关阅读:
    JSP ——第九次作业
    JSP ——第八次作业
    JSP ——第七次作业-mail_system
    JSP ——第六次作业
    JSP——第五次作业
    软件测试——第二次
    JSP作业 四——2
    JSP 作业 四 —(1)
    JSP 第三次作业
    NSData的同步下载与NSConnection的同步下载
  • 原文地址:https://www.cnblogs.com/figsprite/p/10733000.html
Copyright © 2011-2022 走看看