Spring boot 和 mybatis 学习笔记2--Mapper XML (静态)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tianhongqiang/article/details/60975953

学习了mybatis的mapper xml 规则,做了示例如下

   

<?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="com.thq.mybatis.dao.UserDao">
	<!-- 设置缓存 使用最近最少使用算法:移除最长时间不被使用的对象  flushInterval:缓存刷新时间  size:引用数目 -->
	<cache eviction="LRU" flushInterval="60000" size="512" readOnly="false"></cache>
	<!-- 定义可重用的 SQL 代码段 -->
	<sql id="userColumns"> ${alias}.id,${alias}.name,${alias}.age,${alias}.pwd  </sql>
	<!-- 根据Id查找对象 -->
	<select id="selectUserById" parameterType="int"
		resultType="com.thq.mybatis.model.User">
		select
		<!-- sql重用点 -->
		<include refid="userColumns">
			<property name="alias" value="t1" />
		</include>
		from tb_user t1 where t1.id = #{id,jdbcType=NUMERIC}
	</select>

	<select id="selectUsersByAge" parameterType="int"
		resultType="com.thq.mybatis.model.User">
		select * from tb_user where
		age = #{age}
	</select>
	<!-- 向db添加一条记录   并生成主键,设置到user 对象的id字段 可以通过getId()获取-->
	<insert id="addUser" parameterType="com.thq.mybatis.model.User"
		useGeneratedKeys="true" keyProperty="id">
		insert into tb_user (name,age,pwd)
		values(#{name},#{age},#{pwd})
	</insert>
	<!-- 向db更新记录 -->
	<update id="updateUser" parameterType="com.thq.mybatis.model.User">
		update tb_user set
		pwd=#{pwd} where id=#{id}
	</update>
	<!-- 删除记录-->
	<delete id="deleteUser" parameterType="int">
		delete from tb_user where
		id= #{id}
	</delete>
	<!-- 用resultMap 来映射结果集   association 关联对象-->
	<select id="selectUserList" resultMap="selectUserListResult">
		select 
			a.id as user_id,
			a.name as user_name,
			a.age as user_age,
			a.pwd as user_pwd,
			b.name as menu_name,
			b.index as menu_index,
			b.userId as menu_userId,
			b.id as menu_id
		from tb_user a ,tb_menu b
		where a.id = b.userId
	</select>
	<resultMap id="selectUserListResult" type="com.thq.mybatis.model.User">
		<id property="id" column="user_id" />
		<result property="name" column="user_name" />
		<result property="age" column="user_age" />
		<result property="pwd" column="user_pwd" />
		<association property="menu" javaType="com.thq.mybatis.model.Menu">
			<id property="id" column="menu_id" />
			<result property="name" column="menu_name" />
			<result property="index" column="menu_index" />
			<result property="userId" column="menu_userId" />
		</association>
	</resultMap>
	
	<!--  用resultMap 来映射结果集   collection 关联对象-->
	<select id="selectUserCollection" resultMap="selectUserCollectionResult">
		select 
			id as user_id,
			name as user_name,
			age as user_age,
			pwd as user_pwd
		from tb_user
	</select>
	<select id="selectMenuList" parameterType="int"  resultType="com.thq.mybatis.model.Menu">
		select * from tb_menu where userId=#{id}
	</select>
	<resultMap id="selectUserCollectionResult" type="com.thq.mybatis.model.User">
		<id property="id" column="user_id" />
		<result property="name" column="user_name" />
		<result property="age" column="user_age" />
		<result property="pwd" column="user_pwd" />
		<collection property="menus" javaType="ArrayList" column="user_id" select="selectMenuList">
			<id column="id" property="id"></id>
			<result property="name" column="name" />
			<result property="index" column="index" />
			<result property="userId" column="userId" />
		</collection>
	</resultMap>
	
</mapper>
上述文件对应的java Dao

package com.thq.mybatis.dao;

import java.util.List;

import com.thq.mybatis.model.User;

public interface UserDao  {
	public User selectUserById(int id);
	
	public List<User> selectUsersByAge(int age);
	
	public int addUser(User user);
	
	public int updateUser(User user);
	
	public int deleteUser(int id);
	
	public List<User> selectUserList();
	
	public List<User> selectUserCollection(int id);
}
	

附带model

package com.thq.mybatis.model;

import java.io.Serializable;
import java.util.List;

import com.google.gson.annotations.Expose;

public class User implements Serializable {
	private static final long serialVersionUID = 1L;
	private Integer id;
	private String name;
	private Integer age;
	private String pwd;
	private Menu menu;
	private List<Menu> menus;
	
}
package com.thq.mybatis.model;

import java.io.Serializable;

public class Menu implements Serializable {
	private static final long serialVersionUID = 1L;
	private String name;
	private Integer index;
	private Integer id;
	private Integer userId;	
}
 
 

上述代码可以放在

http://download.csdn.net/detail/tianhongqiang/9763988 这个代码中运行

 还可以参见:http://www.mybatis.org/mybatis-3/sqlmap-xml.html

最后 多学习,学写笔记大笑


猜你喜欢

转载自blog.csdn.net/tianhongqiang/article/details/60975953
今日推荐