①MyBatis快速入门
②Mapper代理开发
③MyBatis核心配置文件
④配置文件完成增删改
案例 完成品牌数据的增删改查操作
产品原型地址:
产品大牛 - 让产品工作更简单(Axure原型托管、Sketch标注)
tb_brand表
-- 删除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);
SELECT * FROM tb_brand;
brand类
package com.itheima.pojo;
/**
* 品牌
*
* alt + 鼠标左键:整列编辑
*
* 在实体类中,基本数据类型建议使用其对应的包装类型
*/
public class Brand {
// id 主键
private Integer id;
// 品牌名称
private String brandName;
// 企业名称
private String companyName;
// 排序字段
private Integer ordered;
// 描述信息
private String description;
// 状态:0:禁用 1:启用
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 +
'}';
}
}
MybatisX
1.查询
1.1.查询所有数据
1.2.查看详情
1.3.条件查询
散装参数方法:
局部细节:
整体:
缺点: 必须完全输入三个条件才能查询,过于死板
解决方法: 运用动态SQL
BrandMapper.xml
BrandMapper.xml
2.添加
BrandMapper.xml
MyBatisTest.java
或者在获取sqlSession对象时,传入参数true,则会自动提交事务,则不用手动提交(sqlSession.commit();)
BrandMapper.xml
3.修改
3.1.修改全部字段
BrandMapper.xml
缺点:不够灵活,只能全部修改,不能单一修改
3.2.修改动态字段
BrandMapper.xml
<update id="update">
update tb_brand
<set>
<if test="brandName!=null and brandName!= '' ">
brand_name=#{brandName},
</if>
<if test="companyName!=null and companyName!= '' ">
company_name=#{companyName},
</if>
<if test="ordered!=null ">
ordered=#{ordered},
</if>
<if test="description!=null and description!= '' ">
description=#{description},
</if>
<if test="status!=null">
status=#{Status}
</if>
</set>
Where id=#{id};
</update>
4.删除
4.1.删除一个
BrandMapper.xml
<delete id="deleteByid">
Delete from tb_brand where id=#{id};
</delete>
4.2.批量删除
MyBatis参数传递
⑤注解完成增删改查
UserMapper.java
所学内容均来自于黑马程序员
所学内容均来自于黑马程序员
所学内容均来自于黑马程序员