创建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. 解决win7设置默认程序打开方式失效

    问题描述 我在设置一个文件(.ui)的默认程序打开,总是失效.设置不成功. 原因 正常这个程序应该用 designer.exe 打开,但是我之前设置过(.ui)默认程序打开程序为designer.ex ...

  2. day23 约束 & 锁 & 范式

    考点: 乐观锁=>悲观锁=>锁 表与表的对应关系 一对一:学生与手机号,一个学生对一个手机号 一对多:班级与学生,一个班级对应多个学生 多对一: 多对多:学生与科目,一个学生对应多个科目, ...

  3. Zabbix与乐维监控对比分析(一)——架构、性能篇

    近年来,Zabbix凭借其近乎无所不能的监控及优越的性能一路高歌猛进,在开源监控领域独占鳌头:而作为后起的新锐IT监控平台--乐维监控,则不断吸收Zabbix,Prometheus等优秀开源平台的优点 ...

  4. 【每日一题】【奇偶分别中心扩展/动态规划】2022年2月5日-NC最长回文子串的长度

    描述对于长度为n的一个字符串A(仅包含数字,大小写英文字母),请设计一个高效算法,计算其中最长回文子串的长度. 方法1:奇数偶数分别从中心扩展 import java.util.*; public c ...

  5. Windows10下python3和python2同时安装(三)VS 2013配置python环境

    Windows10下python3和python2同时安装(三) VS 2013配置python环境 说明:本文基于python2和python3同时安装之后,对VS 2013进行配置,下面有些地方文 ...

  6. SQL注入问题/触发器trigger/事务/事物隔离

    SQL注入问题 本质:利用特殊符号的组合产生特殊的含义,从而避开正常的业务逻辑 select * from userinfo where name='jason' -- kasdjksajd' and ...

  7. 使用 SmartIDE 开发golang项目

    目录 概述 架构 开发视图 快速开始 安装 SmartIDE CLI 环境 启动 创建环境 安装工具 调试 基本调试 Start 命令调试 很荣幸在去年加入到 SmartIDE 产品组,从事开发工作, ...

  8. AStar寻路算法示例

    概述 AStar算法是一种图形搜索算法,常用于寻路.他是以广度优先搜索为基础,集Dijkstra算法和最佳优先(best fit)于一身的一种算法. 示例1:4向 示例2:8向 思路 递归的通过估值函 ...

  9. Prometheus及Grafana监控服务的安装使用

    说明 Prometheus 是一个开放性的监控解决方案,通过 Node Exporter 采集当前主机的系统资源使用情况,并通过 Grafana 创建一个简单的可视化仪表盘. docker 安装 pr ...

  10. [机器学习] Yellowbrick使用笔记8-模型选择可视化

    Yellowbrick可视化工具旨在指导模型选择过程.一般来说,模型选择是一个搜索问题,定义如下:给定N个由数值属性描述的实例和(可选)一个估计目标,找到一个由特征.算法和最适合数据的超参数组成的三元 ...