mybatis入门
mybatis解决的问题
- xml或者配置映射sql,执行sql,返回对象
mybatis的xml方式
定义resultMap
<resultMap id="userMap" type="tk.mybatis.simple.model.SysUser">
<id property="id" column="id"/>
<result property="userName" column="user_name"/>
<result property="userPassword" column="user_password"/>
<result property="userEmail" column="user_email"/>
<result property="userInfo" column="user_info"/>
<result property="headImg" column="head_img" jdbcType="BLOB"/>
<result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
</resultMap>
xml支持select,insert,update,delete
<select id="selectById" resultMap="userMap">
select * from sys_user where id = #{id}
</select>
<insert id="insert2" useGeneratedKeys="true" keyProperty="id">
insert into sys_user(
user_name, user_password,
<if test="userEmail != null">
<if test="userEmail != ''">
user_email,
</if>
</if>
user_info, head_img, create_time)
values(
#{userName}, #{userPassword},
<if test="userEmail != null">
<if test="userEmail != ''">
#{userEmail},
</if>
</if>
#{userInfo}, #{headImg, jdbcType=BLOB}, #{createTime, jdbcType=TIMESTAMP})
</insert>
<update id="updateById">
update sys_user
set user_name = #{userName},
user_password = #{userPassword},
user_email = #{userEmail},
user_info = #{userInfo},
head_img = #{headImg, jdbcType=BLOB},
create_time = #{createTime, jdbcType=TIMESTAMP}
where id = #{id}
</update>
<delete id="deleteById">
delete from sys_user where id = #{id}
</delete>
mybatis的注解方式
注解方式写sql和provider注解
@Results(id = "roleResultMap", value = {
@Result(property = "id", column = "id", id = true),
@Result(property = "roleName", column = "role_name"),
@Result(property = "enabled", column = "enabled"),
@Result(property = "createBy", column = "create_by"),
@Result(property = "createTime", column = "create_time")
})
@Select("select id,role_name, enabled, create_by, create_time from sys_role where id = #{id}")
@Insert({"insert into sys_role(role_name, enabled, create_by, create_time)",
"values(#{roleName}, #{enabled}, #{createBy}, #{createTime, jdbcType=TIMESTAMP})"})
@Options(useGeneratedKeys = true, keyProperty = "id")
@Update({"update sys_role",
"set role_name = #{roleName},",
"enabled = #{enabled},",
"create_by = #{createBy},",
"create_time = #{createTime, jdbcType=TIMESTAMP}",
"where id = #{id}"
})
@Delete("delete from sys_role where id = #{id}")
public String selectById(final Long id){
return new SQL(){
{
SELECT("id, privilege_name, privilege_url");
FROM("sys_privilege");
WHERE("id = #{id}");
}
}.toString();
}
mybatis的动态SQL
if choose(when, otherwise,trim,where,set,foreach,bind)
mybaits的代码生成器
支持生成代码
mybatis的高级查询
一对一使用join和association标签
一对多collection标签
mybatis的缓存
一级缓存:单个sqlsession有效
二级缓存:各类插件
ehcache和redis
mybatis的插件开发
拦截器接口
public interface Interceptor {
# 拦截器方法调用
Object intercept(Invocation invocation) throws Throwable;
#要拦截的对象
Object plugin(Object target);
#设置配置
void setProperties(Properties properties);
}
#拦截器前面
@Intercepts(
@Signature(
type = Executor.class,
method = "query",
args = {MappedStatement.class, Object.class,
RowBounds.class, ResultHandler.class}
)
)
拦截4类接口:Executor,ParameterHandler,ResultSetHandler,StatementHandler
spring集成mybatis
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
springboot集成mybatis
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
网友评论