创建一个空的maven项目后如下:
2. 添加依赖
IDEA为我们自动生成了pom.xml文件,我需要在里面添加相应的依赖,
Mybatis依赖、MySQL依赖、junit依赖
3. 构建
其他plugin信息不再赘述完整的pom.xml文件如下:(pom文件的配置基本都一样,我们以后可以粘贴复制之,根据自己的情况做依赖添加或修改)
pom.xml:
4.0.0
com.chang
mybatis-test-03
1.0.0
1.8
org.mybatis
mybatis
3.2.8
mysql
mysql-connector-java
5.1.37
junit
junit
4.11
org.apache.maven.plugins
maven-compiler-plugin
3.1
UTF-8
${java}
org.apache.maven.plugins
maven-dependency-plugin
3.1.1
copy-dependencies
package
copy-dependencies
${project.build.directory}/lib
runtime
org.apache.maven.plugins
maven-jar-plugin
3.0.2
true
com.mybatis.demo.Demo
true
lib
${project.name}
${project.version}
${project.groupId}
4. 在src/main/resources添加一个mybatis-config.xml
我们要使用Mybatis,就是要和数据库打交道,所以我们把之间通过JDBC链接的方式,放到mybatis-config.xml中,内容如下:
5. 编写实体类,放在entity,实体类是跟我们数据库表对应的类
在Mybatis中,我们通过一个类和一个数据库表对应连接,在memo数据库中,memoGroup表(id, name , created_time, modify_time)所对应的实体类如下:
MemoGroup.java
package com.chang.entity;
import java.util.Date;
public class MemoGroup {
private Integer id;
private String name;
private Date createdTime;
private Date modifyTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCratedDate(Date date) {
return createdTime;
}
public void setCratedDate(Date cratedDate) {
this.createdTime = cratedDate;
}
public Date getModifyDate() {
return modifyTime;
}
public void setModifyDate(Date modifyDate) {
this.modifyTime = modifyDate;
}
@Override
public String toString() {
return "memoGroup{" +
"id=" + id +
", name='" + name + '\'' +
", createdTime=" + createdTime +
", modifyTime=" + modifyTime +
'}';
}
}
6. 编写映射,mapper包中,定一个mapper映射接口
实体类我们写好了,接下来我们需要一些方法来操作实体类与数据库的交互,我们创建一个MemoGroupMapper接口:
MemoGroupMapper.java
package com.chang.mapper;
import com.chang.entity.MemoGroup;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* Author:
* Created: 2018/6/24
*/
public interface MemoGroupMapper {
/**
* 查询所有便签组
*
* @return
*/
List queryAll();
/**
* 插入一个便签组
*
* @param memoGroup
* @return
*/
int insetMemoGroup(MemoGroup memoGroup);
/**
* 修改便签组的名称
*
* @param id
* @param name
* @return
*/
int updateMemoGroup(@Param("id") int id, @Param("name") String name);
/**
* 根据Id删除便签组
*
* @param id
* @return
*/
int deleteMemoGroup(int id);
}
7. 编写映射,src/main/resources/mapper 定义mapper.xml
接口我们写好了,但是我们不需要自己写实现类,我们通过一个xml文件,通过映射的方式来实现抽象方法,在main/resources下创建一个mapper目录,用于存放我们的映射文件MemoGroupMapper.xml
MemoGroupMapper.xml
insert into memo_group (name, created_time) values (#{name}, #{createdTime})
update memo_group
set name = #{name}
where id = #{id}
delete from memo_group
where id = #{id}
8. 创建SqlSessionFactory工厂类,使用SqlSessionFactory工厂类打开SqlSession进行操作。
基本映射我们已经写完了,接下来我们要通过一个名为的SqlSession类来实现数据库的操作,首先创建一个测试类,在test/java下创建包com.chang,创建一个MemoGroupMapperTest测试类
MemoGroupMapperTest.java
package com.chang;
import com.chang.entity.MemoGroup;
import com.chang.mapper.MemoGroupMapper;
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.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.Date;
import java.util.List;
public class MemoGroupMapperTest {
public static SqlSessionFactory sqlSessionFactory;
@Before
public void beforeTest(){
SqlSessionFactoryBuilder sqlSessionFactoryBuilder =
new SqlSessionFactoryBuilder();
try{
sqlSessionFactory = sqlSessionFactoryBuilder.build(
Resources.getResourceAsStream("mybatis-config.xml"));
} catch (IOException e) {
e.printStackTrace();
}
}
@AfterClass
public static void setAfter() {
sqlSessionFactory = null;
}
@Test
public void testQueryAll(){
SqlSession sqlSession = null;
try{
sqlSession = sqlSessionFactory.openSession();
MemoGroupMapper memoGroupMapper = sqlSession.getMapper(MemoGroupMapper.class);
List list = memoGroupMapper.queryAll();
Iterator iterator = list.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}finally {
if(sqlSession!=null){
sqlSession.close();
}
}
}
@Test
public void testInsetMemoGroup(){
SqlSession sqlSession = null;
try{
MemoGroup memoGroup = new MemoGroup();
memoGroup.setName("DD");
memoGroup.setCratedDate(new Date());
sqlSession = sqlSessionFactory.openSession();
MemoGroupMapper memoGroupMapper = sqlSession.getMapper(MemoGroupMapper.class);
int effect = memoGroupMapper.insetMemoGroup(memoGroup);
Assert.assertEquals(1,effect);
sqlSession.commit();
}finally {
if(sqlSession!=null){
sqlSession.close();
}
}
}
@Test
public void testUpdateMemoGroup(){
SqlSession sqlSession = null;
try{
sqlSession = sqlSessionFactory.openSession();
MemoGroupMapper memoGroupMapper = sqlSession.getMapper(MemoGroupMapper.class);
int effect = memoGroupMapper.updateMemoGroup(5,"CC");
Assert.assertEquals(1,effect);
sqlSession.commit();
}finally {
if(sqlSession!=null){
sqlSession.close();
}
}
}
@Test
public void testDeleteMemoGroup(){
SqlSession sqlSession = null;
try{
sqlSession = sqlSessionFactory.openSession();
MemoGroupMapper memoGroupMapper = sqlSession.getMapper(MemoGroupMapper.class);
int effect = memoGroupMapper.deleteMemoGroup(7);
Assert.assertEquals(1,effect);
sqlSession.commit();
}finally {
if(sqlSession!=null){
sqlSession.close();
}
}
}
}
做一个查询测试:
============================================================================
到此,我们一个简单的Mybatis项目终于构建完成。