mybatis简介
MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以对配置和原生Map使用简单的 XML 或注解,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
开发环境
软件列表
软件 | 版本 |
---|---|
eclipse | JavaEE版本 |
mysql | 5.7 |
JDK | 1.8 |
mybatis | 3.4.4 |
maven | 3.5.0 |
数据库配置
- 创建一个名为mybatis的数据库,并设置编码为UTF-8,然后再创建一个名为activity的活动表,该表包含活动标题,活动图片,活动描述,活动开始时间,活动结束时间等信息。执行代码如下:
#登录mysql,打开命令窗口输入一下命令
mysql -uroot -proot
#创建名为mybatis的数据库
create database if not exists mybatis character set utf8 collate utf8_general_ci;
#切换数据库/使用数据库
use mybatis;
#创建activity表
create table if not exists activity(
id int not null auto_increment,
title varchar(25) not null default '',
description text not null,
imgPath varchar(255) not null,
start_time datetime not null default now(),
end_time datetime not null default now(),
primary key(id),
unique key(imgPath)
)engine=innodb charset=utf8 comment "活动表" auto_increment=10;
#向表activity中插入一条数据
insert into activity(title,description,imgPath) value("爱美丽","爱美丽","www.zhq.com/1.png");
项目配置
- 创建一个名为mybatisLearn的Java项目,然后在项目目录下创建一个名为lib的文件夹用来放mybatis-3.4.4.jar和mysql-connector-java-6.0.5-bin.jar文件并把两个jar包添加到构建环境配置内
- 在source目录下创建一个名为com.zhq.pojo包用来放置简单对象的实体类,在该包下创建Activity类代码如下:
package com.zhq.pojo;
import java.util.Date;
public class Activity {
private int id;
private String title;
private String note;
private String imgPath;
private Date startTime;
private Date endTime;
public Activity() {
super();
}
public Activity(int id, String title, String note, String imgPath, Date startTime, Date endTime) {
super();
this.id = id;
this.title = title;
this.note = note;
this.imgPath = imgPath;
this.startTime = startTime;
this.endTime = endTime;
}
public Activity(String title, String note, String imgPath, Date startTime, Date endTime) {
super();
this.title = title;
this.note = note;
this.imgPath = imgPath;
this.startTime = startTime;
this.endTime = endTime;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public String getImgPath() {
return imgPath;
}
public void setImgPath(String imgPath) {
this.imgPath = imgPath;
}
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
@Override
public String toString() {
return "Activity [id=" + id + ", title=" + title + ", note=" + note + ", imgPath=" + imgPath + ", startTime="
+ startTime + ", endTime=" + endTime + "]";
}
}
3.在source目录下创建一个包名为com.zhq.mapper用来存放mapper的配置文件,并在该包下创建一个名为ActivityMapper.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="com.zhq.dao.ActivityDao">
<select id="selectAll" resultType="Activity">
select * from activity;
</select>
<select id="selectById" parameterType="int" resultType="Activity">
select * from activity where id = #{id};
</select>
</mapper>
4.设置mybatis配置文件信息,在source目录下创建一个名为mybatis_config.xml的文件,文件内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--设置类别名-->
<typeAliases>
<typeAlias alias="Activity" type="com.zhq.pojo.Activity"/>
</typeAliases>
<!--配置数据源信息及开发环境配置-->
<environments default="development">
<environment id="development">
<!--事务管理配置-->
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!--数据库驱动-->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<!--数据库连接地址-->
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis" />
<!--数据库用户名-->
<property name="username" value="root"/>
<!--数据库密码-->
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!--配置要映射的配置文件-->
<mappers>
<mapper resource="com/zhq/mapper/ActivityMapper.xml"/>
</mappers>
</configuration>
测试
1.创建一个名为com.zhq.test包用做测试,创建一个名为ActivityTest测试类并导入junit测试包。代码如下:
package com.zhq.test;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.io.Reader;
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.Assert;
import org.junit.Before;
import org.junit.Test;
import com.zhq.pojo.Activity;
public class ActivityTest {
private static SqlSessionFactory sqlSessionFactory;
private static Reader reader;
@Before
public void setUp() throws Exception {
try {
reader = Resources.getResourceAsReader("mybatis_config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void test() {
SqlSession session = sqlSessionFactory.openSession();
Activity activity = session.selectOne("com.zhq.dao.ActivityDao.selectById",10);
Assert.assertNotEquals("查找成功", "爱美丽", activity.getTitle());
}
}
网友评论