1. 程式人生 > >MyBatis(3)-映射文件

MyBatis(3)-映射文件

close 運行 ioe 進行 ssi 配置 接口類 ont HERE

本次博文有疑問,請先看MyBatis(1)-簡單入門 和 MyBatis(2)-全局配置文件!

如在有疑問,請留言或者咨詢博主,博主每天都在!謝謝!

映射文件:

主要是在xxxmapper.xml文件裏的配置

映射文件指導這MyBatis如何進行數據庫增刪該查,有著很重要的意義。

下面開始進入正題,最後會發一個完整版的代碼

因為實現的主要內容是增刪改查,所以我們現在接口類中定義好相關的方法,後面不在進行單一的定義!

工程的目錄如下:

技術分享圖片

EmployeeMapper.java

package com.MrChengs.dao;

import java.util.Map;

import
org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import com.MrChengs.bean.Employee; public interface EmployeeMapper {

  //我們直接傳入map public Employee getByMap(Map<String,Object> map); //查詢滿足兩個條件的數據 public Employee getByIdAndNames(@Param("id")int id,@Param("name")String name);
//查詢 public Employee getEmployee(int id); //增加 public void addEmp(Employee employee); //更新 public void updateEmp(Employee employee); //刪除 public void deleteById(int id); }

1)insert-映射插入語句

1.1)EmployeeMapper.xml

  <insert id="addEmp" >

             insert into test(name,gender,email) values(#{name},#{gender},#{email})

     
</insert>
1.2)在測試類中MyBatisTest.java

   @Test
     public void test2() throws IOException{
           //1.得到SqlSeesionFactory
           SqlSessionFactory sessionFactory = getSqlSessionFactory();
           //2.得到SqlSession
           SqlSession sqlSession = sessionFactory.openSession();
           try{
                EmployeeMapper mapper = (EmployeeMapper) sqlSession.getMapper(EmployeeMapper.class);
                //手動添加數據:
                Employee em = new Employee( "zhangsan", "boy", "[email protected]");
                mapper.addEmp(em);
                //手動提交數據
                sqlSession.commit();
           }finally{
                sqlSession.close(); }}

1.3)測試成功

2)update映射更新語句

2.1)EmployeeMapper.xml

 <update id="updateEmp">
           update test set name=#{name},gender=#{gender} where id=#{id}
     </update>

2.2)測試類中

   @Test
     public void test3() throws IOException{
           //1.得到SqlSeesionFactory
           SqlSessionFactory sessionFactory = getSqlSessionFactory();
           //2.得到SqlSession
           SqlSession sqlSession = sessionFactory.openSession();
           try{
                EmployeeMapper mapper = (EmployeeMapper) sqlSession.getMapper(EmployeeMapper.class);
                //手動添加數據:
                Employee em = new Employee( 6,"Jerry", "gril", null);
                mapper.updateEmp(em);
                //手動提交數據
                sqlSession.commit();
           }finally{
                sqlSession.close();}}

2.3)測試成功!

3)delete-映射刪除操作

3.1)EmployeeMapper.xml

     <delete id="deleteById">
           delete from test where id=#{id}
     </delete>

3.2)測試類中

 @Test
     public void test4() throws IOException{
           //1.得到SqlSeesionFactory
           SqlSessionFactory sessionFactory = getSqlSessionFactory();
           //2.得到SqlSession
           SqlSession sqlSession = sessionFactory.openSession();
           try{
                EmployeeMapper mapper = (EmployeeMapper) sqlSession.getMapper(EmployeeMapper.class);
                //手動添加數據:
                mapper.deleteById(6);
                //手動提交數據
                sqlSession.commit();
           }finally{
                sqlSession.close();}}

3.3)測試成功!

4)關於主鍵的值問題

有時候需要用到主鍵值的問題,所以我們需要去學習這個知識點!

4.1)EmployeeMapper.xml

    <!-- useGeneratedKeys="true"使用自增主鍵獲取主鍵的值 -->
    <!-- keyProperty=""接受對應的主鍵值-->
    <insert id="addEmp" useGeneratedKeys="true"  keyProperty="id">
        insert into test(name,gender,email) values(#{name},#{gender},#{email})
    </insert>

4.2)測試類

  @Test
    public void test2() throws IOException{
        //1.得到SqlSeesionFactory
        SqlSessionFactory sessionFactory = getSqlSessionFactory();
        //2.得到SqlSession
        SqlSession sqlSession = sessionFactory.openSession();
        try{
            EmployeeMapper mapper = (EmployeeMapper) sqlSession.getMapper(EmployeeMapper.class);
            //手動添加數據:
            Employee em = new Employee( "zhangsan", "boy", "[email protected]");
            mapper.addEmp(em);
            //獲取主鍵值的屬性值進行打印
            System.out.println(em.getId());
            //手動提交數據
            sqlSession.commit();
        }finally{
            sqlSession.close();
        }
    }

4.3)測試成功

5)參數處理

5.1)單個參數:MyBatis不會做任何處理

   #{參數名} : 取出參數值

5.1.1)xml配置文件中

<select id="getId" resultType="com.MrChengs.bean.Employee">
        select id,name,gender,email from test where id = #{idabc}
    </select>

5.1.2)測試類

@Test
    public void test() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);        
        SqlSessionFactory sqlSessionFactory = (SqlSessionFactory) new 
                SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        try{
            Employee employee =  session.selectOne("getId",5);
            System.out.println(employee);
        }finally{
            session.close();
        }
    }

5.1.3)測試是成功的!

5.2)多個參數時

MyBatis會做特殊的處理

多個參數時封裝成map

key param1 ,param2......

value 傳入的值

5.2.1)接口類中的查詢代碼設計如上所展示的

public Employee getByIdAndNames(@Param("id")int id,@Param("name")String name);
此時去掉@param:
public Employee getByIdAndNames(int id,String name);

5.2.2) xml文件

<select id="getByIdAndNames" resultType="com.MrChengs.bean.Employee" >
        select id,name,gender,email from test where id = #{id} and name=#{name}
 </select>

5.2.3)測試代碼

  @Test
    public void test5() throws IOException{
                SqlSessionFactory sessionFactory = getSqlSessionFactory();
                SqlSession sqlSession = sessionFactory.openSession();
                try{
                    EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
                    Employee employee = mapper.getByIdAndNames(5, "MrChengs");
                    System.out.println(employee);
                }finally{
                    sqlSession.close();
                }
    }

5.2.4)此時會報錯

技術分享圖片

5.2.5)處理方案:是在EmployeeMapper.xml文件中修改的

<!-- 同時使用兩個參數進行數據庫的查詢 -->
    <!-- public Employee getByIdAndNames(int id,String name); -->
    <select id="getByIdAndNames" resultType="com.MrChengs.bean.Employee" >
        select id,name,gender,email from test where id = #{param1} 
      and name=#{param2}
</select>

此時得到正確的答案!

5.3)多個查詢字段時推薦使用的命名字段

明確指定封裝參數時的map和key @param("id") key:使用@param指定的值 5.3.1)接口類中
public Employee getByIdAndNames(@Param("id")int id,@Param("name")String name);

5.3.2)xml文件中

<select id="getByIdAndNames" resultType="com.MrChengs.bean.Employee" >
        select id,name,gender,email from test where id = #{id} and name=#{name}
</select>

測試類是5.2.3中的測試類,此時可以得到正確的結果! 5.4) pojo: 如果參數過多,正好是業務邏輯的數據模型,我們直接傳入pojo剛剛好 #{屬性名} : 取出傳入的pojo的屬性值 如果多個參數不是業務邏輯數據,沒有對應pojo,為了方法我們也可以直接傳入map #{key}就是取出對應的值 5.4.1)接口類中的
  public Employee getByMap(Map<String,Object> map);

5.4.2)xml文件中添加

<select id="getByMap" resultType="com.MrChengs.bean.Employee" >
           select id,name,gender,email from test where id = #{id} and name=#{name}
</select>

5.4.3)測試類

  @Test
    public void test6() throws IOException{
                SqlSessionFactory sessionFactory = getSqlSessionFactory();
                SqlSession sqlSession = sessionFactory.openSession();
                try{
                    EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", 5);
                    map.put("name", "MrChengs"); 
                    Employee employee = mapper.getByMap(map);
                    System.out.println(employee);
                }finally{
                    sqlSession.close();
                }
    }

運行時,可以正確輸出結果!

5.5)思考問題註意

技術分享圖片

未完,待更新....

MyBatis(3)-映射文件