最近换了个新的工作环境,新的项目中使用的是SSM框架,其中Mybatis在之前工作中一直没有使用过,为了能够尽快的开发,在这里先详细学习了下最长使用的mapper.xml,能够实现基本的增删改查。
一、什么是MyBatis
MyBatis是一款支持普通SQL查询、存储过程和高级映射的持久层框架。MyBatis消除了几乎所有的JDBC代码、参数的设置和结果集的检索。MyBatis可以使用简单的XML或注解用于参数配置和原始映射,将接口和Java POJO(普通Java对象)映射成数据库中的记录。
二、具体使用
现有如下场景:有表 Code_Dict
对应实体类:CodeDict
import java.io.Serializable;
import java.util.Date;
public class CodeDict implements Serializable {
private Long id;
private String key;
private String codeType;
private String code;
private String message;
private Boolean success;
private Boolean canRetry;
private Date createTime;
private Date updateTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key == null ? null : key.trim();
}
public String getCodeType() {
return codeType;
}
public void setCodeType(String codeType) {
this.codeType = codeType == null ? null : codeType.trim();
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code == null ? null : code.trim();
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message == null ? null : message.trim();
}
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public Boolean getCanRetry() {
return canRetry == null ? false : canRetry;
}
}
创建Dao层:
import net.wecash.annotations.DataSource;
import net.wecash.service.bean.CodeDict;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface CodeDictDao {
List<CodeDict> findAll();
}
创建对应的CodeDictmapper.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="net.abc.service.dao.CodeDictDao" >
<resultMap id="BaseResultMap" type="net.abc.service.bean.CodeDict" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="key" property="key" jdbcType="VARCHAR" />
<result column="code_type" property="codeType" jdbcType="VARCHAR" />
<result column="code" property="code" jdbcType="VARCHAR" />
<result column="message" property="message" jdbcType="VARCHAR" />
<result column="success" property="success" jdbcType="BIT" />
<result column="can_retry" property="canRetry" jdbcType="BIT" />
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
<result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
</resultMap>
<sql id="Base_Column_List" >
id, key, code_type, code, message, success, can_retry, create_time, update_time
</sql>
<select id="findAll" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from code_dict order by key
</select>
</mapper>
三、配置文件解析:
namespace: 在mybatis中,映射文件中的namespace是用于绑定Dao接口的,即面向接口编程。
当你的namespace绑定接口后,你可以不用写接口实现类,mybatis会通过该绑定自动
帮你找到对应要执行的SQL语句
1、mapper.xml文件加入<resultMap>映射,column是数据库中的字段名,property是实体类javabean中的属性,要一一对应
2、<select>标签中不要用ResultType,要用ResultMap且名字要和<resultMap>属性的id相同。且select语句不要用"select * from user_info",要用具体的字段名如"select user_id,user_name from user_info"
<?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.springapp.maper.UserMapper">
<resultMap type="User" id="BaseResultMap">
<!--
column:数据库中表的字段
property:数据库中表所有映射的实体类javaBean中的属性名
-->
<result column="user_id" property="id"/>
<result column="user_name" property="name"/>
</resultMap>
<!-- 这里的id必须和UserMapper接口中的接口方法名相同,resultMap和上面定义的id名字相同 -->
<select id="getUser" resultMap="BaseResultMap" parameterType="Java.lang.Integer">
select user_id,user_name from user_info where user_id=#{id}
</select>
</mapper>
id—-不说了,用来和namespace唯一确定一条引用的SQL语句
parameterType—-参数类型,如果SQL语句中的动态参数只有一个,这个属性可有可无
resultType—-结果类型,注意如果返回结果是集合,应该是集合所包含的类型,而不是集合本身