1:使用maven导入mybatis的依赖
1.0:使用idea创建一个maven项目
image.png
image.png
1.1:一个maven的空项目,目录大概这样
image.png
1.2:在pom.xml中导入mybatis的依赖,大概需要以下依赖
mysql
mysql-connector-java
8.0.18
org.mybatis
mybatis
3.5.1
org.slf4j
slf4j-api
1.7.29
org.slf4j
slf4j-log4j12
1.7.21
test
log4j
log4j
1.2.17
junit
junit
4.12
test
2:开始配置mybatis
2.0:创建测试数据库表
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`student_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '编号',
`name` VARCHAR(20) DEFAULT NULL COMMENT '姓名',
`phone` VARCHAR(20) DEFAULT NULL COMMENT '电话',
`email` VARCHAR(50) DEFAULT NULL COMMENT '邮箱',
`sex` TINYINT(4) DEFAULT NULL COMMENT '性别',
`locked` TINYINT(4) DEFAULT NULL COMMENT '状态(0:正常,1:锁定)',
`gmt_created` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '存入数据库的时间',
`gmt_modified` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改的时间',
PRIMARY KEY (`student_id`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='学生表';
这是根据上面的表,创建的实体类StudentModel,也可以采用mybatis逆向生成的方式生成,后面会讲到
package mybatis.entity;
import java.io.Serializable;
import java.util.Date;
public class StudentModel implements Serializable {
/**
* 编号
*/
private Integer student_id;
/**
* 姓名
*/
private String name;
/**
* 电话
*/
private String phone;
/**
* 邮箱
*/
private String email;
/**
* 性别
*/
private Byte sex;
/**
* 状态(0:正常,1:锁定)
*/
private Byte locked;
/**
* 存入数据库的时间
*/
private Date gmt_created;
/**
* 修改的时间
*/
private Date gmt_modified;
private static final long serialVersionUID = 1L;
public Integer getStudent_id() {
return student_id;
}
public void setStudent_id(Integer student_id) {
this.student_id = student_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone == null ? null : phone.trim();
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email == null ? null : email.trim();
}
public Byte getSex() {
return sex;
}
public void setSex(Byte sex) {
this.sex = sex;
}
public Byte getLocked() {
return locked;
}
public void setLocked(Byte locked) {
this.locked = locked;
}
public Date getGmt_created() {
return gmt_created;
}
public void setGmt_created(Date gmt_created) {
this.gmt_created = gmt_created;
}
public Date getGmt_modified() {
return gmt_modified;
}
public void setGmt_modified(Date gmt_modified) {
this.gmt_modified = gmt_modified;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", student_id=").append(student_id);
sb.append(", name=").append(name);
sb.append(", phone=").append(phone);
sb.append(", email=").append(email);
sb.append(", sex=").append(sex);
sb.append(", locked=").append(locked);
sb.append(", gmt_created=").append(gmt_created);
sb.append(", gmt_modified=").append(gmt_modified);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}
2.1:接着需要创建mybatis的xml的配置文件,配置文件的一些属性可以参考https://mybatis.org/mybatis-3/zh/configuration.html#properties,当前的目录
image.png
image.png
配置文件mybatis.config.xml的内容
jdbc.properties文件内容
#mysql驱动的路径
jdbc.driverLocation=/Users/apple/Tomcat/apache-tomcat-9.0.14/webapps/ROOT/WEB-INF/lib/mysql-connector-java-5.1.6.jar
jdbc.driverClass=com.mysql.cj.jdbc.Driver
#修改为自己的数据库地址
jdbc.connectionURL=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&useSSL=false&useServerPrepStmts=true&cachePrepStmts=true
jdbc.userId=root
jdbc.password=*******
2.2:创建映射接口与映射文件StudentDao.xml
image.png
映射接口StudentDao
package mybatis.dao;
import mybatis.entity.StudentModel;
import java.util.List;
public interface StudentDao {
// int deleteByPrimaryKey(Integer student_id);
// 单条数据插入
int insert(StudentModel record);
// 批量插入
int insertBatch(List list);
// 校验参数插入
int insertSelective(StudentModel record);
//
// StudentModel selectByPrimaryKey(Integer student_id);
//
// int updateByPrimaryKeySelective(StudentModel record);
//
// int updateByPrimaryKey(StudentModel record);
}
特别注意:在
语句中 insert into student 这里的 student 不能带有双引号 "student",因为后面采用mybatis逆向生成时自动增加的双引号,导致执行时报错
student_id, name, phone, email, sex, locked, gmt_created, gmt_modified
name, phone,
email, sex, locked,
gmt_created, gmt_modified
insert into student
( )
values (#{name,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR}, #{sex,jdbcType=TINYINT}, #{locked,jdbcType=TINYINT},
#{gmt_created,jdbcType=TIMESTAMP}, #{gmt_modified,jdbcType=TIMESTAMP})
insert into student (name, phone,
email, sex, locked,
gmt_created, gmt_modified)
VALUES
(#{item.name,jdbcType=VARCHAR}, #{item.phone,jdbcType=VARCHAR},
#{item.email,jdbcType=VARCHAR}, #{item.sex,jdbcType=TINYINT}, #{item.locked,jdbcType=TINYINT},
#{item.gmt_created,jdbcType=TIMESTAMP}, #{item.gmt_modified,jdbcType=TIMESTAMP})
insert into student
name,
phone,
email,
sex,
locked,
gmt_created,
#{name,jdbcType=VARCHAR},
#{phone,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR},
#{sex,jdbcType=TINYINT},
#{locked,jdbcType=TINYINT},
#{gmt_created,jdbcType=TIMESTAMP},
3:开始使用mybatis,
3.1:创建mybatis的操作类,请参考https://mybatis.org/mybatis-3/zh/getting-started.html
image.png
这里采用单例模式,来保证 SqlSessionFactory 只被创建一次
package mybatis;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
//sqlSessionFactory 的单例模式
public class MyBatisSessionFactory {
private SqlSessionFactory factory;
/**
* 类级的内部类,也就是静态的成员式内部类,该内部类的实例与外部类的实例
* 没有绑定关系,而且只有被调用到才会装载,从而实现了延迟加载
*/
private static class SingletonHolder {
/**
* 静态初始化器,由JVM来保证线程安全
*/
private static MyBatisSessionFactory instance = new MyBatisSessionFactory();
}
/**
* 私有化构造方法
*/
private MyBatisSessionFactory() {
// 通过配置文件创建 sqlSessionFactory 的实列,sqlSessionFactory 用来生成 sqlSession
String resource = "myBatis/mybatis.config.xml";
try {
InputStream inputStream = Resources.getResourceAsStream(resource);
factory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
public static MyBatisSessionFactory getInstance(){
return SingletonHolder.instance;
}
public SqlSessionFactory getFactory() {
return factory;
}
}
3.2:创建测试类,添加测试方法
package mybatis;
import mybatis.dao.StudentDao;
import mybatis.entity.StudentModel;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Date;
import org.slf4j.Logger;
public class MybatisTest {
private static final Logger logger = LoggerFactory.getLogger(MybatisTest.class);
// 单条插入
@Test
public void insert() {
long start = System.currentTimeMillis();
SqlSession session = myBatisSessionFactory.getInstance().getFactory().openSession();
StudentDao studentDaoMapper = session.getMapper(StudentDao.class);
for (int i = 0; i < 500; i++) {
StudentModel model = buildStudentModel(i);
int result = studentDaoMapper.insert(model);
logger.debug("插入 = " + String.valueOf(result) + ", student_id =" + model.getStudent_id());
}
// 提交
session.commit();
long end = System.currentTimeMillis();
logger.info("时间----" +(end - start));
// insertBatch();
}
// 批量插入
@Test
public void insertBatch() {
long start = System.currentTimeMillis();
SqlSession session = myBatisSessionFactory.getInstance().getFactory().openSession();
StudentDao studentDaoMapper = session.getMapper(StudentDao.class);
ArrayList arrayList = new ArrayList();
for (int i = 0; i < 1500; i++) {
StudentModel model = buildStudentModel(i);
arrayList.add(model);
}
studentDaoMapper.insertBatch(arrayList);
// 提交
session.commit();
long end = System.currentTimeMillis();
logger.info("时间----" +(end - start));
}
// 判断插入值为空,则不插入此值
@Test
public void insertSelective() {
SqlSession session = myBatisSessionFactory.getInstance().getFactory().openSession();
StudentDao studentDaoMapper = session.getMapper(StudentDao.class);
StudentModel model = buildStudentModel(0);
int result = studentDaoMapper.insertSelective(model);
logger.info("插入 = " + String.valueOf(result) + ", student_id =" + model.getStudent_id());
// 提交
session.commit();
}
static double phone = 10000000000.f;
static int name = 1;
static int email = 1;
private StudentModel buildStudentModel(int i) {
StudentModel model = new StudentModel();
model.setName("二天" + name);
model.setPhone(String.valueOf(phone));
model.setSex(Byte.parseByte("1"));
model.setEmail("email" + email);
model.setGmt_created(new Date());
model.setStudent_id(1);
return model;
}
}
测试打印
Reader entry: ����1�
Checking to see if class mybatis.entity.StudentModel matches criteria [is assignable to Object]
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 351877391.
Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@14f9390f]
==> Preparing: insert into student ( name, phone, email, sex, locked, gmt_created, gmt_modified ) values (?, ?, ?, ?, ?, ?, ?)
==> Parameters: 二天1(String), 1.0E10(String), email1(String), 1(Byte), null, 2020-04-11 16:44:43.363(Timestamp), null
<== Updates: 1
Committing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@14f9390f]
四月 11, 2020 4:44:43 下午 mybatis.MybatisTest insert
信息: 循环插入时间----872
测试大量数据插入的有效方法
1500条数据插入的对比
四月 11, 2020 4:48:25 下午 mybatis.MybatisTest insert
信息: 循环插入时间----1827
四月 11, 2020 4:48:25 下午 mybatis.MybatisTest insertBatch
信息: 批量插入时间----418
参考的文章:
https://mybatis.org/mybatis-3/zh/getting-started.html
https://www.cnblogs.com/homejim/p/9613205.html
https://www.cnblogs.com/huangjinyong/p/11209753.html