Mybatis小结之详解Mapper.xml
来源:互联网 发布:大数据 统计区别 编辑:程序博客网 时间:2024/06/06 05:23
最近换了个新的工作环境,新的项目中使用的是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;@Repositorypublic 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—-结果类型,注意如果返回结果是集合,应该是集合所包含的类型,而不是集合本身
0 0
- Mybatis小结之详解Mapper.xml
- mybatis Mapper.xml 详解
- mybatis mapper.xml详解
- MyBatis mapper.xml 详解
- mybatis mapper.xml详解
- mybatis的mapper.xml 小结
- MyBatis Mapper XML文件详解
- MyBatis Mapper XML文件详解
- MyBatis Mapper XML文件详解
- MyBatis Mapper XML文件详解
- Mybatis之mapper XML 文件
- mybatis中mapper.xml文件详解
- Mybatis的mapper.xml文件详解
- Mybatis 中 Mapper XML文件详解
- Mybatis的mapper.xml文件详解
- mybatis mapper xml的学习详解
- Mybatis的配置Mapper.xml详解
- Mybatis mapper.xml中常用标签详解
- Windows下Subversion的安装及配置
- Runnable、Callable、Future接口区别
- A+B in Hogwarts
- Java继承之间类的加载顺序, 以及代码块、构造块的执行顺序
- Linux系统简介以及为何常用于服务器
- Mybatis小结之详解Mapper.xml
- 蓝桥杯三羊献瑞全排列
- 深究JavaScript——原型
- 删除单链表中某个元素
- 面向接口编程详解(一)——思想基础
- 九度 oj 题目1549:货币问题
- 《flask web开发》第六章flask-mail
- VS2017安装教程
- Oracle 使用配置文件来配置JDBC连接数据库