Mybatis based on annotation and XML development

1 About Spring Boot

  • SpringBoot is a scaffolding for rapid development based on the Spring framework. It can help our developers quickly build a project environment, and provides some suggested configuration methods, reducing the difficulty of developing and deploying developers' programs.

2 About MyBatis

2.1 Overview of MyBatis

  • MyBatis is a persistence framework for Java, the purpose is to make the operation of the database more convenient, flexible and efficient. Java objects and SQL statements can be mapped through Java annotations and XML files, providing a very flexible way of writing SQL and creating dynamic SQL statements, and can be used in conjunction with the Spring framework.

2.2 MyBatis core idea

  • Separate Java objects from database operations, map to database fields through annotations and XML mapping files, and provide corresponding APIs to operate databases. It can automatically convert SQL statements into JDBC codes, and generate corresponding results according to the specified return value type for developers to use.

2.3 MyBatis usage process

  1. Configure the MyBatis environment
    Add related dependencies in pom.xml: MyBatis Framework and MySQL Driver
  2. The connection information of the configuration database in the configuration file(application.properties)
     spring.datasource.url=jdbc:mysql://localhost:3306/blog?characterEncoding=utf8&serverTimezone=Asia/Shanghai
      spring.datasource.username=root
      spring.datasource.password=root
  1. Define the pojo class and make a mapping relationship with the data table
  2. Write the Dao layer interface, define the method of operating the database in the interface, and implement the specific SQL statement in the annotation or XML file.
      @Mapper
      public interface UserMapper{
    
    
          @Insert("INSERT INTO user VALUES(NULL,#{username})")
          int insert(User user);
      }
  1. use
       // 1.自动装配
       @Autowired
       private UserMapper userMapper
       
       // 2.调用方法
       userMpper.insert(...)

3 MyBatis configuration SQL method

3.1 Annotation-based method

3.1.1 Description

  • The annotation-based method saves the writing of configuration XML files, and can easily complete some simple CRUD operations, but it will be redundant for some complex SQL statements.

3.1.2 Use process

  • (maven project) Add MyBatis and MySQL related dependencies in pom.xml
  • Configure the connection information of the database in the configuration file (application.properties)
  • Create an entity class and make a corresponding relationship with the table in the database
  • Configure the Mapper interface, add the @Mapper annotation to the interface, tell the bottom layer to create an implementation class for this interface, define the logic of data access in the implementation class, and execute the session with the database
  • Define the method in the interface, and use annotations to mark the type of SQL statement on the method. The annotations that can be used are: @Insert, @Dlelete, @Update, @Select If multiple parameters are involved in the SQL statement, you can use the @Param annotation to
    give Name each parameter.
  • Autowire it and use it in your application
     @Autowired
      private XxxMapper xxxMapper;
      
      xxxMapper.接口中的方法(参数...);

3.1.3 Common annotations

  • @Insert(“SQL”)
  • @Dlelete(“SQL”)
  • @Update(“SQL”)
  • @Select(“SQL”)

3.2 Based on XML

3.2.1 Advantages over annotations

  • Better readability
    XML files have good structure and semantics, which can completely separate JAVA codes and SQL statements, which is easy to manage and optimize. Note that configuring SQL statements may make JAVA code verbose.
  • Better reusability
    Write SQL statements into XML files, extract and quote repeated SQL through and tags, thus improving the reusability of SQL statements.
  • Better support for dynamic SQL
    Dynamic SQL statements are SQL generated according to runtime parameters, which are more complex and more convenient to use XML files.

3.2.2 Use process

  • Add dependencies: pom.xml
  • Configure data source: application.propertis
  • Configure the scanning path of the xml file: application.properties
  • Create an entity class and make a correspondence with the data table
  • Define the interface, add the @Mapper annotation, and define the methods in the interface
  • Create an xml file and configure SQL using tags


  • Assembly use
      @Autowired
      private XxxMapper xxxMapper;
      
    xxxMapper.接口方法(参数...);

3.2.3 Common tags

  • insert tag
  • delelte label
  • update tag
  • select tag
    to use the select tag needs to specify the attribute value of resultType
  • foreach tag: for dynamic deletion
      <delete id="xxx">
      	DELETE FROM xxx WHERE id in(
          	<foreache collection="对象类型" item="变量名" separator="分隔符">
          		#{变量名}
          	</foreache>
          )
      </delete>
  • set tag and if tag: for dynamic modification
      <update id="xxx">
      	UPDATE xxx 
          <set>
          	<if test="属性名!=null">字段名=#{属性名},</if>
              <if test="属性名!=null">字段名=#{属性名},</if>
              <if test="属性名!=null">字段名=#{属性名}</if>
          </set>
      </update>
  • sql label and include label: for the reuse of SQL statements
      <!--1.重复SQL抽取-->
      <sql id="selectSql">
          SELECT * FROM xxx
      </sql>
      
      <select id="xxx" resultType="xxx">
          <include refid="selectSql"></include>
      </select>
      
      <select id="yyy" resultType="yyy">
          <include refid="selectSql"></include> WHERE id&gt;5
      </select>

Guess you like

Origin blog.csdn.net/m0_72568513/article/details/131956191