创建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 }

最新文章

  1. WinForm TreeView递归加载
  2. 可拖动的DIV
  3. 用Open Live Account写博文的第一篇文章,立个flag
  4. Android 支付宝以及微信支付快速接入流程
  5. JSON认识
  6. java数据类型定义与输出
  7. Ubuntu Qt arm-linux-androideabi-gcc: Command not found
  8. hdu 4582 树状DP
  9. asp.net core mvc权限控制:分配权限
  10. 闭锁——CountDownLatch
  11. Java: AutoCloseable接口
  12. 【Java数据结构学习笔记之一】线性表的存储结构及其代码实现
  13. Java 运动模糊
  14. python基础知识巩固(os.walk)
  15. jQuery中有关each方法的使用
  16. mysql 导入数据库问题
  17. jQuery: 判断from表单是否修改
  18. BZOJ1497 [NOI2006]最大获利 网络流 最小割 SAP
  19. angular学习笔记(3)- MVC
  20. IndexedDB 教程

热门文章

  1. Sql Server日期转汉字字符串
  2. Python-WebSpider
  3. 【Java进阶】五分钟快速掌握JVM优化概念、常用命令、工具、JUC、多线程、GC等知识
  4. 【离线数仓】Day04-即席查询(Ad Hoc):Presto链接不同数据源查询、Druid建多维表、Kylin使用cube快速查询
  5. form表单里的button 等元素不能使用margin: 0 auto;
  6. Rust学习之旅(读书笔记):枚举 (Enum)
  7. 1.5.5 HDFS读写解析-hadoop-最全最完整的保姆级的java大数据学习资料
  8. [深度学习] ubuntu18.04配置深度学习环境笔记
  9. tempdb日志文件暴增分析
  10. 为什么 java 容器推荐使用 ExitOnOutOfMemoryError 而非 HeapDumpOnOutOfMemoryError ?