这操作真心记不住。。。。。还是写一遍加深记忆
1.首先创建user表,添加数据.
create datebase fuck;
use fuck;
// 删除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);
2.创建Maven模块,导入坐标
导入坐标
mysql
mysql-connector-java
8.0.28
org.mybatis
mybatis
3.5.5
junit
junit
4.12
test
org.slf4j
slf4j-api
1.7.20
ch.qos.logback
logback-classic
1.2.3
ch.qos.logback
logback-core
1.2.3
3.编写MyBatis的核心配置文件,并且导入logback.xml文件
(1)mybatis配置文件
并且把相应的数据库信息导入进去
(2) logback.xml文件
[%level] %blue(%d{HH:mm:ss.SSS}) %cyan([%thread]) %boldGreen(%logger{15}) - %msg %n
4.编写pojo实体类,用get 、set方法
package com.fuck.pojo;
public class Brand {
private Integer id;
private String brandName;
private String companyName;
private Integer ordered;
private String description;
private Integer status;
public Integer getId() {
return id;
}
public void setId(Integer 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 Integer getOrdered() {
return ordered;
}
public void setOrdered(Integer ordered) {
this.ordered = ordered;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
@Override
public String toString() {
return "Brand{" +
"id=" + id +
", brandName='" + brandName + '\'' +
", companyName='" + companyName + '\'' +
", ordered=" + ordered +
", description='" + description + '\'' +
", status=" + status +
'}';
}
}
5.创建mapper接口
package com.fuck.mapper;
import com.fuck.pojo.Brand;
import java.util.List;
public interface BrandMapper {
//查询所有
public List selectAll();
}
6.创建SqlSessionFactory工具类
package com.fuck.Utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class SqLSessionFactoryUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
throw new RuntimeException(e);
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
public static SqlSessionFactory getSqlSessionFactory(){
return sqlSessionFactory;
}
}
总的项目结构
7.创建Mapper映射文件,并写入sql查询语句
BrandMapper.xml文件
8. 最后编写测试类
MyBatisTest
package com.fuck.test;
import com.fuck.Utils.SqLSessionFactoryUtils;
import com.fuck.mapper.BrandMapper;
import com.fuck.pojo.Brand;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import java.util.Base64;
import java.util.List;
public class MyBatisTest {
// 调用SqlSessionFactory方法 构建出SqlSessionFactory的实例 factory
SqlSessionFactory factory= SqLSessionFactoryUtils.getSqlSessionFactory();
// 利用注解
@Test
public void testSelectAll(){
// 获取SqlSession
SqlSession sqlSession= factory.openSession();
// 获取BrandMapper
BrandMapper brandmapper=sqlSession.getMapper(BrandMapper.class);
// 调用方法
List brands=brandmapper.selectAll();
// 输出
System.out.println(brands);
// 关闭sqlSession
sqlSession.close();
}
}
运行结果