mybatis快速入门

Mybatis核心配置

Mybatis中文官网 https://mybatis.net.cn/
在这里插入图片描述
在这里插入图片描述

MybatisX插件安装

在这里插入图片描述

数据库连接

在这里插入图片描述

结果映射

实体类属性名和数据库表列名不一致,不能自动封装数据

  1. 起别名:在SQL语句中,对不一样的列名起别名,别名和实体类属性名一样
    可以定义片段,提升复用性
    数据库中名字brand_name 定义的实体类的字段brandName
<sql id="brand_column">
id, brand_name as brandName, company_name as companyName, ordered, description,status
</sql>

<select id="selectAll" resultType="brand">
		select <include refid="brand_column" /> from tb_brand;
</select>
  1. resultMap: 定义完成不一致的属性名和列名的映射
			<!--
			id:唯一标识
			type:映射的类型,支持别名
			-->
<resultMap id="brandResultMap"type="brand">
			<!--
			Id:完成主键字段的映射
			column:表的列名
			property:实体类的属性名
			result:完成一般字段的映射
			column:表的列名
			property;实体类的属性名
			-->
<result column="brand_name" property="brandName" />
<result column="company_name" property="companyName" />
</resultMapp>

查询

<select></select>

查询所有数据

在这里插入图片描述

1、准备数据映射实体类

在这里插入图片描述

2、编写接口方法 Usermapper.java
package com.example.demo.mapper;

import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
@Mapper
public interface Usermapper {
    List<User> findAll();

}

3、编写SQL语句: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">
<mapper namespace="com.example.demo.mapper.Usermapper">

    <select id="findAll" resultType="com.example.demo.entity.User">
        SELECT * FROM user
    </select>
    
</mapper>
4、执行测试:

test目录下写单元测试用例
以下是大体逻辑:

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.I0Exception;
import java.io.InputStream;
public class MyBatisTest {
	@Test
	public void testSelectAll() throws IOException {
		//1,获取SqlSessionFactory
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactorysqlSessionFactory = 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.释放资源
		sql.Session.close();
	}
}

spring boot中的写法
在这里插入图片描述

查看单条数据

在这里插入图片描述
注意:
在这里插入图片描述

条件查询

@Param(“实体类字段”)类型 实体类字段
在这里插入图片描述

在这里插入图片描述
以下就是参数设置的参数三种实例:
在这里插入图片描述

动态条件查询

条件查询会出现第一二个参数没有的情况下,sql就会语法报错where后面跟了and或者or语法导致错误,这个时候就会用到动态条件查询进行判断

多条件动态查询

*解决方案:
1)使用恒等式让所有条件格式都一样
2)<where>标签替换where关键字
在这里插入图片描述
在where后面直接加 1=1的恒等式第一个条件也加and
在这里插入图片描述
也可以直接用mybatis官方提供的标签,mybatis会把where后面跟着的and/or取消掉
在这里插入图片描述

单条件动态查询

根据用户的输入的条件不同进行单条件不同进行查询
在这里插入图片描述

添加、修改功能

添加

<insert></insert>
没有关联的添加

在这里插入图片描述

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.I0Exception;
import java.io.InputStream;
public class MyBatisTest {
	@Test
	public void testSelectAll() throws IOException {
		//接收参数
		int status = 1;[
		String companyName =“波导手机”;
		String brandName =“波导";
		string description ="手机中的战斗机”;
		int ordered = 100;
		//封装对象
		Brand brand = newBrand();
		brand.setStatus(status);
		brand.setCompanyNane(companyName);
		brand.setBrandName(brandName);
		brand.setDescription(description);
		brand.setOrdered(ordered);
		
		
		//1.获取SqlSessionFactory
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		//2,获取SqlSession对象
		SqlSession sqlSession= sqlSessionFactory.openSession();
		//SqlSession sqySession = sqlSessionFactory.openSession(true);
		//3,获取Mapper接口的代理对象
		BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
		//4,执行方法
		brandMapper.add(brand);
		Integer id = brand.getId();
		System.out.println(id);
		//提交事务
		sqlSession.commit();
		//5.释放资源
		sqlSession.close();
	}
}

上面打印不出对象的Id,打印出的id为null

返回添加数据的主键
<insert useGeneratedKeys="true" keyProperty="id">

在这里插入图片描述

修改

<update> </update>
修改全部字段

在这里插入图片描述

修改动态字段

在这里插入图片描述

删除功能

删除一个

在这里插入图片描述

删除多个

在这里插入图片描述

参数传递

在这里插入图片描述
ParamNameResolver源码分析:
1、把所获取的的参数put到param的Map对象中
*多个参数:封装为Map集合
map.put(“arg8”,参数值1)
map.put(“param1”,参数值1)
map.put(“param2”,参数值2)
map.put(“agr1”,参数值2)

在这里插入图片描述
2、@Param注解会替换Map对象中默认的arg键名称
在这里插入图片描述

如果传入的是数组类型就会创建一个Map对象,键名称就是collection调用数组的方法
在这里插入图片描述
单个参数:

  • 1、POJO类型:直接使用,属性名和参数占位符名称一致

  • 2、Map集合:直接使用,键名和参数占位符名称一致

  • 3、Collection:封装为Map集合

     map.put("arg8",collection集合);
     map.put("collection",collection集合);
    
  • 4、List:封装为Map集合

     map.put("arg0",list集合);
     map.put("collection",list集合);
     map.put("list",list集合);
    
  • 5、Array: 封装为Map集合

     map.put("arg8",数组);
     map.put("array",数组);
    
  • 6、其他类型:直接使用

注解开发

在这里插入图片描述

  • 23
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值