- 一、Mybatis入门(在maven中操作)
- 二、乱乱的整理知识点
- 三、JUnit单元测试
一、Mybatis入门(在maven中操作)
1.创建数据库表
2.导入依赖
org.mybatis
mybatis
3.5.5
mysql
mysql-connector-java
5.1.46
3.导入mybatis 核心配置文件 – > (配置数据库连接信息)
4.编写mapper.xml 配置文件 —>(编写sql脚本)
update tb_brand set status = #{status} where id=#{id}
figs:
可以在idea中下载个规范插件—MybatisX ,下载成功之后可以检查xml中的id是否存在于接口中
5.编码实现
数据库准备:
-- 删除tb_brand表
drop table if exists tb_brand;
-- 创建tb_brand表
create table tb_brand
(
-- id 主键
id int primary key auto_increment,
-- 品牌名称
brand_name varchar(20),
-- 企业名称
company_name varchar(20),
-- 排序字段
ordered int,
-- 描述信息
description varchar(100),
-- 状态:0:禁用 1:启用
status int
);
-- 添加数据
insert into tb_brand (brand_name, company_name, ordered, description, status)
values ('三只松鼠', '三只松鼠股份有限公司', 5, '好吃不上火', 0),
('华为', '华为技术有限公司', 100, '华为致力于把数字世界带入每个人、每个家庭、每个组织,构建万物互联的智能世界', 1),
('小米', '小米科技有限公司', 50, 'are you ok', 1);
pojo类:
Brand类
package com.itheima.pojo;
import lombok.Data;
public class Brand {
private int id;
private String brandName;
private String companyName;
private int ordered;
private String description;
private int status;
@Override
public String toString() {
return "Brand{" +
"id=" + id +
", brandName='" + brandName + ''' +
", companyName='" + companyName + ''' +
", ordered=" + ordered +
", description='" + description + ''' +
", status=" + status +
'}';
}
public Brand(int id, String brandName, String companyName, int ordered, String description, int status) {
this.id = id;
this.brandName = brandName;
this.companyName = companyName;
this.ordered = ordered;
this.description = description;
this.status = status;
}
public Brand() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBrandName() {
return brandName;
}
public void setBrandName(String brandName) {
this.brandName = brandName;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public int getOrdered() {
return ordered;
}
public void setOrdered(int ordered) {
this.ordered = ordered;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}
mybatis-config.xml
mapper.xml
update tb_brand set status = #{status} where id=#{id}
mapper的接口类
package com.itheima.mapper;
import com.itheima.pojo.Brand;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BrandMapper {
List findBrand();
int updateBrandById(@Param("id") int id,@Param("status") int status);
}
使用单元测试进行测试
在使用单元测试之前要先在pom.xml中引入junit的坐标依赖
junit
junit
4.13
test
package com.itheima;
import com.itheima.mapper.BrandMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.Brand;
import com.itheima.pojo.User;
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.Test;
import java.io.InputStream;
import java.util.List;
public class mapperTest {
@Test
public void testMapper() throws Exception {
InputStream resource = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);
SqlSession sqlSession = sqlSessionFactory.openSession();
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
List brand = mapper.findBrand();
for (Brand brand1 : brand) {
System.out.println(brand1);
}
int i = mapper.updateBrandById(2,1);
System.out.println(i);
sqlSession.commit();
//5. 释放资源
sqlSession.close();
}
}
结果:
1、pom.xml配置文件要导入相应的坐标索引
2、mybatis-config.xml配置文件中的标签是设置的是所要扫描的mapper.xml文件(该文件是存在于resources目录下)
3、想要将mapper.xml文件与mapper接口映射成功,需要在resources目录下建立与mapper接口相应的目录层级,在resources目录下建立目录层级,要用/隔开,不能使用 . 如:com/textcast/mapper
4、在mapper接口与mapper.xml映射成功之后,会在target目录层级中二个出现在一起,如:
5、在pom.xml文件中dependency标签中的scope标签代表坐标的依赖范围
如Junit坐标依赖
junit junit 4.13 test
| 依赖范围 | 编译classpath | 测试classpath | 运行classpath | 例子 |
|---|---|---|---|---|
| compile | Y | Y | Y | logback |
| test | - | Y | - | Junit |
| provided | Y | Y | - | servlet-api |
| runtime | - | Y | Y | jdbc驱动 |
| system | Y | Y | - | 存储在本地的jar包 |
6、使用mybatis代理的要求
定义与SQL映射文件同名的Mapper接口,并且将Mapper接口和SQL映射文件放置在同一目录下
设置SQL映射文件的namespace属性为Mapper接口全限定名
在 Mapper 接口中定义方法,方法名就是SQL映射文件中sql语句的id,并保持参数类型和返回值类型一致
7、可以在mybatis-config 中设置别名,注意标签要放在environments标签的上面,位置放不对会报错
三、JUnit单元测试
1、在pom.xml中导入坐标索引
junit
junit
4.13
test
2、JUnit的三个注释
- @Test
只能放在无参数、无返回值、非静态的方法上 - @Before
在执行被@Test注释的方法之前执行 - @After
在执行完@Test注释的方法之后执行



