Mybatis Initial Learning Notes

Mybatis Initial Learning Notes (II) 1. Mybatis implements the development of traditional dao 2. Development of mapper interface 3. Output ResultMap 3...
3.1 junit Unit Testing
3.2 ResultMap

Mybatis Initial Learning Notes (II)

1. Mybatis implements the development of traditional dao
  1. Write a dao layer interface first, then an implementation class.
  2. Problems in traditional dao development:
  • Method calls: Strings are error-prone and hard-coded.
2. Development of mapper interface
  1. Write interfaces (mapper interfaces, mybatis creates interfaces based on interfaces and mapping files)
  2. Writing global configuration
  3. Write mapping files
  • The mapping file and mapper interface have the same name as the package.
  • The fully qualified name of the interface must be consistent with the namespace of the mapper mapping file
  • The method name in the interface is consistent with the label in the mapping file, the parameters in the method are consistent with the parameterType or parameterMap in the mapping file, and the return value type of the method is consistent with the resultType or resultMap in the mapping file.
  1. test

    Project examples and error handling

3. Output ResultMap

3.1 junit Unit Testing

  1. Add the corresponding jar package
  2. Use JUnit to implement tests (@org.junit.test: run directly as a unit)
  3. @ Before: Run before test
  4. @ After: Run after the test
import Mapper.DataMapper; import entity.Data; import org.apache.ibatis.session.SqlSession; import util.MybatisUtils; import java.util.List; public class Test { @org.junit.Test public void test(){ SqlSession session= MybatisUtils.getSqlsession(); DataMapper mapper=session.getMapper(DataMapper.class); List<Data> re = mapper.queryAll(); System.out.println(re); MybatisUtils.closeSqlsession(); } }

3.2 ResultMap

  1. ResultType: ** Property names of entity classes can be mapped correctly if they correspond to fields in database tables. If the fields do not correspond, they cannot be encapsulated. Objects cannot be created if the attribute names of entity classes are different from those of fields in database tables.
    For example, now the property names and fields are all different, running~


Nothing was found.~

Solution:
- Alias
- Mapping between fields and attributes using ResultMap**

  1. ResultMap uses
  • id: Unique id
  • Type: Encapsulated object type
  • Describes mapping relationships between primary key fields and attributes
  • Non-primary key field mapping**
<resultMap type=" " id=" "> <id column=" " property=" "/> <result column=" " property=" "/> </resultMap

Examples of programs:

<resultMap type="entity.Data" id="Data"> <id column="newid" property="id"/> <result column="newname" property="name"/> <result column="newage" property="age"/> </resultMap>

Perfect solution!!!

4. Dynamic SQL
  1. Using if
<select id="queryCombo" parameterType="entity.Data" resultType="entity.Data"> select * from yangtingting where 1=1 <if test="id!=-1"> and id=# </if> <if test="name!=null"> and name=# </if> <if test="age!=-1"> and age=# </if> </select>
  1. Use choose to set search priority
<choose> <when test="id!=-1"> and id=# </when> <otherwise> and age=# </otherwise> </choose>
  1. where tag
  2. foreach

<select id="query" parameterType="List" resultType="entity.Data"> select * from yangtingting where id in <foreach collection="list" index="index" item="id" open="(" close=")" separator=","> # </foreach> </select>
List list=new ArrayList<>(); list.add(3); list.add(20); List<Data> re = mapper.query(list); System.out.println(re);

6 August 2019, 06:14 | Views: 6230

Add new comment

For adding a comment, please log in
or create account

0 comments