位置: IT常识 - 正文

MyBatis快速上手与知识点总结(mybatis怎么用)

编辑:rootadmin
阅读提示: 本文默认已经预装预装maven 1、MyBatis概述 1.1 MyBatis概述 持久层框架,用于简化JDBC开发,是对JDBC的封装 持久层: 负责将数据保存到数据库的代码部分 Java EE三层架构:表现层、业务层、持久层 1.2 JDBC缺点 硬编码,不利于维护 注册驱动、获取连 ... 目录1、MyBatis概述

1.1 MyBatis概述1.2 JDBC缺点1.3 MyBatis优化2、MyBatis快速入门3、Mapper代理开发

3.1 Mapper代理开发概述3.2 使用Mapper代理要求3.3 案例代码实现4、核心配置文件

4.1 多环境配置4.2 类型别名5、配置文件实现CRUD

5.1 环境准备5.2 查询所有数据5.3 查询5.4 多条件查询5.6 添加数据与MyBatis事务5.7 修改5.8 删除数据5.9 MyBatis参数传递6、通过注解实现CRUD

推荐整理分享MyBatis快速上手与知识点总结(mybatis怎么用),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:mybatis操作,mybatis基本步骤,mybatis基本步骤,mybatis入门教程,mybatis的步骤,mybatis怎么用,mybatis入门教程,mybatis操作步骤,内容如对您有帮助,希望把文章链接给更多的朋友!

阅读提示:

本文默认已经预装预装maven

1、MyBatis概述1.1 MyBatis概述

持久层框架,用于简化JDBC开发,是对JDBC的封装

持久层:

负责将数据保存到数据库的代码部分Java EE三层架构:表现层、业务层、持久层1.2 JDBC缺点硬编码,不利于维护注册驱动、获取连接SQL语句操作繁琐手动设置参数手动封装结果集1.3 MyBatis优化硬编码 --> 配置文件繁琐惭怍 --> 框架封装自动完成2、MyBatis快速入门

需求:查询user表中的所有数据

SQL

create database mybatis;use mybatis;drop table if exists tb_user;create table tb_user(id int primary key auto_increment,username varchar(20),password varchar(20),gender char(1),addr varchar(30));INSERT INTO tb_user VALUES (1, 'zhangsan', '123', '男', '北京');INSERT INTO tb_user VALUES (2, '李四', '234', '女', '天津');INSERT INTO tb_user VALUES (3, '王五', '11', '男', '西安');

代码实现

创建模块,导入坐标

在pom.xml中配置文件中添加依赖的坐标

注意:需要在项目的resources目录下创建logback的配置文件

<dependencies> <!--mybatis 依赖--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.5</version> </dependency> <!--mysql 驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> <!--junit 单元测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> <!-- 添加slf4j日志api --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.20</version> </dependency> <!-- 添加logback-classic依赖 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <!-- 添加logback-core依赖 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.2.3</version> </dependency></dependencies>

编写MyBatis核心文件

核心文件用于替换信息,解决硬编码问题

在模块下的resources目录下创建mybatis的配置文件mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <environments default="development"> <environment id="development"> <!-- 采用JDBC的事务管理方式 --> <transactionManager type="JDBC"/> <!-- 数据库连接信息 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql:///mybatis?useSSL=false"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <!-- 加载SQL映射文件 --> <mappers> <mapper resource="UserMapper.xml"/> </mappers></configuration>

编写SQL映射文件

SQL映射文件用于统一管理SQL语句,解决硬编码问题

在模块的resources目录下创建映射配置文件UserMaooer.xml

<?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"><!-- namespace:命名空间--><mapper namespace="test"> <!-- statement --> <select id="selectAll" resultType="priv.dandelion.entity.User"> select * from tb_user; </select></mapper>

编码

实体类

package priv.dandelion.entity;public class User { private Integer id; private String username; private String password; private String gender; private String address; public User() { } public User(Integer id, String username, String password, String gender, String address) { this.id = id; this.username = username; this.password = password; this.gender = gender; this.address = address; } 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 getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", gender='" + gender + '\'' + ", address='" + address + '\'' + '}'; }}

测试类

public static void main(String[] args) throws IOException { // 加载mybatis的核心配置文件,获取SqlSessionFactory String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 获取Session对象,执行SQL语句 SqlSession sqlSession = sqlSessionFactory.openSession(); // 执行SQL,处理结果 List<User> users = sqlSession.selectList("test.selectAll"); System.out.println(users); // 释放资源 sqlSession.close();}3、Mapper代理开发3.1 Mapper代理开发概述

解决形如上述测试类中List<User> users = sqlSession.selectList("test.selectAll");的硬编码问题

解决原生方式中的硬编码简化后期的SQL执行3.2 使用Mapper代理要求

定义与SQL映射文件同名的Mapper接口,并且将Mapper接口和SQL映射文件放置在同一目录下

maven项目开发时要求code和resources分开,可在resources中创建相同包文件来是实现上述效果

设置SQL映射文件的namespace属性未Mapper接口的全限定名

在Mapper接口中定义方法,方法名就是SQL映射文件中SQL语句的id,并且参数类型和返回值类型一致

3.3 案例代码实现

修改SQL映射文件UserMapper.xml

同时还要修改其路径

<?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"><!-- namespace:命名空间--><mapper namespace="priv.dandelion.mapper.UserMapper"> <select id="selectAll" resultType="priv.dandelion.entity.User"> select * from tb_user; </select></mapper>

创建对应的Mapper接口UserMapper.interface

public interface UserMapper { List<User> selectAll();}

修改mybatis核心配置文件中加载SQL映射的<mapper></mapper>的路径

<mappers> <mapper resource="priv/dandelion/mapper/UserMapper.xml"/></mappers>

测试代码

public static void main(String[] args) throws IOException { // 加载mybatis的核心配置文件,获取SqlSessionFactory String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 获取Session对象,执行SQL语句 SqlSession sqlSession = sqlSessionFactory.openSession(); // 执行SQL,处理结果 // 获取UserMapper接口的代理对象 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> users = userMapper.selectAll(); System.out.println(users); // 释放资源 sqlSession.close();}

改进

如果Mapper接口名称和SQL映射文件名称相同,并在同一目录下,则可以使用包扫描的方式简化SQL映射文件的加载,简化mybatis核心配置文件

<mappers> <!--加载sql映射文件--> <!-- <mapper resource="priv/dandelion/mapper/UserMapper.xml"/>--> <!--Mapper代理方式--> <package name="priv.dandelion.mapper"/></mappers>4、核心配置文件4.1 多环境配置

在核心配置文件的 environments 标签中其实是可以配置多个 environment ,使用 id 给每段环境起名,在 environments 中使用 default='环境id' 来指定使用哪儿段配置。我们一般就配置一个 environment 即可

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <typeAliases> <package name="priv.dandelion.entity"/> </typeAliases> <environments default="development"> <environment id="development"> <!-- 采用JDBC的事务管理方式 --> <transactionManager type="JDBC"/> <!-- 数据库连接信息 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <!-- JDBC连接数据库,SSL,Unicode字符集,UTF-8编码 --> <property name="url" value="jdbc:mysql:///mybatis?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> <environment id="test"> <transactionManager type="JDBC"/> <!-- 数据库连接信息 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql:///mybatis?useSSL=false"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <!-- 加载SQL映射文件 --> <mappers> <!-- <mapper resource="priv/dandelion/mapper/UserMapper.xml"/>--> <package name="priv.dandelion.mapper"/> </mappers></configuration>4.2 类型别名

映射配置文件中的resultType属性需要配置数据封装的类型(类的全限定名),繁琐

Mybatis 提供了 类型别名(typeAliases) 可以简化这部分的书写

<configuration> <!-- name属性未实体类所在的包 --> <typeAliases> <package name="priv.dandelion.entity"/> </typeAliases></configuration><mapper namespace="priv.dandelion.mapper.UserMapper"> <!-- resultType的值不区分大小写 --> <select id="selectAll" resultType="user"> select * from tb_user; </select></mapper>5、配置文件实现CRUD5.1 环境准备

SQL

-- 删除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;

实体类

public class Brand { private Integer id; private String brand_name; private String company_name; private Integer ordered; private String description; private Integer status; public Brand() { } public Brand( Integer id, String brand_name, String company_name, Integer ordered, String description, Integer status ) { this.id = id; this.brand_name = brand_name; this.company_name = company_name; this.ordered = ordered; this.description = description; this.status = status; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getBrand_name() { return brand_name; } public void setBrand_name(String brand_name) { this.brand_name = brand_name; } public String getCompany_name() { return company_name; } public void setCompany_name(String company_name) { this.company_name = company_name; } 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 + ", brand_name='" + brand_name + '\'' + ", company_name='" + company_name + '\'' + ", ordered=" + ordered + ", description='" + description + '\'' + ", status=" + status + '}'; }}

安装插件MyBatisX

步骤

编写接口方法Mapper接口参数返回值在SQL映射文件中编写SQL语句MyBatisX插件自动补全编写SQL若数据库字段名和实体类字段名不同,则需要解决该问题(见 5.2 SQL映射文件)编写执行测试获取SqlSessionFactory获取sqlSession对象获取mapper接口的代理对象执行方法释放资源5.2 查询所有数据

本节要点:

测试类的编写方式解决数据库字段和实体类字段名不同的问题

编写接口方法

public interface BrandMapper { public List<Brand> selectAll();}

编写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"><!-- namespace:命名空间--><mapper namespace="priv.dandelion.mapper.BrandMapper"><!-- 起别名解决数据库和实体类字段名不同问题 <sql id="brand_column"> id, brand_name as brandName, company_name as companyName, ordered, description, status </sql> <select id="selectAll" resultType="priv.dandelion.entity.Brand"> select * from tb_brand; select <include refid="brand_column"/> from tb_brand; </select> --> <!-- resultMap解决数据库和实体类字段不同问题 --> <resultMap id="brandResultMap" type="brand"> <result column="brand_name" property="brandName" /> <result column="company_name" property="companyName" /> </resultMap> <!-- 不使用resultType, 使用resultMap --> <select id="selectAll" resultMap="brandResultMap"> select * from tb_brand; </select></mapper>

编写测试方法

@Testpublic void testSelectAll() throws IOException { // 获取SqlSessionFactory String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 获取sqlSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(); // 获取mapper接口的代理对象 BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class); // 执行方法 List<Brand> brands = brandMapper.selectAll(); System.out.println(brands); // 释放资源 sqlSession.close();}5.3 查询

本节要点:

MyBatis的SQL映射文件中,SQL语句如何接收对应参数

使用占位符进行参数传递

占位符名称和参数保持一致

占位符

#{占位符名}:会替换为?,防止SQL注入,一般用于替换字段值${占位符名}:存在SQL注入问题,一般用于执行动态SQL语句,如表名列名不固定的情况(见)

编写接口方法

void update(Brand brand);

SQL映射文件查询代码标签

<resultMap id="brandResultMap" type="brand"> <result column="brand_name" property="brandName" /> <result column="company_name" property="companyName" /></resultMap><select id="selectById" resultMap="brandResultMap"> select * from tb_brand where id = #{id};</select>

测试方法

@Test public void testSelectByCondition() throws IOException { // 接收参数 int status = 1; String companyName = "华为"; String brandName = "华为"; // 获取SqlSessionFactory String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 获取sqlSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(); // 获取mapper接口的代理对象 BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class); // 执行方法 companyName = "%" + companyName + "%"; brandName = "%" + brandName + "%"; List<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName); System.out.println(brands); // 释放资源 sqlSession.close(); }5.4 多条件查询

本节要点:

多条件查询:如果有多个参数,需要使用@Paran("SQL参数占位符名称")注解多条件的动态条件查询:对象属性名称要和参数占位符名称一致(详见5-2解决数据库字段和实体类字段名不同的问题)单条件的动态条件查询:保证key要和参数占位符名称一致

多条件查询

SQL映射文件

<!-- resultMap解决数据库和实体类字段不同问题 --><resultMap id="brandResultMap" type="brand"> <result column="brand_name" property="brandName"/> <result column="company_name" property="companyName"/></resultMap><!-- 条件查询 --><select id="selectByCondition" resultMap="brandResultMap"> select * from tb_brand where status = #{status} and company_name like #{companyName} and brand_name like #{brandName}</select>

散装参数

接口

// 散装参数List<Brand> selectByCondition( @Param("status")int status, @Param("companyName")String companyName, @Param("brandName")String brandName);

测试方法

@Test public void testSelectByCondition() throws IOException { // 接收参数 int status = 1; String companyName = "华为"; String brandName = "华为"; // 获取SqlSessionFactory String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 获取sqlSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(); // 获取mapper接口的代理对象 BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class); // 执行方法 companyName = "%" + companyName + "%"; brandName = "%" + brandName + "%"; List<Brand> brands = brandMapper.selectByCondition( status, companyName, brandName); System.out.println(brands); // 释放资源 sqlSession.close(); }

对象参数

接口

// 对象参数List<Brand> selectByCondition(Brand brand);

测试方法

// 细节不表,仅展示执行方法// 执行方法Brand brand = new Brand();brand.setStatus(status);brand.setCompanyName("%" + companyName + "%");brand.setBrandName("%" + brandName + "%");List<Brand> brands = brandMapper.selectByCondition(brand);System.out.println(brands);

map集合参数

MyBatis快速上手与知识点总结(mybatis怎么用)

接口

// 集合参数List<Brand> selectByCondition(Map map);

测试方法

// 细节不表,仅展示执行方法// 执行方法Map map = new HashMap();map.put("status", status);map.put("companyName", "%" + companyName + "%");map.put("brandName", "%" + brandName + "%");List<Brand> brands = brandMapper.selectByCondition(map);System.out.println(brands);

多条件动态条件查询

优化条件查询,如页面上表单存在多个条件选项,但实际填写表单仅使用部分条件筛选的情况

SQL映射文件

<!-- resultMap解决数据库和实体类字段不同问题 --><resultMap id="brandResultMap" type="brand"> <result column="brand_name" property="brandName"/> <result column="company_name" property="companyName"/></resultMap><!-- 动态条件查询 --><select id="selectByCondition" resultMap="brandResultMap"> select * from tb_brand <where> <if test="status != null"> status = #{status} </if> <if test="companyName != null"> and company_name like #{companyName} </if> <if test="brandName != null"> and brand_name like #{brandName} </if> </where></select>

注:

if标签的test属性中可以包含逻辑或等逻辑判断,使用and、or进行连接

若条件SQL中同时包含AND等连接符

对所有的条件前都加AND,并在WHERE后加任意真判断,即WHERE 1=1 AND ... AND ...加入<if></if>判断标签造轮子,自行决定添加AND的条件使用<where></where>标签替换原SQL中的WHERE关键字,MyBatis将自动进行语法修正,如示例所示

单条件的动态条件查询

优化条件查询:如表单中存在多个条件筛选,但仅有其中一个生效的情况

使用标签choose标签类似于Java中的switchwhen标签类似于Java中的caseotherwise标签类似于Java中的default

SQL映射文件

<!-- resultMap解决数据库和实体类字段不同问题 --><resultMap id="brandResultMap" type="brand"> <result column="brand_name" property="brandName"/> <result column="company_name" property="companyName"/></resultMap><!-- 单条件动态查询 --><select id="selectByConditionSingle" resultMap="brandResultMap"> select * from tb_brand <where> <choose> <when test="status != null"> status = #{status} </when> <when test="companyName != null and companyName != ''"> company_name like #{companyName} </when> <when test="brandName != null and brandName != ''"> brand_name like #{brandName} </when> </choose> </where></select>5.6 添加数据与MyBatis事务

添加

接口

// 添加void add(Brand brand);

SQL映射文件

<insert id="add"> insert into tb_brand (brand_name, company_name, ordered, description, status) values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});</insert>

测试方法

@Testpublic void testAdd() throws IOException { // 接收参数 int status = 1; String companyName = "aaa"; String brandName = "xxx"; String description = "这是一段介绍"; int ordered = 100; // 获取SqlSessionFactory String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 获取sqlSession对象 // SqlSession sqlSession = sqlSessionFactory.openSession(); SqlSession sqlSession = sqlSessionFactory.openSession(true); // 获取mapper接口的代理对象 BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class); // 执行方法 Brand brand = new Brand(); brand.setStatus(1); brand.setBrandName(brandName); brand.setCompanyName(companyName); brand.setDescription(description); brand.setOrdered(ordered); brandMapper.add(brand); System.out.println("添加成功"); // 提交事务 // sqlSession.commit(); // 释放资源 sqlSession.close();}

Mybatis事务

MyBatis默认手动事务,执行添加等操作时会自动回滚

MyBayis事务处理的方法

// 方法一:在获取sqlSession对象时设置参数,开启自动事务SqlSession sqlSession = sqlSessionFactory.openSession(true);sqlSession.close();// 方法二:手动提交事务SqlSession sqlSession = sqlSessionFactory.openSession();sqlSession.commit();sqlSession.close();

添加 - 主键返回

传入实体类对象进行数据添加,在数据添加完成后,会将id信息写回该实体类对象

SQL映射文件

<insert id="add" useGeneratedKeys="true" keyProperty="id"> insert into tb_brand (brand_name, company_name, ordered, description, status) values (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});</insert>

获取写回信息

Integer id = brand.getId();5.7 修改

修改整条数据

接口

// 修改,可使用int返回值,返回受影响行数void update(Brand brand);

SQL映射文件

<update id="update"> update tb_brand set brand_name = #{brandName}, set company_name = #{companyName}, set ordered = #{ordered}, set description = #{description}, set status = #{status} where id = #{id};</update>

修改部分字段

优化上述代码应对仅修改部分属性导致其他属性数据丢失问题

使用<set></set>标签替换set关键字列表,区别于<where></where>标签,注意语法

SQL映射文件

<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>

测试代码

@Testpublic void testUpdate() throws IOException { // 接收参数 int id = 5; int status = 0; String companyName = "AAA"; String brandName = "XXX"; int ordered = 300; // 获取SqlSessionFactory String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 获取sqlSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(true); // 获取mapper接口的代理对象 BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class); // 执行方法 Brand brand = new Brand(); brand.setId(id); brand.setStatus(status); brand.setBrandName(brandName); brand.setCompanyName(companyName); brand.setOrdered(ordered); brandMapper.update(brand); System.out.println("修改成功"); // 释放资源 sqlSession.close();}5.8 删除数据

删除一条数据

接口

// 删除一条数据void deleteById(int id);

SQL映射文件

<delete id="deleteById"> delete from tb_brand where id = #{id};</delete>

测试

@Testpublic void testDeleteById() throws IOException { // 接收参数 int id = 5; // 获取SqlSessionFactory String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 获取sqlSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(true); // 获取mapper接口的代理对象 BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class); // 执行方法 brandMapper.deleteById(id); System.out.println("删除成功"); // 释放资源 sqlSession.close();}

批量删除数据

用于解决删除时传入参数为数组的情况

使用<foreach></foreach>标签代替SQL语句中的id in (?, ?, ..., ?)collection属性为MyBatis封装后数组对应的key,封装后属性值应为array(见注释)

MyBatis默认会将数组参数封装为Map集合,其key为array,即 array = ids可在接口中对参数数组使用@Param注解,将封装后的key手动命名,则可在映射文件中使用separator属性为分隔符open和close属性分别为在<foreach></foreach>前后拼接字符,主要用于代码规范,示例中未展示

接口

// 删除多个数据void deleteByIds(@Param("ids")int[] ids);

SQL映射文件

<delete id="deleteByIds"> delete from tb_brand where id in ( <!-- <foreach collection="array" item="id"> --> <foreach collection="ids" item="id" separator=","> #{id} </foreach> );</delete>

测试

@Testpublic void testDeleteByIds() throws IOException { // 接收参数 int[] ids = {6, 7}; // 获取SqlSessionFactory String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 获取sqlSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(true); // 获取mapper接口的代理对象 BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class); // 执行方法 brandMapper.deleteByIds(ids); System.out.println("删除成功"); // 释放资源 sqlSession.close();}5.9 MyBatis参数传递

概述

多个参数

设有如下代码:

User select(@Param("username") String username,@Param("password") String password);

MyBatis会将散装的多个参数封装为Map集合

若不使用@Param注解,则会使用以下命名规则:

map.put("arg0",参数值1);map.put("arg1",参数值2);map.put("param1",参数值1);map.put("param2",参数值2);

即Map集合中的参数的key分别为arg0, arg1, param1, param2

使用@Param注解会将Map集合中的参数的arg替换为指定内容,增强其可读性

单个参数

POJO类型:直接使用,要求属性名和参数占位符名称一致(见 5.2 SQL映射文件)

Map集合类型:直接使用,要求key和参数占位符名称一致(见 5.2 SQL映射文件)

Collection集合类型:封装Map集合,可以使用@Param注解替换Map集合中默认arg键名

map.put("arg0",collection集合);map.put("collection",collection集合;

List集合类型:封装为Map集合,可以使用@Param注解,替换Map集合中默认的arg键名

map.put("arg0",list集合);map.put("collection",list集合);map.put("list",list集合);

Array类型:封装为Map集合,可以使用@Param注解,替换Map集合中默认的arg键名

map.put("arg0",数组);map.put("array",数组);

其他类型:直接使用,与参数占位符无关,但尽量见名知意

6、通过注解实现CRUD

概述

用于简化开发,可以对简单的查询使用注解进行操作,以替换xml中的statement对于复杂的查询,仍然建议使用xml配置文件,否则代码会十分混乱

使用方法

注解(部分)

查询 :@Select添加 :@Insert修改 :@Update删除 :@Delete

示例

使用注解简化查询

原接口

Brand selectById(int id);

原SQL映射文件

<!-- resultMap解决数据库和实体类字段不同问题 --><resultMap id="brandResultMap" type="brand"> <result column="brand_name" property="brandName"/> <result column="company_name" property="companyName"/></resultMap><select id="selectById" resultMap="brandResultMap"> select * from tb_brand where id = #{id};</select>

使用注解进行开发

接口

@ResultMap("brandResultMap")// 解决数据库和实体类字段名称不同@Select("select * from tb_brand where id = #{id}")// 查询语句Brand selectById(int id);

SQL映射文件:不再需要原先的statement

<!-- resultMap解决数据库和实体类字段不同问题 --><resultMap id="brandResultMap" type="brand"> <result column="brand_name" property="brandName"/> <result column="company_name" property="companyName"/></resultMap><!--<select id="selectById" resultMap="brandResultMap"> select * from tb_brand where id = #{id};</select>-->
本文链接地址:https://www.jiuchutong.com/zhishi/309285.html 转载请保留说明!

上一篇:Python学习:数据类型转换的方法教程(python数据编程)

下一篇:c语言中static如何修饰函数(c 语言 static)

  • 百度电影不能播放怎么回事(为什么百度影音播放不了)(为什么百度上的电影突然不能下载了)

    百度电影不能播放怎么回事(为什么百度影音播放不了)(为什么百度上的电影突然不能下载了)

  • ipad7代多少寸(ipad第7代是2019还是2020)

    ipad7代多少寸(ipad第7代是2019还是2020)

  • 钉钉重新提交作业老师会知道吗(钉钉重新提交作业老师知道吗)

    钉钉重新提交作业老师会知道吗(钉钉重新提交作业老师知道吗)

  • 抖音直播有哪些违禁词(抖音直播有哪些礼物)

    抖音直播有哪些违禁词(抖音直播有哪些礼物)

  • 只露手和桌面的视频怎么拍(只露手和桌面的直播怎么拍)

    只露手和桌面的视频怎么拍(只露手和桌面的直播怎么拍)

  • 微信视频通话内容能恢复吗

    微信视频通话内容能恢复吗

  • 朋友圈不显示三天可见是不是被删除了(朋友圈不显示三天可见只有一条线)

    朋友圈不显示三天可见是不是被删除了(朋友圈不显示三天可见只有一条线)

  • 屏蔽此人消息还能收到对方信息吗(屏蔽此人消息还能看见吗)

    屏蔽此人消息还能收到对方信息吗(屏蔽此人消息还能看见吗)

  • i12充电仓怎样才算充满(i12充电仓的按钮)

    i12充电仓怎样才算充满(i12充电仓的按钮)

  • 手机怎样压缩文件打包(手机怎样压缩文件到最小)

    手机怎样压缩文件打包(手机怎样压缩文件到最小)

  • 苹果6指纹解锁不灵敏怎么办(苹果6指纹解锁在哪里)

    苹果6指纹解锁不灵敏怎么办(苹果6指纹解锁在哪里)

  • 为什么微信实名认证身份信息不符合(为什么微信实名认证非要绑银行卡)

    为什么微信实名认证身份信息不符合(为什么微信实名认证非要绑银行卡)

  • 怎么删除本地主题(怎么删除本地主页的视频)

    怎么删除本地主题(怎么删除本地主页的视频)

  • 小程序店铺怎么退款(小程序店铺怎么注销)

    小程序店铺怎么退款(小程序店铺怎么注销)

  • 天猫积分有什么用(天猫积分有什么用兑换)

    天猫积分有什么用(天猫积分有什么用兑换)

  • 手机一卡一卡的怎么办(为什么手机用久了会卡)

    手机一卡一卡的怎么办(为什么手机用久了会卡)

  • wps删除空白页怎么删(wps删除空白页怎么删文档)

    wps删除空白页怎么删(wps删除空白页怎么删文档)

  • 移动卡显示hd怎么取消(移动卡显示hd收费吗)

    移动卡显示hd怎么取消(移动卡显示hd收费吗)

  • 电脑上怎么调整照片kb(电脑上怎么调整图片的像素和大小)

    电脑上怎么调整照片kb(电脑上怎么调整图片的像素和大小)

  • 快手b类违规怎么解封(快手a类违规处理措施)

    快手b类违规怎么解封(快手a类违规处理措施)

  • qq冻结qq账号怎么解冻    (qq冻结qq账号怎么解冻手机号换了)

    qq冻结qq账号怎么解冻    (qq冻结qq账号怎么解冻手机号换了)

  • 在快手买的东西怎么查物流(在快手买的东西被骗了,还能要回来钱吗)

    在快手买的东西怎么查物流(在快手买的东西被骗了,还能要回来钱吗)

  • vivox23有万能遥控吗(vivo有没有万能遥控器)

    vivox23有万能遥控吗(vivo有没有万能遥控器)

  • 小红书怎么看客服聊天记录(小红书怎么看客户电话)

    小红书怎么看客服聊天记录(小红书怎么看客户电话)

  • regsync.exe - regsync是什么进程 有什么用

    regsync.exe - regsync是什么进程 有什么用

  • drvddll.exe是病毒进程吗 drvddll进程安全吗(loaddriver.exe是什么病毒)

    drvddll.exe是病毒进程吗 drvddll进程安全吗(loaddriver.exe是什么病毒)

  • 分享24个网页游戏源代码,总有一个是你想要的(十大网页游戏)

    分享24个网页游戏源代码,总有一个是你想要的(十大网页游戏)

  • tzselect命令  查看设置时区(trace命令详解)

    tzselect命令 查看设置时区(trace命令详解)

  • 什么时候公司需要赔偿员工
  • 计提的增值税
  • 专项应付款怎核算?
  • 期末结转的内容
  • 非财政补助支出
  • 发票作废进项税额转出怎么做账
  • 年终奖有计算公式吗
  • 增值税专票销货清单模板
  • 职工旅游费计入什么科目
  • 退税后钱到哪里
  • 出售无形资产属于让渡资产使用权吗
  • 股票买卖属于投资活动吗
  • 挂靠的项目税务局怎么交地税?
  • 工会经费可以不提吗
  • 建筑行业增值税是多少
  • 反写状态已反写是什么意思
  • 视同销售产品与收款尾差怎么处理
  • 进项税额转出多转了怎么办
  • 怎么判断分红前已提取足够法定公积金?
  • 移动通信定额发票
  • 亏损企业能否享受失业金
  • 办公费差旅费属于
  • 发票跨期审计调查报告
  • 销售商品结转成本会计分录
  • 股票投资报酬率怎么算excel
  • 投资者与被投资者的名人
  • 电脑在线测速的软件
  • 离职违约金赔偿标准
  • 资产负债表中资产项目的排列顺序是
  • 发票被盗丢失如何处理
  • php实现保存网站内容
  • json去除某个字段
  • 非盈利组织稳定吗
  • linux命令行怎么用
  • 个体工商户减免个人所得税政策
  • php批量上传
  • 税款差0.05是怎么处理
  • 交易性金融资产公允价值变动计入
  • php curl 封装
  • php开发程序
  • 消费税购置税价格一样
  • 存货采购成本有余额吗
  • php中为什么图片显示不出来
  • python拆分文本文件
  • 劳务派遣公司的注册资本最低限额为
  • Vite + Vue2 + Vuetify2 + <script setup> + TypeScript 搭配开发项目
  • 人力资源公司代办
  • react高阶组件
  • 帝国cms调用api接口
  • 一个简单的后台与数据库交互的登录与注册[sql注入处理、以及MD5加密]
  • 企业净利率多少算高
  • 当月收入未开票怎么入账
  • 电商平台收入何时到账
  • 建筑企业增值税怎么算
  • 银行代付工资如何操作
  • 企业账户里的政府账户
  • 服装公司的会计怎么做账
  • 停产期间发生的事故
  • 公司购买银行理财产品怎么做账
  • 税收滞纳金计入营业外收入的哪里
  • win8 恢复
  • winxp和win7之间的系统
  • win10不能显示桌面
  • 手机苹果操作系统
  • win8桌面图标变大了怎么调小
  • 添加或删除程序在哪win10
  • win7关机没有强制关机选项
  • linux入门学习
  • android 数据库app
  • 安卓运行环境搭建
  • cmd常用命令详解
  • js实现计算器代码
  • python相乘
  • 编写js代码要注意什么
  • Python 'takes exactly 1 argument (2 given)' Python error
  • 拟录用人员公示后还会递补吗
  • 个人所得税申报怎么申报
  • 中国税务网络版
  • 铜进口量2019
  • 新加坡国税局的电话号码
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设