Mybatis Initial Learning Notes (II)
- 1. Mybatis implements the development of traditional dao
- 2. Development of mapper interface
- 3. Output ResultMap
- 4. Dynamic SQL
- Write a dao layer interface first, then an implementation class.
- Problems in traditional dao development:
- Method calls: Strings are error-prone and hard-coded.
- Write interfaces (mapper interfaces, mybatis creates interfaces based on interfaces and mapping files)
- Writing global configuration
- 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.
3.1 junit Unit Testing
- Add the corresponding jar package
- Use JUnit to implement tests (@org.junit.test: run directly as a unit)
- @ Before: Run before test
- @ 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
- 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**
- 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- 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>
- Use choose to set search priority
<choose> <when test="id!=-1"> and id=# </when> <otherwise> and age=# </otherwise> </choose>
- where tag
- 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);