媒介
之前一段时间写了【Spring源码阐明】系列的文章,感受对Spring的道理及利用各方面都把握了不少,一气呵成,开始下一个系列的文章【MyBatis源码阐明】,在【MyBatis源码阐明】文章的基本之上,可以继承阐明数据库毗连池、Spring整合MyBatis源码、Spring事物打点tx等等。
【MyBatis源码阐明】整个文章布局相较【Spring源码阐明】稍微改一改,后者会在每一部门源码阐明的开头列出要阐明的源码的实例,好比:
【MyBatis源码阐明】系列文章,在本文中会一次性地将所有的代码示例写完,之后就针对这些代码一部门一部门举办阐明,探究MyBatis道理。
其实MyBatis代码示例,我在之前的文章内里记得至少写了两遍,完全可以拿之前的文章作为例子,可是这里要再写一遍,就但愿分享给网友伴侣们一点立场:作为一个措施员,照旧该当多去写代码,多去实践,软件开发,不要认为之前写过的对象就没须要再写一遍,之前懂的内容就没须要再进修一遍,温故知新,写得越多用得越纯熟,思考得越多生长越快。
SQL筹备
首先照旧建表,这里筹备一段SQL:
drop table if exists mail; create table mail ( id int auto_increment not null comment '主键id', create_time datetime not null comment '建设时间', modify_time timestamp not null comment '修改时间', web_id int not null comment '站点id,1暗示新浪,2暗示QQ,3暗示搜狐,4暗示火狐', mail varchar(50) not null comment '邮箱名', use_for varchar(30) comment '邮箱用途', primary key(id), index use_for(use_for), unique index web_id_mail(web_id, mail) )charset=utf8 engine=innodb comment='邮箱表';
许多人大概有不止一个邮箱,新浪的、腾讯的、搜狐的,每个邮箱大概又有纷歧样的用途,这里就拿邮箱做一个例子。
成立每张表的时候该当留意独一约束,像这里,软件开发,一个网站下的邮箱必然是独一的,不行能在新浪下同时存在两个名为”123@sina.com”的邮箱名,因此对web_id+mail做独一索引。
成立实体类
成立完毕SQL之后,第二步必然是为表成立在Java层面的实体类,在SQL层面差异的词语利用”_”支解,在Java层面差异的词语则利用驼峰定名法:
此刻为mail表成立实体类:
public class Mail {
/**
* 主键id
*/
private long id;
/**
* 建设时间
*/
private Date createTime;
/**
* 修改时间
*/
private Date modifyTime;
/**
* 网站id,1暗示新浪,2暗示QQ,3暗示搜狐,4暗示火狐
*/
private int webId;
/**
* 邮箱
*/
private String mail;
/**
* 用途
*/
private String useFor;
public Mail() {
}
public Mail(int webId, String mail, String useFor) {
this.webId = webId;
this.mail = mail;
this.useFor = useFor;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getModifyTime() {
return modifyTime;
}
public void setModifyTime(Date modifyTime) {
this.modifyTime = modifyTime;
}
public int getWebId() {
return webId;
}
public void setWebId(int webId) {
this.webId = webId;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getUseFor() {
return useFor;
}
public void setUseFor(String useFor) {
this.useFor = useFor;
}
@Override
public String toString() {
return "MailDO [id=" + id + ", createTime=" + createTime + ", modifyTime=" + modifyTime + ", webId=" + webId + ", mail=" + mail + ", useFor="
+ useFor + "]";
}
}
留意实体类必然要重写toStirng()要领,便于定位问题。
成立数据会见层
下一步,小我私家爱好是成立数据会见层,对付数据会见层凡是有如下约定: