一、环境准备
创建数据库表(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);
在 com.test.pojo
包下创建 Brand 实体类
public class Brand {
// id 主键
private Integer id;
// 品牌名称
private String brandName;
// 企业名称
private String companyName;
// 排序字段
private Integer ordered;
// 描述信息
private String description;
// 状态:0:禁用 1:启用
private Integer status;
//省略 setter and getter。自己写时要补全这部分代码
}
二、查询所有数据
如上图所示就页面上展示的数据,而这些数据需要从数据库进行查询。接下来我们就来讲查询所有数据功能,而实现该功能我们分以下步骤进行实现:1、编写Mapper接口中对应的方法。2、XML映射配置文件中完成对应的sql语句。
2.1、编写接口方法
public interface BrandMapper {
/**
* 查询所有
*/
List<Brand> selectAll();
}
2.2、编写SQL语句
<?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.itheima.mapper.BrandMapper">
<select id="selectAll" resultType="brand">
select *
from tb_brand;
</select>
</mapper>
2.3、编写测试方法
@Test
public void testSelectAll() throws IOException {
//1. 获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2. 获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//3. 获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4. 执行方法
List<Brand> brands = brandMapper.selectAll();
System.out.println(brands);
//5. 释放资源
sqlSession.close();
}
通过日志查看对应的sql:
从上面结果可以看到 brandName
和 companyName
这两个属性的数据没有封装成功,值为null。
这是因为在实体类中属性名是 brandName
和 companyName
,而表中的字段名为 brand_name
和 company_name。
三、表名和pojo中字段名不一致的解决方式
3.1、起别名解决上述问题
我们可以在写sql语句时给这两个字段起别名,将别名定义成和属性名一致即可。
<select id="selectAll" resultType="brand">
select
id, brand_name as brandName, company_name as companyName, ordered, description, status
from tb_brand;
</select>
3.2、使用resultMap标签解决上述问题
在映射配置文件中(Mapper对应的XML映射文件)使用resultMap定义字段和属性的映射关系
<resultMap id="brandResultMap" type="brand">
<!--
id:完成主键字段的映射
column:表的列名
property:实体类的属性名
result:完成一般字段的映射
column:表的列名
property:实体类的属性名
-->
<result column="brand_name" property="brandName"/>
<result column="company_name" property="companyName"/>
</resultMap>
resultMap标签中的jdbcType这一列可以删除
3.3、开启驼峰命名自动映射
在mybatis-config.xml核心配置文件中配置使用驼峰命名法,3.2中是在XML映射文件,与3.3不同。
<settings>
<!--开启驼峰命名映射-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
总结:
1、实体类属性名和数据库表列名不一致时,不能自动封装数据。
2、起别名:对不一样的列名起别名,别名和实体类属性名一样。
3、使用resultMap标签完成不一致的属性名和列名的映射(常用)
4、开启驼峰命名自动映射(常用)
四、参数占位符
#{} :执行SQL时,会将 #{} 占位符替换为?,将来自动设置参数值。
${} :拼接SQL。底层使用的是 Statement
,会存在SQL注入问题。如下图将 映射配置文件中的 #{} 替换成 ${} 来看效果
五、转义字符
">" 、"<" 等这些字符在xml中有特殊的含义,所以此时我们需要将这些符号进行转义,可以使用以下两种方式进行转义
5.1、<
就是<
的转义字符。>
就是>的转义字符
5.2、<![CDATA[内容]]>
在mapper.xml中输入cd就有提示能输出全部内容
六、 常用标签和参数
6.1、resultMap标签
使用resultMap标签完成不一致的属性名和列名的映射。
<resultMap id="brandResultMap" type="brand">
<!--
id:完成主键字段的映射
column:表的列名
property:实体类的属性名
result:完成一般字段的映射
column:表的列名
property:实体类的属性名
-->
<result column="brand_name" property="brandName"/>
<result column="company_name" property="companyName"/>
</resultMap>
6.2、ParameterType参数
对于有参数的mapper接口方法,我们在映射配置文件中应该配置 ParameterType
来指定参数类型。只不过该属性可以省略
6.3、ResultType参数
ResultType表示查询的每一行数据要封装的数据类型,引用类型要写全类名,只有<select>标签才有此属性。
6.4、@param注解
@param注解的作用主要是在Mapper层中的方法可以使用多个参数,并给这些参数取别名。
注意:
1、在不使用@Param注解的时候,函数的参数只能为一个,并且在查询语句取值时只能用#{}。如果想传递多个参数,parameterType参数类型为map(此处为别名)或者为JavaBean(实体类)。
2、而使用@Param注解则可以使用多个参数,无需再设置parameterType,并且在查询语句中可以使用#{}或者${}
七、动态sql
用户在输入条件时,肯定不会所有的条件都填写,这个时候我们就要使用到动态sql语句。
动态sql语句:
<select id="selectByCondition" resultMap="brandResultMap">
select *
from tb_brand
<where>
<if test="status != null">
and status = #{status}
</if>
<if test="companyName != null and companyName != '' ">
and company_name like concat('%',#{companyName},'%')
</if>
<if test="brandName != null and brandName != '' ">
and brand_name like concat('%',#{brandName},'%')
</if>
</where>
</select>
注意:需要给每个条件前都加上 and 关键字。 因为where 标签 会动态的去掉第一个条件前的 and。
这里的status不是String类型,所以不能在条件中判断:!=' ',这样会报错
7.1、where 标签的作用
1、替换where关键字
2、会动态的去掉第一个条件前的 and
例如前端只传参数companyName,那么就会把company_Name前面的and去掉,变成
select * from tb_brand where company_Name like '%companyName%'
3、如果所有的参数没有值则不加where关键字
7.2、where1=1的使用
使用where1=1,也可以解决动态sql的拼接问题,具体的看JavaWeb(四)中mapper.xml中使用where1=1,以及select(1)的意义
7.3、choose、when标签的使用
choose、when、otherwise标签相当于swith、case、defult
<choose>
<when test="orderBy !=null and orderBy != ''">
${orderBy} ${paixu}
</when>
<otherwise>
a.PENALTY_DAYS
</otherwise>
</choose>
这段sql类似于:
switch
case orderBy !=null and orderBy != ' '’''
defult a.PENALTY_DAYS
八、insert标签
mapper中的代码
int insertCustomerService(CustomerServiceModel customerServiceModel);
mapper.xml中对应的代码:
<insert id="insertCustomerService" useGeneratedKeys="true" keyColumn="id" keyProperty="id" parameterType="com.niwodai.obs.borrow.model.member.CustomerServiceModel">
INSERT INTO t_customer_service_remark(corp_no,user_id,`name`,customer_no, remark)
values(#{corpNo},#{customerId},#{customerName},#{customerNo},#{remark})
</insert>
useGeneratedKeys="true" keyColumn="id" keyProperty="id
的作用:在t_customer_service_remark表中添加完数据后,将主键的值赋值给CustomerServiceModel类中的id字段
keyColumn:表中的列名
keyProperty:类中的属性名
例如在表中新增数据后,id为55,此时就会将55赋值到CustomerServiceModel类中的id字段
九、<foreach标签的使用
mapper中的代码
List<NewRiskResult> getRiskResultByBusiIdList(@Param("busiIdList") List<String> busiIdList);
mapper.xml中的代码
<select id="getRiskResultByBusiIdList" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List" />
FROM t_new_risk_result
where 1=1
<if test="busiIdList!=null and busiIdList.size()>0">
AND busi_id IN
<foreach collection="busiIdList" item="busiId" open="(" close=")" separator="," >
#{busiId}
</foreach>
</if>
</select>
<foreach collection="busiIdList" item="busiId" open="(" close=")" separator="," >
#{busiId}
</foreach>
中代码的作用:
遍历名字为busiIdList的集合,遍历出来的每个元素是busiId,
open="(" close=")" separator=","
表示in 后面的(busiId1,busiId2,busiId3)
“(” 、“)”、“,”
十、trim标签的使用
prefix
:属性用于在生成的 SQL 语句的开头添加一个指定的字符串。通常用于添加 SQL 语句的关键词
suffix
:属性用于在生成的 SQL 语句末尾添加一个指定的字符串。通常用于添加 SQL 语句的结尾关键词
suffixOverrides:属性用于删除出现在生成的 SQL 语句末尾的指定字符串。
<insert id="insert" useGeneratedKeys="true" keyColumn="id" keyProperty="id" parameterType="com.fuwei.borrow.pojo.entity.KycOcrInfo">
INSERT INTO t_kyc_ocr_info
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="null != firstName and '' != firstName">
first_name,
</if>
<if test="null != lastName and '' != lastName">
last_name,
</if>
<if test="null != gender ">
gender,
</if>
<if test="null != nationality and '' != nationality">
nationality,
</if>
<if test="null != idNumber and '' != idNumber">
id_number,
</if>
<if test="null != dateOfBirth and '' != dateOfBirth">
dateOfBirth,
</if>
<if test="null != dateOfIssue and '' != dateOfIssue">
dateOfIssue,
</if>
<if test="null != dateOfExpiry and '' != dateOfExpiry">
dateOfExpiry,
</if>
<if test="null != cuil and '' != cuil">
cuil,
</if>
<if test="null != cult and '' != cult">
cult,
</if>
<if test="null != customerNo and '' != customerNo">
customer_no,
</if>
<if test="null != createTime ">
create_time,
</if>
<if test="null != updateTime ">
update_time
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="null != firstName and '' != firstName">
#{firstName},
</if>
<if test="null != lastName and '' != lastName">
#{lastName},
</if>
<if test="null != gender ">
#{gender},
</if>
<if test="null != nationality and '' != nationality">
#{nationality},
</if>
<if test="null != idNumber and '' != idNumber">
#{idNumber},
</if>
<if test="null != dateOfBirth and '' != dateOfBirth">
#{dateOfBirth},
</if>
<if test="null != dateOfIssue and '' != dateOfIssue">
#{dateOfIssue},
</if>
<if test="null != dateOfExpiry and '' != dateOfExpiry">
#{dateOfExpiry},
</if>
<if test="null != cuil and '' != cuil">
#{cuil},
</if>
<if test="null != cult and '' != cult">
#{cult},
</if>
<if test="null != customerNo and '' != customerNo">
#{customerNo},
</if>
<if test="null != createTime ">
#{createTime},
</if>
<if test="null != updateTime ">
#{updateTime}
</if>
</trim>
</insert>
以这个sql语句为例,组成的sql为:后面的很多字段我都省略了
insert into t_kyc_ocr_info (first_name,last_name) values(firstName,lastName)
<trim prefix="(" suffix=")" suffixOverrides=",">
prefix表示这里的(first_name,last_name),“(”,左括号
suffix表示上面的“)”,右括号
suffixOverrides表示去掉多余的“,”
例如last_name后面如果有“,”就会被直接删除