创建user表,添加数据,Mysql:

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

创建模块,导入坐标 在创建好的模块中的 pom.xml 配置文件中添加依赖的坐标:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>mybatis-demo</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>

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

</project>

编写 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">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///mybatis?useSSL=false&amp;serverTimezone=UTC"/>
<property name="username" value="root"/>

<property name="password" value="1234"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 加载sql的映射文件 , -->
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>

编写 SQL 映射文件 --> 统一管理sql语句,解决硬编码问题 在模块的 resources 目录下创建映射配置文件 UserMapper.xml ,内容如下:

 1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE mapper
3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5 <!--
6 namespace:名称空间
7 -->
8
9 <mapper namespace="test">
10 <select id="selectAll" resultType="com.itheima.pojo.User">
11 select * from tb_user;
12 </select>
13 </mapper>

在 com.itheima.pojo 包下创建 User,在 com.itheima 包下编写 MybatisDemo 测试:

注意:mybatis-config.xml 文件 的 相关配置,如添加UTC和数据库账号密码,且pom.xml的数据库最好和已安装的数据库版本号相同,并且在idea左侧栏先连接上自己的数据库。

<property name="url" value="jdbc:mysql:///mybatis?useSSL=false&amp;serverTimezone=UTC"/>

<property name="username" value="root"/>
<property name="password" value="123456"/>

单个条件(动态SQL)

BrandMapper.xml: 其中where 标签 和 choose 标签 使得 动态搜索(查询条件可能不全)得以实现

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

MybatisTest.java :

 1     @Test
2 public void testSelectByConditionSingle() throws IOException {
3
4 int status = 1;
5 String companyName = "华为";
6 String brandName = "华为";
7
8 companyName = "%" + companyName + "%";
9 brandName = "%" + brandName + "%";
10
11 Brand brand = new Brand();
12 // brand.setStatus(status);
13 // brand.setCompanyName(companyName);
14 // brand.setBrandName(brandName);
15
16
17 // Map map = new HashMap();
18 //map.put("status",status);
19 // map.put("companyName",companyName);
20 // map.put("brandName",brandName);
21
22 String resource = "mybatis-config.xml";
23 InputStream inputStream = Resources.getResourceAsStream(resource);
24 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
25
26 SqlSession sqlSession = sqlSessionFactory.openSession();
27
28 BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
29
30 // List<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName);
31 // List<Brand> brands = brandMapper.selectByCondition(brand);
32
33
34 List<Brand> brands = brandMapper.selectByConditionSingle(brand);
35 System.out.println(brands);
36
37 sqlSession.close();
38
39
40 }

黑马Mybatis快速入门的更多相关文章

  1. MyBatis学习总结(一)——MyBatis快速入门

    一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...

  2. MyBatis快速入门

    一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...

  3. MyBatis学习总结(一)——MyBatis快速入门(转载)

    本文转载自http://www.cnblogs.com/jpf-java/p/6013537.html MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了 ...

  4. MyBatis入门学习教程-MyBatis快速入门

    一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...

  5. MyBatis学习总结(一)——MyBatis快速入门

    一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...

  6. 【转】MyBatis学习总结(一)——MyBatis快速入门

    [转]MyBatis学习总结(一)——MyBatis快速入门 一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC ...

  7. MyBatis学习总结-MyBatis快速入门的系列教程

    MyBatis学习总结-MyBatis快速入门的系列教程 [MyBatis]MyBatis 使用教程 [MyBatis]MyBatis XML配置 [MyBatis]MyBatis XML映射文件 [ ...

  8. MyBatis学习笔记(一)——MyBatis快速入门

    转自孤傲苍狼的博客:http://www.cnblogs.com/xdp-gacl/p/4261895.html 一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优 ...

  9. Java基础-SSM之mybatis快速入门篇

    Java基础-SSM之mybatis快速入门篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 其实你可能会问什么是SSM,简单的说就是spring mvc + Spring + m ...

  10. MyBatis学习总结(1)——MyBatis快速入门

    一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...

随机推荐

  1. DevExpress中GridControl控件焦点改变时触发事件

    FocusedRowObjectChanged 事件.可以在焦点改变一行的时候触发对应的事件. 做一个记录 大家如果有问题可以 Console.WriteLine("加群"+&qu ...

  2. win 10玩魔兽争霸/黑边,不能全屏,闪退

    1.win键+s键搜索注册表 打开 找到路径 计算机\HKEY_CURRENT_USER\SOFTWARE\Blizzard Entertainment\Warcraft III\Video 这里有两 ...

  3. 数电第五周周结_by_yc

    数电第五周周结_by_yc 基本要点: 组合逻辑电路的行为特点.经典组合逻辑电路的设计.PPA优化 组合逻辑电路设计要点:   ①敏感变量列表应包含所有会影响输出的控制量:   ②条件语句的完全描述, ...

  4. 社论 22.10.9 优化连续段dp

    CF840C 给定一个序列 \(a\),长度为 \(n\).试求有多少 \(1\) 到 \(n\) 的排列 \(p_i\),满足对于任意的 \(2\le i\le n\) 有 \(a_{p_{i-1} ...

  5. 【zookeeper】Zookeeper相关概念、重难点(myid)、语法、使用、工具

    1234567890 1请按照我是想额度插入fvtgb6yhn7ujm8ik,9ol.

  6. 下载Font Awesome框架

    目录 一:下载Font Awesome框架 二:如何使用font awesome 1.使用图标等样式,点击复制标签即可,需要嵌套在i标签内 2.点击图标,复制标签,然后粘贴使用即可. 3.动态图片等 ...

  7. 浅谈字节码增强技术系列2-Asm与Cglib

    作者:董子龙 前言 记得那是2022年秋天的第一场雨,比2021年来的稍晚一些,在那个秋雨朦胧的下午,正在工位上奋笔疾书的我突然听到了前面波哥对着手机听筒说出来的"温柔"的话语:说 ...

  8. django框架(部分讲解)

    ORM执行SQL语句 有时候ORM的操作效率可能偏低 我们是可以自己编写SQL的 方式1: raw()方法执行原生sql语句 models.User.objects.raw('select * fro ...

  9. 使用xshell连接linux虚拟机

    目录 1.涉及的软件 2.连接步骤 2.1.虚拟机网络连接设置 2.2.配置linux的ip地址 2.3.关闭linux的防火墙 2.4.启动ssh服务 2.5.使用xshell连接linux 1.涉 ...

  10. 【深入浅出 Yarn 架构与实现】4-4 RM 管理 Application

    在 YARN 中,Application 是指应用程序,它可能启动多个运行实例,每个运行实例由 -个 ApplicationMaster 与一组该 ApplicationMaster 启动的任务组成, ...