Use of MyBatis (2) Use annotated MyBatis joint query to query dynamic sql step by step

MyBatis (2)

The first part of the use of MyBatis, the delivery address: The use of MyBatis (1) Beginners must see the usage log factory of MyBatis MyBatis's life cycle ResultMap based on xml configuration

One, use annotation development

  1. The annotations are used in the methods of the Mapper interface, no need to use the UserMapper.xml file
public interface UserMapper {
    
    
    
    @Select("select * from user limit #{startIndex}, #{pageSize}")
    List<User> getUserListById(Map<String, Integer> map);

}

  1. Need to bind the interface in the core configuration file mybatis-config.xml
<mappers>
    <mapper class="com.qizegao.dao.UserMapper"/>
</mappers>

  1. test
@Test
public void test01() {
    
    
    SqlSession sqlSession = MyBatisUtils.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

    //向map参数中赋值
    HashMap<String, Integer> map = new HashMap<String, Integer>();
    map.put("startIndex", 0);
    map.put("pageSize", 2);

    List<User> userListById = userMapper.getUserListById(map);
    sqlSession.close();
}

  1. Log output

Insert picture description here

2. Use annotations to complete the delete operation

  1. Add annotations to the Mapper interface
public interface UserMapper {
    
    

    @Delete("delete from user where id = #{uid}")
    int deleteUser(@Param("uid") int id);

    /**
     * 1. 方法上的注解还有@Insert()、@Update()两种,分别完成对应的操作
     * 2. 方法中的参数使用@Param注解表示给参数名起别名
     *
     * 注意: (1)参数有多个基本类型和String类型时需要使用@Param注解
     *       (2)参数如果只有一个基本类型时,可以不使用@Param注解,但尽量使用
     *       (3)类类型不需要使用@Param注解
     *
     */

}

  1. Need to bind the interface in the core configuration file mybatis-config.xml
<mappers>
    <mapper class="com.qizegao.dao.UserMapper"/>
</mappers>

  1. test

note:

public static SqlSession getSqlSession() {
    
    
    //可以在MyBatisUtils工具类中设置sqlSession为自动提交
    return sqlSessionFactory.openSession(true);
}

@Test
public void test01() {
    
    
    SqlSession sqlSession = MyBatisUtils.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    int res = userMapper.deleteUser(3);
    //已经在工具类中设置为自动提交,故此处无需提交事务
    sqlSession.close();
}

  1. Log output
    Insert picture description here

  2. The rest of the operations using annotations are similar, so I won’t repeat them

Three, joint query

There are two tables t_key and t_lock, the corresponding relationship is as follows:

Insert picture description here

1. Ways of cascading attributes

(1) Key class and Lock class

Insert picture description here

(2) Declaration in the KeyMapper interface

public Key getKeyById(Integer id);

(3) Write in KeyMapper.xml

 	<select id="getKeyById" resultMap="mykey">
 		select k.id, k.`keyname`, k.`lockid`, l.`id` lid, l.`lockName` 
		from t_key k
		left join t_lock l on k.`lockid`=l.`id`
		where k.`id`=#{id}
 	</select>

  	<resultMap id="mykey" type="com.qizegao.bean.Key" >
 		<id property="id" column="id"/>
 		<result property="keyName" column="keyname"/>
 		<result property="lock.id" column="lid"/>
 		<result property="lock.lockName" column="lockName"/>
 	</resultMap> 

2. Ways to use association tags

Written in KeyMapper.xml (the content of the select tag is consistent with the above)

 	<resultMap id="mykey" type="com.qizegao.bean.Key" >
 		<id property="id" column="id"/>
 		<result property="keyName" column="keyname"/>
 		<!-- 使用association标签表示联合了一个对象 -->
 		<!-- javaType属性指定联合的对象类型 -->
 		<association property="lock" javaType="com.qizegao.bean.Lock">
 			<!-- 定义如何封装lock对象,property属性表示的是lock对象的属性 -->
 			<id property="id" column="lid"/>
 			<result property="lockName" column="lockName"/>
 		</association>
 	</resultMap>

3. The way to use the collection tag

(1) Key class and Lock class

Insert picture description here

(2) Declaration in the LockMapper interface

public Lock getLockById(Integer id);

(3) Declared in LockMapper.xml

 	<select id="getLockById" resultMap="mylock">
 		select l.*,k.id kid,k.`keyname`,k.`lockid` 
 		from t_lock l 
		left join t_key k on l.`id`=k.`lockid`
		where l.id=#{id}
 	</select>

 	<resultMap id="mylock" type="com.atguigu.bean.Lock">
 		<id property="id" column="id"/>
 		<result property="lockName" column="lockName"/>
 		<!-- 
 			使用collection标签表示联合了一个集合
 			ofType属性指定集合里面元素的类型
 		-->
 		<collection property="keys" ofType="com.atguigu.bean.Key">
 			<!-- 定义集合中的封装规则,property属性表示的是Key类中的属性 -->
 			<id property="id" column="kid"/>
 			<result property="keyName" column="keyname"/>
 		</collection>
 	</resultMap>

Fourth, step by step query

  1. Key class and Lock class

Insert picture description here

  1. Written in KeyMapper interface and LockMapper interface
public Key getKeyByIdSimple(Integer id); //KeyMapper中的方法

public Lock getLockByIdSimple(Integer id); //LockMapper中的方法
  1. Written in KeyMapper.xml and LockMapper.xml
 	<!-- LockMapper.xml中编写 -->
 	<select id="getLockByIdSimple" resultType="com.atguigu.qizegao.Lock">
 		select * from t_lock where id=#{id}
 	</select>

 	<!-- KeyDao.xml中编写 -->
 	<select id="getKeyByIdSimple" resultMap="mykey02">
 		select * from t_key where id=#{id}
 	</select>
 	<resultMap id="mykey02" type="com.qizegao.bean.Key">
 		<id property="id" column="id"/>
 		<result property="keyName" column="keyname"/>
 		<association property="lock" 
 			<!-- 1. select属性执行目标方法对应的sql语句
 				 2. column属性将select查询结果的哪一列封装 				 
 			 -->
 			select="com.qizegao.dao.LockDao.getLockByIdSimple"
 			column="lockid" ></association>
 	</resultMap>

Five, dynamic sql

  1. Create a database table t_teacher

Insert picture description here

  1. Create the Teacher class
public class Teacher {
    
    

	private Integer id;
	private String name;
	private String course;
	private String address;
	private Date birth;
	//以及其余JavaBean结构
	
}
  1. Written in TeacherDao
public List<Teacher> getTeacherByCondition(Teacher teacher);
  1. Write in TeacherDao.xml
	<resultMap id="teacherMap" type="com.qizegao.bean.Teacher">
		<id property="id" column="id" />
		<result property="address" column="address" />
		<result property="birth" column="birth_date" />
		<result property="course" column="class_name" />
		<result property="name" column="teacherName" />
	</resultMap>
	
	<select id="getTeacherByCondition" resultMap="teacherMap">
		
			select * from t_teacher
		
		<!-- trim标签用来截取字符串:
		
				(1) prefix属性为sql整体添加一个前缀,一般设置为where,
				    当没有条件符合where时,会自动去掉where前缀
				    
				(2) prefixOverrides属性去除每个if标签中指定的前缀,一般设置为and
					当某个and后面的条件不满足时,自动去掉and前缀
					
				(3) suffix和suffixOverrides属性使用与上述类似  		
		 -->

		<trim prefix="where" prefixOverrides="and">
		
			<!-- if标签的test属性写判断条件,
			     当满足判断条件时就将if标签体中的sql语句拼接到if标签体外的sql语句上
			     如 "id != null" 表示传入的JavaBean的id属性值不为null时才满足条件
		    -->
		
			<if test="id!=null">
				id > #{id} and
			</if>

			<!-- &amp;表示&,&quot;表示" -->
			<if test="name!=null &amp;&amp; !name.equals(&quot;&quot;)">
				and teacherName like #{name} 
			</if>
			
			<if test="birth!=null">
			    <!-- &lt;表示< -->
				and birth_date &lt; #{birth} 
			</if>
		</trim>
	</select>
  1. Written in the test class

Manually create a teacher object required in the parameter, call the setter method of the object according to the demand, the sql statement will dynamically change according to whether the corresponding attribute value is passed in

  1. note

Remove the trim tag, use the if tag to automatically remove the prefix and in the if tag

Six, other tags of dynamic sql

1. foreach tag

(1) Written in TeacherDao

public List<Teacher> getTeacherByIdIn(@Param("ids")List<Integer> ids);

(2) Prepared in TeacherDao.xml

	<select id="getTeacherByIdIn" resultMap="teacherMap">
		
		SELECT * FROM t_teacher WHERE id IN
		
		<!-- foreach标签用来遍历集合
				1. collection属性指定要遍历的集合 
				2. item属性为每次遍历出的元素起一个变量名
				3. separator属性定义遍历到的元素的分隔符
				4. open属性定义foreach标签中的sql语句以什么开始
				5. close属性定义foreach标签中的sql语句以什么结束
				6. index属性表示索引:
					(1) 如果遍历的是一个list: 
							index:指定的变量保存了当前索引 
							item:保存当前遍历的元素的值 
					(2) 如果遍历的是一个map: 
							index:指定的变量保存了当前遍历的元素的key 
							item:保存当前遍历的元素的值
		-->
		
		<if test="ids.size >0">
			<foreach collection="ids" item="id_item" separator="," open="("
				close=")">
				#{id_item} <!-- 取出遍历的元素的值 -->
			</foreach>
		</if>
	</select>

2. choose tag

(1) Written in TeacherDao

public List<Teacher> getTeacherByConditionChoose(Teacher teacher);

(2) Prepared in TeacherDao.xml

	<select id="getTeacherByConditionChoose" resultMap="teacherMap">
		select * from t_teacher
		
		<!-- sql语句不写where,使用where标签 -->
		
		<where> <!-- 相当于在sql语句补了一个where,且自动去除and或or -->

			<!-- choose标签是从上向下判断,
				 一旦满足了when条件,就将when标签中的sql语句添加到原sql语句后面
				 一旦满足了一个when标签就不会去判断其余的when标签,跳出choose标签
			 -->
			
			<choose>
				<when test="id!=null">
					id=#{id}
				</when>
				<when test="name!=null and !name.equals(&quot;&quot;)">
					teacherName=#{name}
				</when>
				<when test="birth!=null">
					birth_date = #{birth}
				</when>
				<otherwise>
					1=1 <!-- 永远为true -->
				</otherwise>
			</choose>
		</where>
	</select>

3. set tag

(1) Written in TeacherDao

public int updateTeacher(Teacher teacher);

(2) Prepared in TeacherDao.xml

	<update id="updateTeacher">
		UPDATE t_teacher
		
		<!-- 使用set标签代替sql语句中的set
			 可以自动的去掉if标签中sql语句后面多余的逗号
		 -->
		
		<set>
			<if test="name!=null and !name.equals(&quot;&quot;)">
				teacherName=#{name},
			</if>
			<if test="address!=null and !address.equals(&quot;&quot;)">
				address=#{address},
			</if>
			<if test="birth!=null">
				birth_date=#{birth}
			</if>
		</set>
		<where> <!-- where标签代替sql语句手写where -->
			id=#{id}
		</where>
	</update>

Guess you like

Origin blog.csdn.net/weixin_49343190/article/details/110390862