网站建设选谋者,网站开发与电子商务,凡科网微信小程序制作,徐州商城网站建设文章目录 1. MyBatis概述2. MyBatis快速入门2.1 创建项目2.2 添加依赖2.3 数据准备2.4 编写代码2.4.1 编写核心配置文件2.4.2 编写SQL映射文件2.4.3 编写Java代码 3. Mapper代理开发4. MyBatis核心配置文件5. 案例练习5.1 数据准备5.2 查询数据5.2.1 查询所有数据5.2.2 查询单条… 文章目录 1. MyBatis概述2. MyBatis快速入门2.1 创建项目2.2 添加依赖2.3 数据准备2.4 编写代码2.4.1 编写核心配置文件2.4.2 编写SQL映射文件2.4.3 编写Java代码 3. Mapper代理开发4. MyBatis核心配置文件5. 案例练习5.1 数据准备5.2 查询数据5.2.1 查询所有数据5.2.2 查询单条数据详情5.2.3 条件查询5.2.3.1 固定多条件查询5.2.3.2 动态多条件查询5.2.3.3 动态单条件查询 5.3 添加数据5.4 修改数据5.5 删除数据5.5.1 删除单条数据5.5.2 批量删除数据 6. Mybatis参数传递6.1 多个参数6.2 单个参数 7. 注解实现CRUD 1. MyBatis概述
1.MyBatis概念MyBatis是一款优秀的持久层框架用于简化JDBC开发。
它支持自定义 SQL、存储过程以及高级映射免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJOPlain Old Java Objects普通老式 Java 对象为数据库中的记录 注JavaEE分为三层架构表现层、业务层和持久层持久层是负责将数据保存到数据库的那一层代码。 2.MyBatis文档 https://mybatis.net.cn/
3.MyBatis简化了JDBC的开发免除了JDBC 代码以及设置参数和获取结果集的工作
硬编码通过配置文件简化 注册驱动获取连接SQL语句 操作繁琐MyBatis自动完成 设置参数封装结果集
2. MyBatis快速入门
MyBatis开发步骤
导入MyBatis和数据库驱动等相关依赖创建数据表以及该表对应的实体类编写MyBatis核心配置文件用于替换连接信息解决硬编码问题编写SQL映射文件用于统一管理SQL语句解决硬编码问题编写代码 加载核心配置文件获取SqlSessionFactory对象获取SqlSession对象执行SQL语句释放资源
2.1 创建项目
1.New Project创建新项目 2.填写项目名称等信息Build System勾选Maven创建maven项目 3.修改maven配置修改settings.xml内容和IDEA配置 注settings.xml路径如下apache-maven-3.9.3\conf\settings.xml 修改settings.xml内容如下
?xml version1.0 encodingUTF-8?
settings xmlnshttp://maven.apache.org/SETTINGS/1.2.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsdlocalRepositoryD:\repository/localRepositorymirrorsmirroridnexus-aliyun/idmirrorOfcentral/mirrorOfnameNexus aliyun/nameurlhttp://maven.aliyun.com/nexus/content/groups/public/url/mirror/mirrors
/settings修改IDEA配置如下 2.2 添加依赖
1.查找mybatis依赖配置
查看mybatis快速入门文档得到mybatis依赖配置如下https://mybatis.net.cn/getting-started.html
dependencygroupIdorg.mybatis/groupIdartifactIdmybatis/artifactIdversionx.x.x/version
/dependency2.查找mysql驱动依赖配置
打开cmd输入mysql -V 查看mysql版本此处我的版本是 8.0.32 mvnrepository官网查找该版本对应的mysql驱动https://mvnrepository.com/
!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --
dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.32/version
/dependency3.复制依赖代码到pom.xml中最终pom.xml代码如下
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.itheima/groupIdartifactIdmybatis/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependencies!--mybatis依赖--dependencygroupIdorg.mybatis/groupIdartifactIdmybatis/artifactIdversion3.5.13/version/dependency!--mysql驱动--dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.32/version/dependency!--junit单元测试--dependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.13.1/versionscopetest/scope/dependency/dependencies/project2.3 数据准备
1.创建数据库
-- 创建mybatis数据库
create database if not exists mybatis;2.创建表
-- 删除tb_user表
drop table if exists tb_user;-- 创建tb_user表
create table tb_user (id int primary key auto_increment, -- id主键username varchar(20), -- 用户名password varchar(20), -- 密码gender varchar(1), -- 性别addr varchar(30)-- 地址
);-- 向tb_user表插入数据
insert into tb_user values (1, 张三 , 123, 男, 北京);
insert into tb_user values (2, 李四 , 456, 女, 天津);
insert into tb_user values (3, 王五 , 123456, 男, 西安);-- 查询tb_user表
select * from tb_user; 3.在 com.itheima包下新建 pojo 包并在 pojo 包下创建实体类 User对应tb_user表
package com.itheima.pojo;/*** 用户类* 在实体类中基本数据类型建议使用其对应的包装类型*/
public class User {private Integer id; // id主键private String username; // 用户名private String password; // 密码private String gender; // 性别private String addr; // 地址public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public String getUsername() {return username;}public void setUsername(String username) {this.username username;}public String getPassword() {return password;}public void setPassword(String password) {this.password password;}public String getGender() {return gender;}public void setGender(String gender) {this.gender gender;}public String getAddr() {return addr;}public void setAddr(String addr) {this.addr addr;}Overridepublic String toString() {return User{ id id , username username \ , password password \ , gender gender \ , addr addr \ };}
}
2.4 编写代码
2.4.1 编写核心配置文件
resources 目录下新建 mybatis-config.xml 作为核心配置文件内容如下 注数据库的连接信息根据自己本地电脑的数据库信息填写 ?xml version1.0 encodingUTF-8 ?
!DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd
configurationenvironments defaultdevelopmentenvironment iddevelopmenttransactionManager typeJDBC/dataSource typePOOLED!--数据库连接信息--property namedriver valuecom.mysql.cj.jdbc.Driver/property nameurl valuejdbc:mysql://127.0.0.1:3306/mybatis?useSSLfalse/property nameusername valueroot/property namepassword value123456//dataSource/environment/environmentsmappers!--加载sql映射文件--mapper resourceUserMapper.xml//mappers
/configuration2.4.2 编写SQL映射文件
resources 目录下新建 UserMapper.xml作为用户表sql映射文件需要与mybatis-config.xml中mapper标签中填写的文件名一致。
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd!--namespace: 名称空间--
mapper namespacetestselect idselectAll resultTypecom.itheima.pojo.Userselect * from tb_user;/select
/mapper2.4.3 编写Java代码
在com.itheima包下新建MybatisDemo.java内容如下
package com.itheima;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 java.io.IOException;
import java.io.InputStream;
import java.util.List;/*** Mybatis快速入门代码*/
public class MybatisDemo {public static void main(String[] args) throws IOException {// 1.加载mybatis核心配置文件获取SqlSessionFactoryString resource mybatis-config.xml;InputStream inputStream Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);// 2.从SqlSessionFactory中获取SqlSession用它来执行sql语句SqlSession sqlSession sqlSessionFactory.openSession();// 3.执行sql语句ListUser users sqlSession.selectList(test.selectAll); // 名称空间.id为唯一标识System.out.println(users);// 4.释放资源sqlSession.close();}
}
执行代码运行结果如下 最终项目结构如下 总结
mybatis-config.xml 为核心配置文件存在着数据库连接信息以及需要加载的映射文件信息UserMapper.xmlUserMapper.xml为SQL映射文件此处用于编写和管理SQL语句并对每个SQL语句提供唯一的标识由 名称空间.id 组成如 test.selectAllMybatisDemo.javamybatis的程序执行入口代码通过执行方法 sqlSession.selectList(test.selectAll)执行 test.selectAll 对应的SQL语句 select * from tb_user;
3. Mapper代理开发
使用Mapper代理开发的优势
解决原生方式中的硬编码简化后期执行SQL
原生方式
ListUser users sqlSession.selectList(test.selectAll);
System.out.println(users);Mapper代理开发方式 UserMapper userMapper sqlSession.getMapper(UserMapper.class);ListUser users userMapper.selectAll();System.out.println(users);使用Mapper代理开发方式完成入门案例
定义与SQL映射文件同名的Mapper接口并且将Mapper接口和SQL映射文件放置在同一目录下设置SQL映射文件的namespace属性Mapper接口全限定名在Mapper接口中定义方法方法名就是SQL映射文件中sql语句的id并保持参数类型和返回值类型一致编码 通过SqlSession的getMapper方法获取Mapper接口的代理对象调用对应方法完成sql的执行 注如果Mapper接口名称和SQL映射文件名称相同并在同一目录下则可以使用包扫描的方式简化SQL映射文件的加载 1.定义与SQL映射文件同名的Mapper接口并且将Mapper接口和SQL映射文件放置在同一目录下
在 com.itheima包下新建 mapper 包并新建UserMapper的接口在resource目录下新建目录层次结构 com/itheima/mapper然后把 UserMapper.xml 拖到该目录下 将UserMapper的接口的目录结构与UserMapper.xml都放在在com/itheima/mapper目录下是为了保证在maven工程编译之后UserMapper的接口和 UserMapper.xml会打包在同一目录下 编译前的结构 编译后的结构 2.设置SQL映射文件 UserMapper.xml的namespace属性为Mapper接口全限定名 com.itheima.mapper.UserMapper
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd!--namespace: 名称空间--
mapper namespacecom.itheima.mapper.UserMapperselect idselectAll resultTypecom.itheima.pojo.Userselect * from tb_user;/select
/mapper
3.在Mapper接口中定义方法方法名就是SQL映射文件中sql语句的id并保持参数类型和返回值类型一致
方法名为selectAll()对应的是 UserMapper.xml的idselectAll返回类型为 User 对象的集合即 ListUser 类型
package com.itheima.mapper;import com.itheima.pojo.User;import java.util.List;public interface UserMapper {ListUser selectAll();
}
4.由于 UserMapper.xml路径发生了变化所以也要相应的修改 mybatis-config.xml的sql映射文件路径为 com/itheima/mapper/UserMapper.xml
?xml version1.0 encodingUTF-8 ?
!DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd
configurationenvironments defaultdevelopmentenvironment iddevelopmenttransactionManager typeJDBC/dataSource typePOOLED!--数据库连接信息--property namedriver valuecom.mysql.cj.jdbc.Driver/property nameurl valuejdbc:mysql://127.0.0.1:3306/mybatis?useSSLfalse/property nameusername valueroot/property namepassword value123456//dataSource/environment/environmentsmappers!--加载sql映射文件--mapper resourcecom/itheima/mapper/UserMapper.xml//mappers
/configuration 注由于Mapper接口名称和SQL映射文件名称相同且在同一目录下那么也可以使用包扫描的方式简化SQL映射文件的加载 !--一般方式加载sql映射文件--
mapper resourcecom/itheima/mapper/UserMapper.xml/以上一般方式可以替换为包扫描方式
!--包扫描方式加载sql映射文件--
package namecom.itheima.mapper/5.编码
通过SqlSession的getMapper方法获取Mapper接口的代理对象调用对应方法完成sql的执行
package com.itheima;import com.itheima.mapper.UserMapper;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;/*** Mybatis快速入门代码*/
public class MybatisDemo {public static void main(String[] args) throws IOException {// 1.加载mybatis核心配置文件获取SqlSessionFactoryString resource mybatis-config.xml;InputStream inputStream Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);// 2.从SqlSessionFactory中获取SqlSession用它来执行sql语句SqlSession sqlSession sqlSessionFactory.openSession();// 3.获取UserMapper接口的代理对象UserMapper userMapper sqlSession.getMapper(UserMapper.class);// 4.执行sql语句ListUser users userMapper.selectAll();System.out.println(users);// 5.释放资源sqlSession.close();}
}
执行代码运行结果如下 4. MyBatis核心配置文件
MyBatis核心配置文件说明见官网https://mybatis.net.cn/configuration.html 注配置各个标签时需要遵守前后顺序这个顺序就是以上截图的顺序 1.environments 环境配置MyBatis 可以配置成适应多种环境这种机制有助于将 SQL 映射应用于多种数据库之中 现实情况下有多种理由需要这么做。例如开发、测试和生产环境需要有不同的配置或者想在具有相同 Schema 的多个生产数据库中使用相同的 SQL 映射。还有许多类似的使用场景。
例如以下配置表示有开发和测试两个环境的数据库信息默认使用开发环境的数据库信息。 如果想切换默认的数据库信息则修改default属性为对应的environment 配置的id即可比如defaulttest表示默认使用测试数据库的环境信息 !--
environments
1.配置数据库连接环境信息可以配置多个environment
2.通过default属性切换不同的environment
--
environments defaultdevelopment!--开发环境数据库信息--environment iddevelopmenttransactionManager typeJDBC/dataSource typePOOLED!--数据库连接信息--property namedriver valuecom.mysql.cj.jdbc.Driver/property nameurl valuejdbc:mysql://127.0.0.1:3306/mybatis?useSSLfalse/property nameusername valueroot/property namepassword value123456//dataSource/environment!--测试环境数据库信息--environment idtesttransactionManager typeJDBC/dataSource typePOOLED!--数据库连接信息--property namedriver valuecom.mysql.cj.jdbc.Driver/property nameurl valuejdbc:mysql://127.0.0.1:3306/mybatis?useSSLfalse/property nameusername valueroot/property namepassword value123456//dataSource/environment
/environments2.typeAliases类型别名可为 Java 类型设置一个缩写名字。 它仅用于 XML 配置意在降低冗余的全限定类名书写。
例如原先 UserMapper.xml中的 resultType 属性为 com.itheima.pojo.User
!--namespace: 名称空间--
mapper namespacecom.itheima.mapper.UserMapperselect idselectAll resultTypecom.itheima.pojo.Userselect * from tb_user;/select
/mapper使用 typeAliases
首先修改核心配置文件 mybatis-config.xml中的typeAliases配置然后修改 UserMapper.xml中的 resultType 属性
mybatis-config.xml
typeAliasespackage namecom.itheima.pojo/
/typeAliasesUserMapper.xml
!--namespace: 名称空间--
mapper namespacecom.itheima.mapper.UserMapperselect idselectAll resultTypeUserselect * from tb_user;/select
/mapper5. 案例练习
5.1 数据准备
需求能够使用映射配置文件实现CRUD操作此处是对商品品牌数据的增删改查操作 如上图所示产品原型里面包含了品牌数据的 查询 、按条件查询、添加、删除、批量删除、修改 等功能而这些功能其实就是对数据库表中的数据进行CRUD操作。接下来我们就使用Mybatis完成品牌数据的增删改查操作。以下是我们要完成功能列表
查询 查询所有数据查询详情条件查询 添加修改 修改全部字段修改动态字段 删除 删除一个批量删除
1.创建数据库表 tb_brand
-- 删除tb_brand表
drop table if exists tb_brand;-- 创建tb_brand表
create table tb_brand (id int primary key auto_increment, -- id主键brand_name varchar(20), -- 品牌名称company_name varchar(20), -- 企业名称ordered int, -- 排序字段description varchar(100), -- 描述信息status int -- 状态0禁用 1启用
);-- 向tb_brand表插入数据
insert into tb_brand (brand_name, company_name, ordered, description, status)
values (三只松鼠, 三只松鼠股份有限公司, 5, 好吃不上火, 0),(华为, 华为技术有限公司, 100, 华为致力于把数字世界带入每个人、每个家庭、每个组织构建万物互联的智能世界, 1),(小米, 小米科技有限公司, 50, are you ok, 1);-- 查询tb_brand表
select * from tb_brand; 2.在 com.itheima.pojo 包下创建 Brand 实体类
package com.itheima.pojo;/*** 品牌* 在实体类中基本数据类型建议使用其对应的包装类型*/
public class Brand {private Integer id; // id 主键private String brandName; // 品牌名称private String companyName; // 企业名称private Integer ordered; // 排序字段private String description; // 描述信息private Integer status; // 状态0禁用 1启用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;}Overridepublic String toString() {return Brand{ id id , brandName brandName \ , companyName companyName \ , ordered ordered , description description \ , status status };}
}
3.编写测试用例在 test/java 目录下创建包 com.itheima.test 以及测试用例类 MybatisTest
项目结构如下 4.编写MyBatis核心配置文件在 resources 目录下创建 mybatis-config.xml文件内容如下 注数据库的连接信息根据自己本地电脑的数据库信息填写 ?xml version1.0 encodingUTF-8 ?
!DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd
configuration!--类型别名--typeAliasespackage namecom.itheima.pojo//typeAliases!--environments配置数据库连接环境信息可以配置多个environment--environments defaultdevelopment!--开发环境数据库信息--environment iddevelopmenttransactionManager typeJDBC/dataSource typePOOLED!--数据库连接信息--property namedriver valuecom.mysql.cj.jdbc.Driver/property nameurl valuejdbc:mysql://127.0.0.1:3306/mybatis?useSSLfalse/property nameusername valueroot/property namepassword value123456//dataSource/environment/environments!--加载sql映射文件--mapperspackage namecom.itheima.mapper//mappers/configuration
5.安装 MyBatisX 插件
MybatisX 是一款基于 IDEA 的快速开发插件为效率而生功能如下
XML映射配置文件 和 接口方法 间相互跳转根据接口方法生成 statement
安装方式点击 File - Settings - Plugins - 搜索 MybatisX 就能看到如下图所示界面点击 install 安装。 插件效果
红色头绳的表示映射配置文件蓝色头绳的表示mapper接口。在mapper接口点击红色头绳的小鸟图标会自动跳转到对应的映射配置文件在映射配置文件中点击蓝色头绳的小鸟图标会自动跳转到对应的mapper接口。 也可以在mapper接口中定义方法自动生成映射配置文件中的 statement 如图所示 5.2 查询数据
5.2.1 查询所有数据
如图所示就是页面上展示的数据而这些数据需要从数据库进行查询 接下来我们就来讲查询所有数据功能而实现该功能我们分以下步骤进行实现
编写接口方法编写SQL语句编写测试方法
1.编写接口方法在 com.itheima.mapper 包写创建名为 BrandMapper 的接口并在该接口中定义 ListBrand selectAll() 方法。
package com.itheima.mapper;import com.itheima.pojo.Brand;import java.util.List;public interface BrandMapper {/*** 查询所有*/ListBrand selectAll();}
2.编写SQL语句在 reources 目录下创建 com/itheima/mapper 目录结构并在该目录下创建名为 BrandMapper.xml 的映射配置文件
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd!--namespace: 名称空间--
mapper namespacecom.itheima.mapper.BrandMapperselect idselectAll resultTypebrandselect * from tb_brand;/select
/mapper
3.编写测试方法在MybatisTest类中编写如下代码
package com.itheima.test;import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
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.IOException;
import java.io.InputStream;
import java.util.List;public class MybatisTest {Testpublic void testSelectAll() throws IOException {// 1.获取SqlSessionFactoryString 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.执行方法ListBrand brands brandMapper.selectAll();System.out.println(brands);// 5.释放资源sqlSession.close();}
}
运行结果如下 根据上述运行结果可以看到id、ordered、description 和 status 数据封装成功能够正常打印数据但是 brandName 和 companyName 数据自动封装失败。
原因是因为实体类中的属性名和数据库表中的字段名不一致导致的查询 实体类 和 表中的字段 发现在实体类中属性名是 brandName 和 companyName 而表中的字段名为 brand_name 和 company_name如下图所示 。 解决方案只需要保持这两部分的名称一致这个问题就迎刃而解有以下两种方式可以解决
给字段起别名使用resultMap定义字段和属性的映射关系推荐
1.给字段取别名
编写SQL语句时给字段取别名将别名定义成与属性名一致即可
mapper namespacecom.itheima.mapper.BrandMapperselect idselectAll resultTypebrandselectid, brand_name as brandName, company_name as companyName, ordered, description, statusfrom tb_brand;/select
/mapper上面的SQL语句中的字段列表书写麻烦如果表中还有更多的字段同时其他的功能也需要查询这些字段时就显得我们的代码不够精炼Mybatis提供了sql 片段可以提高sql的复用性。
mapper namespacecom.itheima.mapper.BrandMapper!--定义sql片段id属性值是唯一标识--sql idbrand_columnid, brand_name as brandName, company_name as companyName, ordered, description, status/sql!--使用include标签引用sql片段refid指定sql片段的id值--select idselectAll resultTypebrandselectinclude refidbrand_column /from tb_brand;/select
/mapper注起别名 sql片段的方式可以解决上述问题但是它也存在问题。如果还有功能只需要查询部分字段而不是查询所有字段那么我们就需要再定义一个 SQL 片段这就显得不是那么灵活。 2.使用resultMap定义字段和属性的映射关系
mapper namespacecom.itheima.mapper.BrandMapper!--定义了brandResultMap对应的是brand类型--resultMap idbrandResultMap typebrand!--1.id标签完成主键字段的映射result标签完成一般字段的映射2.column为数据库表字段property为实体类属性--result columnbrand_name propertybrandName/result columncompany_name propertycompanyName//resultMap!--此处改为使用resultMap而不是resultType--select idselectAll resultMapbrandResultMapselect *from tb_brand;/select
/mapper注意上面只需要定义 字段名 和 属性名 不一样的映射而一样的则不需要专门定义出来。 修改后的运行结果如下 5.2.2 查询单条数据详情
有些数据的属性比较多在页面表格中无法全部实现而只会显示部分而其他属性数据的查询可以通过 查看详情 来进行查询如下图所示 1.编写接口方法
package com.itheima.mapper;import com.itheima.pojo.Brand;import java.util.List;public interface BrandMapper {/*** 查看详情根据Id查询*/Brand selectById(int id);}
2.编写SQL语句
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd!--namespace: 名称空间--
mapper namespacecom.itheima.mapper.BrandMapper!--字段与属性映射--resultMap idbrandResultMap typebrandresult columnbrand_name propertybrandName/result columncompany_name propertycompanyName//resultMapselect idselectById resultMapbrandResultMapselect * from tb_brand where id #{id};/select
/mapper
3.编写测试方法
package com.itheima.test;import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
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.IOException;
import java.io.InputStream;
import java.util.List;public class MybatisTest {Testpublic void testSelectById() throws IOException {// 1.获取SqlSessionFactoryString 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.执行方法int id 1; // 接收参数该id以后需要传递过来Brand brand brandMapper.selectById(id);System.out.println(brand);// 5.释放资源sqlSession.close();}
}
运行结果如下 1.参数占位符可以看到以上SQL语句中使用了 #{id}也就是参数占位符。
mybatis提供了两种参数占位符
#{}执行SQL时会将 #{} 占位符替换为 ?底层使用的是 PreparedStatement可以防止SQL注入。${}直接拼接SQL底层使用的是 Statement会存在SQL注入问题。 注对比得出开发都是使用 #{} 参数占位符 2.parameterType的使用对于有参数的mapper接口方法我们在映射配置文件中应该配置 ParameterType 来指定参数类型只不过该属性都可以省略。
select idselectById parameterTypeint resultMapbrandResultMapselect * from tb_brand where id #{id};
/select3.SQL语句中的特殊字符处理
以后肯定会在SQL语句中写一下特殊字符比如某一个字段大于某个值但是 等字符在xml中有特殊含义所以此时我们需要将这些符号进行转义可以使用以下两种方式进行转义
使用xml的转义字符例如lt; 就是 的转义字符。使用CDATA格式为 ![CDATA[内容]]例如 ![CDATA[ ]] 就是 的转义字符。
select idselectById resultMapbrandResultMapselect * from tb_brandwhere id ![CDATA[ ]] #{id};
/select注如果使用了大于或者小于某个值那么返回的就是 List 集合此时要修改mapper接口的返回对象为ListBrand 5.2.3 条件查询
5.2.3.1 固定多条件查询 我们经常会遇到如上图所示的多条件查询将多条件查询的结果展示在下方的数据列表中。而我们做这个功能需要分析最终的SQL语句应该是什么样思考两个问题
条件表达式如何连接
条件字段 企业名称 和 品牌名称 需要进行模糊查询所以条件应该是 简单的分析后我们来看功能实现的步骤
编写接口方法 参数所有查询条件结果ListBrand 在映射配置文件中编写SQL语句编写测试方法并执行
1.编写接口方法该功能有三个参数我们就需要考虑定义接口时参数应该如何定义。
Mybatis针对多参数有多种实现
散装参数需要使用 Param(SQL中的参数占位符名称)实体类封装参数只需要保证SQL中的参数名和实体类属性名对应上即可设置成功map集合只需要保证SQL中的参数名和map集合的键名对应上即可设置成功
1.1 散装参数使用 Param(参数名称) 标记每一个参数在映射配置文件中就需要使用 #{参数名称} 进行占位
package com.itheima.mapper;import com.itheima.pojo.Brand;
import org.apache.ibatis.annotations.Param;import java.util.List;public interface BrandMapper {/*** 多条件查询*/ListBrand selectByCondition(Param(status) int status, Param(companyName) String companyName, Param(brandName) String brandName);}
1.2 实体类封装参数将多个参数封装成一个 实体对象 将该实体对象作为接口的方法参数。该方式要求在映射配置文件的SQL中使用 #{内容} 时里面的内容必须和实体类属性名保持一致。
package com.itheima.mapper;import com.itheima.pojo.Brand;
import org.apache.ibatis.annotations.Param;import java.util.List;public interface BrandMapper {/*** 多条件查询*/ListBrand selectByCondition(Brand brand);
}
1.3 map集合将多个参数封装到map集合中将map集合作为接口的方法参数。该方式要求在映射配置文件的SQL中使用 #{内容} 时里面的内容必须和map集合中键的名称一致。
package com.itheima.mapper;import com.itheima.pojo.Brand;
import org.apache.ibatis.annotations.Param;import java.util.List;
import java.util.Map;public interface BrandMapper {/*** 多条件查询*/ListBrand selectByCondition(Map map);
}
2.编写SQL语句
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd!--namespace: 名称空间--
mapper namespacecom.itheima.mapper.BrandMapper!--字段与属性映射--resultMap idbrandResultMap typebrandresult columnbrand_name propertybrandName/result columncompany_name propertycompanyName//resultMapselect idselectByCondition resultMapbrandResultMapselect * from tb_brandwhere status #{status}and company_name like #{companyName}and brand_name like #{brandName}/select
/mapper
3.编写测试方法
3.1 对应 1.1 中使用 Param(参数名称) 标记每一个参数的方式
package com.itheima.test;import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
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.IOException;
import java.io.InputStream;
import java.util.List;public class MybatisTest {Testpublic void testSelectByCondition() throws IOException {// 1.获取SqlSessionFactoryString 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.执行方法// 接收参数int status 1;String companyName 华为;String brandName 华为;// 处理参数companyName % companyName %;brandName % brandName %;// 接口方法参数使用 Param 方式调用的方法ListBrand brands brandMapper.selectByCondition(status, companyName, brandName);System.out.println(brands);// 5.释放资源sqlSession.close();}
}
3.2 对应 1.2 中的将多个参数封装成一个 实体对象的方式
package com.itheima.test;import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
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.IOException;
import java.io.InputStream;
import java.util.List;public class MybatisTest {Testpublic void testSelectByCondition() throws IOException {// 1.获取SqlSessionFactoryString 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.执行方法// 接收参数int status 1;String companyName 华为;String brandName 华为;// 处理参数companyName % companyName %;brandName % brandName %;// 接口方法参数是 实体类对象 方式调用的方法Brand brand new Brand();brand.setStatus(status);brand.setCompanyName(companyName);brand.setBrandName(brandName);ListBrand brands brandMapper.selectByCondition(brand);System.out.println(brands);// 5.释放资源sqlSession.close();}
}
3.3 对应 1.3 中的将多个参数封装到map集合的方式
package com.itheima.test;import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
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.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class MybatisTest {Testpublic void testSelectByCondition() throws IOException {// 1.获取SqlSessionFactoryString 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.执行方法// 接收参数int status 1;String companyName 华为;String brandName 华为;// 处理参数companyName % companyName %;brandName % brandName %;// 接口方法参数是 map集合对象 方式调用的方法Map map new HashMap();map.put(status , status);map.put(companyName, companyName);map.put(brandName , brandName);ListBrand brands brandMapper.selectByCondition(map);System.out.println(brands);// 5.释放资源sqlSession.close();}
}
运行结果如下 5.2.3.2 动态多条件查询
上述使用固定多条件查询的功能实现存在很大的问题用户在输入条件时不一定会所有的条件都填写这个时候我们的SQL语句就不能像上面那样写。
例如用户只输入 当前状态 时SQL语句就是
select * from tb_brand where status #{status}而用户如果只输入 企业名称 时SQL语句就是
select * from tb_brand where company_name like #{companName}而用户如果输入了 当前状态 和 企业名称 时SQL语句又不一样
select * from tb_brand where status #{status} and company_name like #{companName}针对上述的需要Mybatis对动态SQL有很强大的支撑
ifchoose (when, otherwise)trim (where, set)foreach
1.修改SQL语句
if 标签的作用条件判断 test 属性逻辑表达式 where 标签的作用 替换where关键字会动态的去掉第一个条件前的 and如果所有的参数没有值则不加where关键字
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd!--namespace: 名称空间--
mapper namespacecom.itheima.mapper.BrandMapper!--字段与属性映射--resultMap idbrandResultMap typebrandresult columnbrand_name propertybrandName/result columncompany_name propertycompanyName//resultMapselect idselectByCondition resultMapbrandResultMapselect * from tb_brandwhereif teststatus ! nulland status #{status}/ifif testcompanyName ! null and companyName ! and company_name like #{companyName}/ifif testbrandName ! null and brandName ! and brand_name like #{brandName}/if/where/select
/mapper 注意需要给每个条件前都加上 and 关键字。 2.修改测试方法构造测试场景运行观察是否发生报错
用户只输入了 当前状态用户如果只输入了 企业名称用户输入了 当前状态 和 企业名称 用户什么都没输入 此处以5.2.3.1小节使用map集合封装参数的方式为例 // 用户只输入了当前状态
Map map new HashMap();
map.put(status , status);
ListBrand brands brandMapper.selectByCondition(map);
System.out.println(brands);// 用户只输入了企业名称
Map map new HashMap();
map.put(companyName, companyName);
ListBrand brands brandMapper.selectByCondition(map);
System.out.println(brands);// 用户只输入了当前状态和企业名称
Map map new HashMap();
map.put(status , status);
map.put(companyName, companyName);
ListBrand brands brandMapper.selectByCondition(map);
System.out.println(brands);// 用户什么都没输入
Map map new HashMap();
ListBrand brands brandMapper.selectByCondition(map);
System.out.println(brands);5.2.3.3 动态单条件查询
如下图所示在查询时只能选择 品牌名称、当前状态、企业名称 这三个条件中的一个但是用户到底选择哪儿一个我们并不能确定。这种就属于单个条件的动态SQL语句这种需求需要使用到 choosewhenotherwise标签 实现 而 choose 标签类似于Java 中的switch语句。 1.编写接口方法
package com.itheima.mapper;import com.itheima.pojo.Brand;
import org.apache.ibatis.annotations.Param;import java.util.List;
import java.util.Map;public interface BrandMapper {/*** 单条件动态查询* param brand* return*/ListBrand selectBySingleCondition(Brand brand);
}
2.编写SQL语句
choosewhenotherwise标签 可以类比 Java 中的switch语句
choose 相当于 switchwhen 相当于 caseotherwise 相当于 default
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd!--namespace: 名称空间--
mapper namespacecom.itheima.mapper.BrandMapper!--字段与属性映射--resultMap idbrandResultMap typebrandresult columnbrand_name propertybrandName/result columncompany_name propertycompanyName//resultMapselect idselectBySingleCondition resultMapbrandResultMapselect * from tb_brandwherechoose !--相当于switch--when teststatus ! null !--相当于case--status #{status}/whenwhen testcompanyName ! null and companyName ! !--相当于case--company_name like #{companyName}/whenwhen testbrandName ! null and brandName ! !--相当于case--brand_name like #{brandName}/when/choose/where/select
/mapper
3.编写测试方法
package com.itheima.test;import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
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.IOException;
import java.io.InputStream;
import java.util.List;public class MybatisTest {Testpublic void testSelectBySingleCondition() throws IOException {// 1.获取SqlSessionFactoryString 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.执行方法// 接收参数int status 1;String companyName 华为;String brandName 华为;// 处理参数companyName % companyName %;brandName % brandName %;// 接口方法参数是 实体类对象 方式调用的方法Brand brand new Brand();brand.setCompanyName(companyName);ListBrand brands brandMapper.selectBySingleCondition(brand);System.out.println(brands);// 5.释放资源sqlSession.close();}
}
运行结果如下 5.3 添加数据
如下图是我们平时在添加数据时展示的页面而我们在该页面输入想要的数据后添加 提交 按钮就会将这些数据添加到数据库中。接下来我们就来实现添加数据的操作。 1.编写接口方法 参数除了id之外的所有的数据id对应的是表中主键值而主键我们是 自动增长 生成的。 package com.itheima.mapper;import com.itheima.pojo.Brand;
import org.apache.ibatis.annotations.Param;import java.util.List;
import java.util.Map;public interface BrandMapper {/*** 添加*/void add(Brand brand);
}
2.编写SQL语句
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd!--namespace: 名称空间--
mapper namespacecom.itheima.mapper.BrandMapperinsert idaddinsert into tb_brand (brand_name, company_name, ordered, description, status)values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});/insert
/mapper
3.编写测试方法
package com.itheima.test;import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
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.IOException;
import java.io.InputStream;
import java.util.List;public class MybatisTest {Testpublic void testAdd() throws IOException {// 1.获取SqlSessionFactoryString 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.执行方法// 接收参数int status 1;String companyName 波导手机;String brandName 波导;String description 手机中的战斗机;int ordered 100;// 封装对象Brand brand new Brand();brand.setStatus(status);brand.setCompanyName(companyName);brand.setBrandName(brandName);brand.setDescription(description);brand.setOrdered(ordered);// 执行方法brandMapper.add(brand);// 提交事务sqlSession.commit();// 5.释放资源sqlSession.close();}
}
执行完上述代码后查询 tb_brand表发现数据已更新 在数据添加成功后有时候需要获取插入数据库数据的主键主键是自增长。
例如添加订单和订单项如下图就是京东上的订单 订单数据存储在订单表中订单项存储在订单项表中。
添加订单数据 添加订单项数据订单项中需要设置所属订单的id 明白了什么时候 主键返回 。接下来我们简单模拟一下在添加完数据后打印id属性值能打印出来说明已经获取到了。
我们将上面添加品牌数据的案例中映射配置文件里 statement 进行修改如下
useGeneratedKeys属性是否获取自动增长的主键值true表示获取keyProperty 属性指定将获取到的主键值封装到哪儿个属性里
insert idadd useGeneratedKeystrue keyPropertyidinsert into tb_brand (brand_name, company_name, ordered, description, status)values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});
/insert修改执行方法打印返回的主键 id
// 执行方法
brandMapper.add(brand);
Integer id brand.getId();
System.out.println(id);5.4 修改数据
如下图所示是修改页面用户在该页面书写需要修改的数据点击 提交 按钮就会将数据库中对应的数据进行修改。需要注意的是如果哪儿个输入框没有输入内容我们是将表中数据对应字段值替换为空白还是保留字段之前的值答案肯定是保留之前的数据。 1.编写接口方法
package com.itheima.mapper;import com.itheima.pojo.Brand;public interface BrandMapper {/*** 修改*/int update(Brand brand);
}
2.编写SQL语句
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd!--namespace: 名称空间--
mapper namespacecom.itheima.mapper.BrandMapperupdate idupdateupdate tb_brandsetif testbrandName ! null and brandName ! brand_name #{brandName},/ifif testcompanyName ! null and companyName ! company_name #{companyName},/ifif testordered ! nullordered #{ordered},/ifif testdescription ! null and description ! description #{description},/ifif teststatus ! nullstatus #{status}/if/setwhere id #{id};/update
/mapper
3.编写测试方法
package com.itheima.test;import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
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.IOException;
import java.io.InputStream;
import java.util.List;public class MybatisTest {Testpublic void testUpdate() throws IOException {// 1.获取SqlSessionFactoryString 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.执行方法// 接收参数int id 5;int status 0;String companyName 波导手机;String brandName 波导;String description 波导手机,手机中的战斗机;int ordered 200;// 封装对象Brand brand new Brand();brand.setId(id);brand.setStatus(status);brand.setOrdered(ordered);// 执行方法int count brandMapper.update(brand);System.out.println(count);// 提交事务sqlSession.commit();// 5.释放资源sqlSession.close();}
}
运行结果如下
执行代码前 执行代码后 5.5 删除数据
5.5.1 删除单条数据
如下图所示每行数据后面都有一个 删除 按钮当用户点击了该按钮就会将改行数据删除掉。那我们就需要思考这种删除是根据什么进行删除呢是通过主键id删除因为id是表中数据的唯一标识。 1.编写接口方法
package com.itheima.mapper;import com.itheima.pojo.Brand;public interface BrandMapper {/*** 根据id删除*/void deleteById(int id);
}
2.编写SQL语句
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd!--namespace: 名称空间--
mapper namespacecom.itheima.mapper.BrandMapperdelete iddeleteByIddelete from tb_brand where id #{id};/delete
/mapper
3.编写测试方法
package com.itheima.test;import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
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.IOException;
import java.io.InputStream;
import java.util.List;public class MybatisTest {Testpublic void testDeleteById() throws IOException {// 1.获取SqlSessionFactoryString 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.执行方法// 接收参数int id 5;// 执行方法brandMapper.deleteById(id);// 提交事务sqlSession.commit();// 5.释放资源sqlSession.close();}
}
运行结果如下
执行代码前 执行代码后 5.5.2 批量删除数据
如下图所示用户可以选择多条数据然后点击上面的 删除 按钮就会删除数据库中对应的多行数据。 1.编写接口方法
package com.itheima.mapper;import com.itheima.pojo.Brand;public interface BrandMapper {/*** 批量删除*/void deleteByIds(int[] ids);
} 参数是一个数组数组中存储的是多条数据的 id 2.编写SQL语句编写SQL时需要遍历数组来拼接SQL语句Mybatis 提供了 foreach 标签供我们使用。
foreach 标签用来迭代任何可迭代的对象如数组、集合等。
collection 属性 mybatis会将数组参数封装为一个Map集合。 默认array 数组可以使用Param注解改变map集合的默认key的名称 item 属性本次迭代获取到的元素。separator 属性集合项迭代之间的分隔符。foreach 标签不会错误地添加多余的分隔符也就是最后一次迭代不会加分隔符。open 属性该属性值是在拼接SQL语句之前拼接的语句只会拼接一次close 属性该属性值是在拼接SQL语句拼接后拼接的语句只会拼接一次
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd!--namespace: 名称空间--
mapper namespacecom.itheima.mapper.BrandMapperdelete iddeleteByIdsdelete from tb_brand where idinforeach collectionarray itemid separator, open( close)#{id}/foreach;/delete
/mapper
假如数组中的id数据是{1,2,3}那么拼接后的sql语句就是
delete from tb_brand where id in (1,2,3);可以使用Param注解改变map集合的默认key的名称例如修改默认key为 ids
foreach collectionids itemid separator, open( close)#{id}
/foreachvoid deleteByIds(Param(ids) int[] ids);3.编写测试方法
package com.itheima.test;import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
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.IOException;
import java.io.InputStream;
import java.util.List;public class MybatisTest {Testpublic void testDeleteByIds() throws IOException {// 1.获取SqlSessionFactoryString 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.执行方法// 接收参数int[] ids {1, 2, 3};// 执行方法brandMapper.deleteByIds(ids);// 提交事务sqlSession.commit();// 5.释放资源sqlSession.close();}
}
运行结果如下
执行代码前 执行代码后 6. Mybatis参数传递
Mybatis 接口方法中可以接收各种各样的参数如下
多个参数单个参数单个参数又可以是如下类型 POJO 类型Map 集合类型Collection 集合类型List 集合类型Array 类型其他类型
6.1 多个参数
如下面的代码就是接收两个参数而接收多个参数需要使用 Param 注解那么为什么要加该注解呢这个问题要弄明白就必须来研究Mybatis 底层对于这些参数是如何处理的。
User select(Param(username) String username,Param(password) String password);select idselect resultTypeuserselect * from tb_userwhere username#{username}and password#{password}
/select我们在接口方法中定义多个参数Mybatis 会将这些参数封装成 Map 集合对象值就是参数值而键在没有使用 Param 注解时有以下命名规则 以 arg 开头 第一个参数就叫 arg0第二个参数就叫 arg1以此类推。如 map.put(arg0参数值1);map.put(arg1参数值2); 以 param 开头 第一个参数就叫 param1第二个参数就叫 param2依次类推。如 map.put(param1参数值1);map.put(param2参数值2);
代码验证
1.编写接口方法在 UserMapper 接口中定义如下方法
package com.itheima.mapper;import com.itheima.pojo.User;import java.util.List;public interface UserMapper {User select(String username,String password);
}
2.编写SQL语句在 UserMapper.xml 映射配置文件中定义SQL可以使用 arg 或者 param
使用 arg 开头
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd!--namespace: 名称空间--
mapper namespacecom.itheima.mapper.UserMapperselect idselect resultTypeuserselect * from tb_userwhere username#{arg0}and password#{arg1}/select
/mapper
使用 param 开头
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd!--namespace: 名称空间--
mapper namespacecom.itheima.mapper.UserMapperselect idselect resultTypeuserselect * from tb_userwhere username#{param1}and password#{param2}/select
/mapper
3.编写测试方法在MybatisTest类中编写如下代码
package com.itheima.test;import com.itheima.mapper.UserMapper;
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.IOException;
import java.io.InputStream;
import java.util.List;public class MybatisTest {Testpublic void test() throws IOException {// 1.获取SqlSessionFactoryString resource mybatis-config.xml;InputStream inputStream Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);// 2.获取SqlSession对象SqlSession sqlSession sqlSessionFactory.openSession();// 3.获取Mapper接口的代理对象UserMapper userMapper sqlSession.getMapper(UserMapper.class);// 4.执行方法// 接收参数String username 张三;String password 123;User user userMapper.select(username, password);System.out.println(user);// 5.释放资源sqlSession.close();}
}
运行结果如下 在映射配合文件的SQL语句中使用用 arg 开头的和 param 书写代码的可读性会变的特别差此时可以使用 Param 注解。在接口方法参数上使用 Param 注解Mybatis 会将 arg 开头的键名替换为对应注解的属性值。
代码验证
1.在 UserMapper 接口中定义如下方法在 username 参数前加上 Param 注解
Mybatis 在封装 Map 集合时键名就会变成如下
map.put(username参数值1);map.put(arg1参数值2);map.put(param1参数值1);map.put(param2参数值2);
User select(Param(username) String username, String password);2.在 UserMapper.xml 映射配置文件中定义SQL
正确的SQL语句
select idselect resultTypeuserselect * from tb_userwhere username#{username}and password#{arg1}
/select错误的SQL语句
select idselect resultTypeuserselect * from tb_userwhere username#{arg0}and password#{arg1}
/select运行报错如下显示没找到arg0参数有效的参数为 [arg1, param1, username, param2] 结论以后接口参数是多个时在每个参数上都使用 Param 注解。这样代码的可读性更高 6.2 单个参数
单个参数的类型 POJO 类型直接使用要求 属性名 和 参数占位符名称 一致 Map 集合类型直接使用要求 map集合的键名 和 参数占位符名称 一致 Collection 集合类型Mybatis 会将集合封装到 map 集合中如下 map.put(arg0collection集合);map.put(collectioncollection集合); List 集合类型Mybatis 会将集合封装到 map 集合中如下 map.put(arg0list集合);map.put(collectionlist集合);map.put(listlist集合); Array 类型Mybatis 会将集合封装到 map 集合中如下 map.put(arg0数组);map.put(array数组); 其他类型 比如int类型参数占位符名称 叫什么都可以尽量做到见名知意。 注以上都可以使用 Param 注解替换map集合中默认的 arg 键名 7. 注解实现CRUD
使用注解开发会比配置文件开发更加方便但是注解只能完成简单功能而配置文件可以完成复杂功能
Mybatis 针对 CURD 操作都提供了对应的注解已经做到见名知意。如下
查询 Select添加 Insert修改 Update删除 Delete 注解是用来替换映射配置文件方式配置的所以使用了注解就不需要再映射配置文件中书写对应的 statement 1.查询数据
编写接口方法
package com.itheima.mapper;import com.itheima.pojo.User;
import org.apache.ibatis.annotations.Select;import java.util.List;public interface UserMapper {Select(select * from tb_user where id #{id})User selectById(int id);
}
编写测试方法
package com.itheima.test;import com.itheima.mapper.UserMapper;
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.IOException;
import java.io.InputStream;
import java.util.List;public class MybatisTest {Testpublic void test() throws IOException {// 1.获取SqlSessionFactoryString resource mybatis-config.xml;InputStream inputStream Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);// 2.获取SqlSession对象SqlSession sqlSession sqlSessionFactory.openSession();// 3.获取Mapper接口的代理对象UserMapper userMapper sqlSession.getMapper(UserMapper.class);// 4.执行方法// 接收参数int id 1;User user userMapper.selectById(id);System.out.println(user);// 5.释放资源sqlSession.close();}
}
2.添加数据
编写接口方法
package com.itheima.mapper;import com.itheima.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;import java.util.List;public interface UserMapper {Insert(insert into tb_user (username, password, gender, addr) values (#{username}, #{password}, #{gender}, #{addr}))int add(User user);
}
编写测试方法
package com.itheima.test;import com.itheima.mapper.UserMapper;
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.IOException;
import java.io.InputStream;
import java.util.List;public class MybatisTest {Testpublic void test() throws IOException {// 1.获取SqlSessionFactoryString resource mybatis-config.xml;InputStream inputStream Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);// 2.获取SqlSession对象SqlSession sqlSession sqlSessionFactory.openSession();// 3.获取Mapper接口的代理对象UserMapper userMapper sqlSession.getMapper(UserMapper.class);// 4.执行方法// 接收参数String username 诸葛亮;String password 234;String gender 男;String addr 成都;// 封装对象User user new User();user.setUsername(username);user.setPassword(password);user.setGender(gender);user.setAddr(addr);// 执行方法int count userMapper.add(user);System.out.println(count);// 提交事务sqlSession.commit();// 5.释放资源sqlSession.close();}
}
3.修改数据
编写接口方法
package com.itheima.mapper;import com.itheima.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;import java.util.List;public interface UserMapper {Update(update tb_user set username #{username}, password #{password}, gender #{gender}, addr #{addr} where id #{id};)int update(User user);
}
编写测试方法
package com.itheima.test;import com.itheima.mapper.UserMapper;
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.IOException;
import java.io.InputStream;
import java.util.List;public class MybatisTest {Testpublic void test() throws IOException {// 1.获取SqlSessionFactoryString resource mybatis-config.xml;InputStream inputStream Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);// 2.获取SqlSession对象SqlSession sqlSession sqlSessionFactory.openSession();// 3.获取Mapper接口的代理对象UserMapper userMapper sqlSession.getMapper(UserMapper.class);// 4.执行方法// 接收参数int id 1;String username 诸葛亮;String password 234;String gender 男;String addr 成都;// 封装对象User user new User();user.setId(id);user.setUsername(username);user.setPassword(password);user.setGender(gender);user.setAddr(addr);// 执行方法int count userMapper.update(user);System.out.println(count);// 提交事务sqlSession.commit();// 5.释放资源sqlSession.close();}
}
4.删除数据
编写接口方法
package com.itheima.mapper;import com.itheima.pojo.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;import java.util.List;public interface UserMapper {Delete(delete from tb_user where id #{id};)void deleteById(int id);
}
编写测试方法
package com.itheima.test;import com.itheima.mapper.UserMapper;
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.IOException;
import java.io.InputStream;
import java.util.List;public class MybatisTest {Testpublic void test() throws IOException {// 1.获取SqlSessionFactoryString resource mybatis-config.xml;InputStream inputStream Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);// 2.获取SqlSession对象SqlSession sqlSession sqlSessionFactory.openSession();// 3.获取Mapper接口的代理对象UserMapper userMapper sqlSession.getMapper(UserMapper.class);// 4.执行方法// 接收参数int id 1;// 执行方法userMapper.deleteById(id);// 提交事务sqlSession.commit();// 5.释放资源sqlSession.close();}
}