MyBatis 编程进阶~mybatis基于接口方式实现对数据库中的数据进行操作
程序员文章站
2022-06-02 08:21:29
...
1.1. MyBatis基于接口实现的基本步骤
Step01: 创建maven桌面项目并添加依赖
Step02: 创建配置文件config.propertis(内容为数据库相关)
Step03: 创建mybatis核心配置文件mybatis-configs.xml文件
Step04: 配置Mybatis基础数据服务(properties,datasource,mapper)
Step05: 创建映射文件BlogMapper.xml
Step06: 创建实体类Blog(与表对应,可用于封装表中数据)
Step07: 创建BlogDao接口,并添加相关方法.
Step08: 配置BlogMapper映射文件,添加相关元素.
Step09: 基于BlogDao接口与映射文件实现CRUD操作
1.2. MyBatis进阶编程实现
1.2.1. 创建Maven桌面项目
创建maven桌面项目并添加依赖
<!-- 添加mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<!-- 添加mysql驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<!-- 添加junit依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
1.2.2. 创建config.properties文件
在src/main/resources目录下创建config.properties文件,此文件中定义
系统中的一些常用配置信息,例如访问数据库的相关信息,其内容如下
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/cgbmybatis
username=root
password=root
1.2.3. 创建mybatis核心配置文件
在src/main/resources目录下创建mybatis-configs.xml文件,并配置数据源等相关信息,数据信息从config.properties文件读取.
<?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>
<!--配置properties文件-->
<properties resource="config.properties"/>
<!-- 配置初始化环境(连接) -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
</configuration>
1.2.4. 创建Blog实体对象
创建Blog类实现与数据库中Blog表实现映射
public class Blog {
private Integer id;
private String title;
private String content;
private Date createdTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getCreatedTime() {
return createdTime;
}
public void setCreatedTime(Date createdTime) {
this.createdTime = createdTime;
}
@Override
public String toString() {
return "Blog [id=" + id + ", title=" + title + ", content=" + content + ", createdTime=" + createdTime + "]";
}
}
1.2.5. 创建BlogDao接口
创建数据访问接口,并添加相关方法
package com.jt.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.jt.entity.Blog;
public interface BlogDao {
/***
* 根据id进行对象查找
* @param id
* @return
*/
Blog findBlogById(Integer id);
List<Blog> findPageBlogs(
@Param("offset")Integer offset,
@Param("pageSize")Integer pageSize);
int insertObject(Blog blog);
int updateObject(Blog blog);
int deleteObject(Integer id);
}
1.2.6. 创建BlogMapper映射文件
<?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.jt.dao.BlogDao">
</mapper>
BlogMapper文件中添加与BlogDao接口对应的映射元素
添加基于ID进行查询的元素
<select id="findBlogById"
parameterType="int"
resultType="blog">
select *
from blog
where id=#{id}
</select>
添加分页查询元素
<select id="findPageBlogs"
resultType="blog">
select *
from blog
limit #{offset},#{pageSize}
</select>
添加insert元素
<insert id="insertObject"
parameterType="blog">
insert into blog
(id,title,content,createdTime)
values
(null,#{title},#{content},now())
</insert>
添加更新元素
<update id="updateObject"
parameterType="blog">
update blog
set title=#{title},content=#{content}
where id=#{id}
</update>
添加删除元素
<delete id="deleteObject"
parameterType="int">
delete from blog where id=#{id}
</delete>
最后在mybatis-configs.xml中添加BlogMapper文件
1.2.7. 创建单元测试类执行测试
创建单元测试类,并添加相关方法实现基于Dao接口方式的数据库操作.
public class TestBlog01 {
private SqlSessionFactory factory;
@Before
public void init()throws IOException{
factory=new SqlSessionFactoryBuilder()
.build(Resources.getResourceAsStream(
"mybatis-configs.xml"));
}
@Test
public void testFindBlogById(){
//1.创建session
SqlSession session=factory.openSession();
//2.执行sql
//2.1获取dao对象
BlogDao dao=
session.getMapper(BlogDao.class);
//2.2执行dao中方法
Blog blog=dao.findBlogById(1);
System.out.println(blog);
//3.关闭session
session.close();
}
@Test
public void testFindPageBlogs(){
//1.创建session
SqlSession session=factory.openSession();
//2.执行sql
//2.1获取dao对象
BlogDao dao=
session.getMapper(BlogDao.class);
//2.2执行dao中方法
List<Blog> list=dao.findPageBlogs(0, 2);
for(Blog b:list){
System.out.println(b);
}
//3.关闭session
session.close();
}
@Test
public void testInsertObject(){
//1.创建session
SqlSession session=factory.openSession();
//2.执行sql
//2.1获取dao对象
BlogDao dao=
session.getMapper(BlogDao.class);
//2.2执行dao中方法
Blog blog=new Blog();
blog.setTitle("te");
blog.setContent("te...");
dao.insertObject(blog);
session.commit();
//3.关闭session
session.close();
}
}