mybatis interface-oriented programming, the value correspondence of the namespace attribute

Background: In the Mybatis project, because the traditional Dao development method can be used to achieve the required functions, but in this way, there will be a lot of repetitive code in the implementation class (disadvantage 1), and the mapping file execution statement needs to be specified in the method. The id of the id, and the correctness of the id cannot be guaranteed when writing (in the multi-person development, the id may also have the same name. When the implementation class has been written, there will be parameter errors. This is the second disadvantage), so the use of interface-oriented programming appeared. method.

In mybatis , the namespace in the mapping file is used to bind the Dao interface, that is, interface-oriented programming.

When your namespace binds the interface, you don't need to write the interface implementation class, mybatis will automatically pass the binding

Help you find the corresponding SQL statement to be executed, as follows:

interface

public interface ItemsMapperCustom {
    //Commodity query list
	public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;//This is the method of the interface! ! ! ! !
}

 For the mapping file is as follows:

<?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="cn.itcast.ssm.mapper.ItemsMapperCustom" ><!--Pay attention to the value of namespace, this is the path of the interface-->

  <!-- Define the fragment of commodity query, commodity query condition-->
  <sql id="query_items_where">
  <!-- Use dynamic sql, judge by if, and perform sql splicing when the conditions are met-->
       <!-- The query condition of the item is passed through the itemsCustom property in the ItemsQueryVo packaging object -->
       <if test="itemsCustom!=null">
          <if test="itemsCustom.name!=null and itemsCustom.name!="""></if>
          items.name LIKE '%${itemsCustom.name}%'
       </if>
  </sql>
  
  <!-- Product list query -->
  <!-- It is recommended that parameterType should be passed into the wrapper object-->
  <!-- resultType is recommended to use extended object -->
  <select id="findItemsList" parameterType="cn.itcast.ssm.po.ItemsQueryVo"  
   resultType="cn.itcast.ssm.po.ItemsCustom">
       SELECT items.* FROM items
       <where>
           <include refid="query_items_where"></include>
       </where>
        
  </select>
  	
</mapper>

Note that the methods in the interface correspond one-to-one with the IDs of the SQL statements in the mapping file .

Then you can use the interface-oriented programming directly in the code without writing the implementation class.

Implementation:

@Autowired
    private ItemsMapperCustom itemsMapperCustom;//The integration package of spring and mybatis, ItemsMapperCustom has arrived in the spring container
    
    @Override
    public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)
            throws Exception {
        // Query the database through ItemsMapperCustom
        return itemsMapperCustom.findItemsList(itemsQueryVo);//This is the execution method! ! ! ! ! ! ! ! ! ! ! !
    }
Since I have used the integration package of srping and MyBatis, I have lost some steps to simply use the Mabatis method, so it may be a bit difficult for those who only learned MyBatis to understand some places, but the idea should be right, if there are any mistakes, welcome Axe, let's make progress together.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325534918&siteId=291194637