目录
MyBatis环境初始化
概述
Mybatis是一个优秀的持久层框架,底层基于JDBC实现与数据库的交互。并在JDBC操作的基础上做了封装和优化,它借助灵活的SQL定制,参数及结果集的映射方式,更好的适应了当前互联网技术的发展。Mybatis框架的简单应用架构,
在当今的互联网应用中项目,mybatis框架通常会由spring框架进行资源整合,作为数据层技术实现数据交互操作
创建项目模块
创建项目moudle,其初始pom.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<groupId>com.cy</groupId>
<artifactId>03-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>03-mybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
添加项目依赖
mysql数据库驱动依赖,
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
添加mybatis依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
Mybatis简易配置实现。
假如需要对mybatis框架进行简易配置,可以创建并打开application.properties文件,在此文件中进行基本配置
mybatis.configuration.default-statement-timeout=30
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.mapper-locations=classpath:/mapper/*/*.xml
配置mybatis中的sql日志的输出
logging.level.com.cy=DEBUG
创建项目启动类
package com.cy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyBatisApplication {//Application.class
public static void main(String[] args) {//Main Thread
SpringApplication.run(MyBatisApplication .class, args);
}
}
环境测试代码实现
在src/test/java目录中添加测试类,对mybatis框架整合进行基本测试,代码如下:
package com.cy.pj.sys.dao;
@SpringBootTest
public class MyBatisTests {
@Autowired
private SqlSession sqlSession;
@Test
public void testGetConnection() {
Connection conn=sqlSession.getConnection();
System.out.println("connection="+conn);
}
}
在SpringBoot脚手架工程中,Spring框架会基于MyBatis框架底层配置,创建SqlSessionFactory对象,然后再通过此工厂对象创建SqlSession,最后基于Springku框架为测试类注入SqlSession对象,接下来,我们可以通过SqlSession对象实现与数据库的会话了。如图所示:
公告数据层MyBatis实践
业务描述
基于SpringBoot脚手架工程对MyBatis框架的整合,实现对通告数据的操作。
Pojo类设计
创建SysNotice类,借助此类对象封装公告(通知)数据。
package com.cy.pj.sys.pojo;
public class SysNotice {
/** 公告ID */
private Long id;
/** 公告标题 */
private String title;
/** 公告类型(1通知 2公告) */
private String type;
/** 公告内容 */
private String content;
/** 公告状态(0正常 1关闭) */
private String status;
/** 备注*/
private String remark;
/** 创建时间 */
private Date createdTime;
/** 修改时间*/
private Date modifiedTime;
/** 创建用户 */
private String createdUser;
/** 修改用户*/
private String modifiedUser;
//自己添加set/get/toString方法
}
Dao接口及方法
第一步:定义通告业务数据层接口及业务方法。借助此类型的对象基于MyBatis技术实现与数据库的交互。
package com.cy.pj.notice.dao;
@Mapper
public interface SysNoticeDao {
/**
* 基于条件查询公告信息
* @param notice 用于封装查询参数
* @return 基于查询条件查询到结果
*/
List<SysNotice> selectNotices(SysNotice notice);
/**
* 基于id删除公告信息
* @param id
* @return 删除了几行
* 说明:在jdk8之前,对于接口方法,假如参数有多个或者参数是数组,是不可以
* 直接在sql映射文件中使用参数名的,需要通过@Param这个注解定义参数名,然后
* 在sql映射语句中使用@Param注解中的名字获取参数数据。对于数组而言在sql映射
* 中可以直接使用array进行接收也可以。
*/
int deleteById(@Param("ids") Long... id);//array
/**
* 持久化notice对象数据
* @param notice (封装了要写入的数据)
* @return 返回写入的行数。
*/
int insertNotice(SysNotice notice);
/**
* 基于id查询notice对象
* @param id 公告唯一标识id
* @return 基于id查询到结果
* 建议:简单sql映射语句可以直接写到接口方法上,复杂sql还是推荐写到xml映射文件
*/
@Select("select * from sys_notices where id=#{id}")
SysNotice selectById(Long id);
/**
* 持久化notice对象数据
* @param notice (封装了要更新的数据)
* @return 返回更新的行数。
*/
int updateNotice(SysNotice notice);
}
其中:@Mapper是由MyBatis框架中定义的一个描述数据层接口的的注解(所有的注解只起到一个描述性的作用),用于告诉Spring框架此接口的实现由mybatis创建,并将其实现类对象存储到spring容器。当系统启动时,会对启动类所在包以及子包中的类进行扫描,假如发现接口上有@Mapper注解(mybatis提供),系统底层会基于接口创建其实现类(借助反射包中Proxy类进行创建),在实现类的内部,底层会又基于sqlsession对象实现数据访问和操作。
第二步:创建SysNoticeDao接口对应的SQL映射文件,名字为SysNoticeMapper.xml,存储在resources/mapper/sys目录下,同时定义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.cy.pj.sys.dao.SysNoticeDao">
</mapper>
第三步:在映射文件中添加insertNotice方法对应的Sql映射,关键代码如下:
<insert id="insertNotice" parameterType="com.cy.pj.sys.pojo.SysNotice">
insert into sys_notices
(title,type,content,status,remark,
createdTime,modifiedTime,createdUser,modifiedUser)
values
(#{title},#{type},#{content},#{status},#{remark},
now(),now(),#{createdUser},#{modifiedUser})
</insert>
第四步:在映射文件中定义更新公告对应的SQL映射,关键代码如下:
<update id="updateNotice" parameterType="com.cy.pj.sys.pojo.SysNotice">
update sys_notices
set title=#{title},
content=#{content},
type=#{type},
status=#{status},
remark=#{remark},
modifiedTime=now(),
modifiedUser=#{modifiedUser}
where id=#{id}
</update>
第五步:在映射文件中添加基于条件的SQL查询映射,关键代码如下:
<select id="selectNotices"
parameterType="com.cy.pj.sys.pojo.SysNotice"
resultType="com.cy.pj.sys.pojo.SysNotice">
select *
from sys_notices
<where>
<if test="type!=null and type!=''">
type=#{type}
</if>
<if test="title!=null and title!=''">
and title like concat("%",#{title},"%")
</if>
<if test="modifiedUser!=null and modifiedUser!=''">
and modifiedUser like concat("%",#{modifiedUser},"%")
</if>
</where>
order by createdTime desc
</select>
第六步:在映射文件中定义删除操作对应的SQL映射,关键代码如下:
<delete id="deleteById"><!--(1,2,3,4,5)-->
<!--foreach 用于迭代一个数组或集合-->
delete from sys_notices
<where>
<if test="ids!=null and ids.length>0">
id in
<foreach collection="ids" open="(" close=")" separator="," item="id">
#{id}
</foreach>
</if>
or 1=2
</where>
</delete>
单元测试实现及分析
第一步:在src/java/test目录下定义测试类,对NoticeDao对象进行应用测试。
package com.cy.pj.sys.dao;
@SpringBootTest
public class SysNoticeDaoTests {
@Autowired
private SysNoticeDao sysNoticeDao;
….
}
第二步:在单元测试类中添加insert操作的单元测试方法,代码如下:
@Test
void testInsertNotice(){
//创建SysNotice对象,通过此对象封装要写入到数据库的数据
SysNotice notice=new SysNotice();
notice.setTitle("CGB2011结课时间");
notice.setContent("2021/3/20正式结课");
notice.setStatus("0");
notice.setType("1");
notice.setCreatedUser("tony");
notice.setModifiedUser("tony");
//将SysNotice对象持久化到数据库
sysNoticeDao.insertNotice(notice);
//此方法的实现内部会通过SQLSession向表中写数据。
}
第三步:定义基于id进行查询的单元测试方法,代码如下:
@Test
void testSelectById(){
SysNotice notice=sysNoticeDao.selectById(1L);
System.out.println(notice);
}
第四步:定义基于id执行修改的单元测试方法,代码如下:
@Test
void testUpdateNotice(){
//基于id查询通知对象
SysNotice notice=sysNoticeDao.selectById(1L);
notice.setType("2");
notice.setContent("2021/07/09 春节假期");
notice.setModifiedUser("json");
//将更新以后的内容持久化到数据库
sysNoticeDao.updateNotice(notice);
}
第五步:定义单元测试方法,代码如下:
@Test
void testDeleteById(){
int rows=
sysNoticeDao.deleteById(1,3,4);
System.out.println("rows="+rows);
}
第六步:定义基于条件查询通过信息的单元测试方法,代码如下:
@Test
void testSelectNotices(){
SysNotice notice=new SysNotice();
notice.setType("1");
notice.setTitle("开学");
notice.setModifiedUser("tony");
List<SysNotice> list=sysNoticeDao.selectNotices(notice);
for(SysNotice n:list){
System.out.println(n);
}
}