案例大致文件结构:
BrandMapper接口类,存放有sql的查询方法
pojo下的Brand是实体类
BrandMapper.xml与mybatis-config.xml是配置文件
test包下的myBatisTest是测试类
准备环境
1.数据库表tb_Brand
2.实体类 Brand
3.测试用例
4.安装MyBatisX插件
1.数据库表tb_Brand
2.实体类 Brand
package com.itheima.pojo;
/*
在实体类中,基本数据类型,建议使用其对应的包装类
*/
public class Brand {
//id主键
private Integer id;
//品牌名称
private String brandName;
//企业名称
private String companyName;
//排序字段
private Integer ordered;
//描述信息
private String discription;
//状态 0:禁用 1:启用
private Integer status;
public Brand(Integer id, String brandName, String companyName, Integer ordered, String discription,
Integer status) {
super();
this.id = id;
this.brandName = brandName;
this.companyName = companyName;
this.ordered = ordered;
this.discription = discription;
this.status = status;
}
public Brand() {
}
@Override
public String toString() {
return "Brand [id=" + id + ", brandName=" + brandName + ", companyName=" + companyName + ", ordered=" + ordered
+ ", discription=" + discription + ", status=" + 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 getDiscription() {
return discription;
}
public void setDiscription(String discription) {
this.discription = discription;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
}
4.安装MyBatisX插件(更好用)
1.查询所有数据
(1).编写接口方法Mapper接口 List<Brand> selectAll();
参数:无
返回值结果为:List<Brand>
(2).编写sql语句:sql映射文件
<select id="selectAll" resultType="com.itheima.pojo.Brand">
select * from tb_brand;
</select>
(3).执行方法,测试
(1).编写接口方法Mapper接口 List<Brand> selectAll(); 参数:无 返回值结果为:List<Brand>
package com.itheima.mapper;
/*
使用mapper代理方式完成入门案例
1.定义sql映射文件同名的mapper接口,并且将mapper接口和sql映射文件防止在同一目录下
2.设置sql映射文件的namespace属性为mapper接口全限定名
3.在mapper接口中定义方法,方法名就是sql映射文件中的sql语句中的id,并保持参数类型和返回值类型一致
4.编码
1.通过SqlSession的getMapper方法获取,Mapper接口的代理对象
2.调用对应方法完成sql的执行
细节:如果Mapper接口名称和sql映射文件名称相同,并在同意目录下,则就可以使用包扫描的方式简化sql映射文件的加载
把<mapper resource="com/itheima/mapper/UserMapper.xml"/>更换为<package name="com.itheima.mapper"/>
*/
import com.itheima.pojo.Brand;
import java.util.List;
public interface BrandMapper {
//1.查询所有
public List<Brand> selectAll();
//2.查看详情
}
(2).编写sql语句:sql映射文件
<select id="selectAll" resultType="com.itheima.pojo.Brand">
select * from tb_brand;
</select>
<select id="selectAll" resultType="com.itheima.pojo.Brand">
select * from tb_brand;
</select>
(3).执行方法,测试
(结果是成功的!)但是也可以发现公司名称和商品名称的值为null,这是由于
数据库表的字段名称 和 实体类的属性名 不一样,则不能自动封装数据
接下来我们来准备解决这个问题
有三种方式
1.起别名
2.起别名并且定义sql片段
3.使用resultMap
1.起别名(缺点:太笨重)
<select id="selectAll" resultType="com.itheima.pojo.Brand">
select
id,brand_name as brandName,company_name as companyName,ordered,discription,status
from tb_brand;
</select>
2.起别名并且定义sql片段(缺点:笨重,不灵活)
<sql id="brand_column">
id,brand_name as brandName,company_name as companyName,ordered,discription,status
</sql>
<select id="selectAll" resultType="com.itheima.pojo.Brand">
select
<include refid="brand_column"></include>
from tb_brand;
</select>
3.使用resultMap(缺点:看似不方便,其实方便的一批)
<resultMap id="brandResultMap" type="com.itheima.pojo.Brand">
<result column="brand_name" property="brandName"></result>
<result column="company_name" property="companyName"></result>
</resultMap>
<select id="selectAll" resultMap="brandResultMap">
select
*
from tb_brand;
</select>
对于这个问题做出总结:
数据库表的字段名称 和 实体类的属性名 不一样,则不能自动封装数据 起别名 缺点:每一次都需要重写 定义sql片段(不灵活) ResultMap(以后最常用) id:完成主键字段的映射的 column:数据库表的列名 property:实体类的属性名 result:完成一般字段的映射 resultMap步骤: 1.定义resultMap标签 2.在select标签中把resultType换成resultMap属性