1、创建数据库,并在库中创建表(代码如下:)
1.1、创建库
DROP DATABASE IF EXISTS tms;
CREATE DATABASE tms default character set utf8;
1.2、创建表
#使用库
use tms
#创建表
DROP TABLE IF EXISTS `t_members`;
CREATE TABLE `t_members` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`nickname` varchar(255) DEFAULT NULL COMMENT '昵称',
`password` char(32) NOT NULL DEFAULT '' COMMENT '密码',
`realname` varchar(36) NOT NULL DEFAULT '' COMMENT '真实姓名',
`gender` enum('MALE','FEMALE','NONE') NOT NULL DEFAULT 'NONE' COMMENT '姓名',
`rank` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '会员等级',
`email` char(50) NOT NULL DEFAULT '' COMMENT '邮箱',
`mobile` varchar(15) NOT NULL DEFAULT '' COMMENT '手机号',
`safequestion` varchar(255) NOT NULL DEFAULT '0' COMMENT '安全问题',
`safeanswer` char(30) NOT NULL DEFAULT '' COMMENT '安全答案',
`createdTime` datetime DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='会员表';
1.3、插入数据(简单模板数据)
#数据1
INSERT INTO `t_members` VALUES ('2', 'tom', '123456', 'Tom Hanks', 'MALE', '1', '[email protected]', '13588888888', '1+1=?', '3', 'now()');
#数据2
INSERT INTO `t_members` VALUES ('3', 'andy', '123789', 'Andy Zhou', 'FEMALE', '1', '[email protected]', '13699999999', '2+2=?', '5', 'now()');
2.1、添加mybatis依赖(mybatis使用需要)
org.mybatis
mybatis
3.2.8
2.2、添加MySQL驱动依赖(连接或操作数据库)
mysql
mysql-connector-java
5.1.40
2.3、添加junit依赖(测试使用)
junit
junit
4.12
在resources下创建mybatis-config.xml配置文件,并配置如下内容:
在src/main/resources/mapper目录下创建映射文件
MemberMapper.xml
代码:
借助实体类对象存储数据库表中数据与对象中数据之间的映射(添加set、get方法,重写toString方法,并实现序列化)
package com.zhou11506.member.dao.entity;
import java.io.Serializable;
import java.util.Date;
/**
* Member实体类,与数据库t_members表映射
* @author zhou11506
*/
public class Member implements Serializable {
private static final long serialVersionUID = 6948864912341044105L;
private Integer id;
private String nickname;
private String password;
private String realname;
private String gender;
private String email;
private String mobile;
private Integer rank=0;
private String safequestion;
private String safeanswer;
private Date createdTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public Integer getRank() {
return rank;
}
public void setRank(Integer rank) {
this.rank = rank;
}
public String getSafequestion() {
return safequestion;
}
public void setSafequestion(String safequestion) {
this.safequestion = safequestion;
}
public String getSafeanswer() {
return safeanswer;
}
public void setSafeanswer(String safeanswer) {
this.safeanswer = safeanswer;
}
public Date getCreatedTime() {
return createdTime;
}
public void setCreatedTime(Date createdTime) {
this.createdTime = createdTime;
}
@Override
public String toString() {
return "Member{" +
"id=" + id +
", nickname='" + nickname + '\'' +
", password='" + password + '\'' +
", realname='" + realname + '\'' +
", gender='" + gender + '\'' +
", email='" + email + '\'' +
", mobile='" + mobile + '\'' +
", rank=" + rank +
", safequestion='" + safequestion + '\'' +
", safeanswer='" + safeanswer + '\'' +
", createdTime=" + createdTime.toLocaleString() +
'}';
}
}
7.1、在Memberdao文件添加查询方法:
代码:
List findPageObjects();
7.2、MemberMapper映射文件中添加映射元素:
注:上述查询方法中的“”实际工作中应写需要查询的字段,为测试方便此次用“”代替。
7.3、定义单元测试:
代码:
package com.zhou11506;
import com.zhou11506.member.dao.MemberDao;
import com.zhou11506.member.dao.entity.Member;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
/**
* 测试mybatis
* @author zhou11506
*/
public class TestMybatis {
/**查询测试方法*/
@Test
public void testFindPageObjects() throws IOException {
//定义配置文件的路径
String resource="mybatis-config.xml";
//通过SqlSessionFactoryBuilder创建SqlSessionFactory
SqlSessionFactory factory=new SqlSessionFactoryBuilder()
.build(Resources.getResourceAsStream(resource));
//通过SqlSessionFactory创建SqlSession
SqlSession session=factory.openSession();
//执行dao对象
MemberDao dao=session.getMapper(MemberDao.class);
//得到结果集
List list=dao.findPageObjects();
//输出结果集
System.out.println(list);
//关闭连接
session.close();
}
}
测试结果:
[Member{id=1, nickname='tom', password='123456', realname='Tom Hanks', gender='MALE', email='[email protected]', mobile='13588888888', rank=1, safequestion='1+1=?', safeanswer='3', createdTime=2018-12-31 15:11:26}, Member{id=2, nickname='andy', password='123789', realname='Andy Zhou', gender='FEMALE', email='[email protected]', mobile='13699999999', rank=1, safequestion='2+2=?', safeanswer='5', createdTime=2018-12-31 15:11:26}]
以上为MyBatis基于接口方式实现数据操作的简单案例。若想实现新增、修改、删除操作,可根据以上案例在dao文件添加相应方法—>MemberMapper文件添加相应的SQL语句—>在Test测试类的29行处将查询的方法更改为相应的方法即可。
通过上述的案例,我们可以看出MyBatis实现了SQL语句与JAVA语句的分离,SQL语句写在了相应的Mapper.xml(如MemberMapper.xml)文件下。这使得程序后续的可维护性更便利。