首页
技术小册
AIGC
面试刷题
技术文章
MAGENTO
云计算
视频课程
源码下载
PDF书籍
「涨薪秘籍」
登录
注册
第一章:简介
第二章:环境搭建
第三章:if 判断
第四章:where 查询条件
第五章:trim 自定义字符串截取
第六章:choose 分支选择
第七章:set 更新
第八章:foreach 遍历
第九章:foreach 批量插入(Mysql)
第十章:foreach 批量插入(Oracle)
第十一章:内置参数
第十二章:bind 绑定
第十三章:抽取可重用的 SQL 片段
当前位置:
首页>>
技术小册>>
Mybatis合辑3-Mybatis动态SQL
小册名称:Mybatis合辑3-Mybatis动态SQL
- 导入相关 jar 包的 Maven 坐标: ``` <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> ``` - sql脚本 ``` DROP TABLE IF EXISTS `employee`; CREATE TABLE `employee` ( `id` int(11) NOT NULL AUTO_INCREMENT, `last_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `gender` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `email` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; ``` - db.propreties ``` jdbc.url=jdbc:mysql://192.168.134.100:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true jdbc.driverClass=com.mysql.cj.jdbc.Driver jdbc.username=root jdbc.password=123456 ``` - log4j.xml ``` <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"> <param name="Encoding" value="UTF-8" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n" /> </layout> </appender> <logger name="java.sql"> <level value="debug" /> </logger> <logger name="org.apache.ibatis"> <level value="info" /> </logger> <root> <level value="debug" /> <appender-ref ref="STDOUT" /> </root> </log4j:configuration> ``` - Employee.java ``` package com.sunxiaping.domain; import org.apache.ibatis.type.Alias; @Alias("emp") public class Employee { private Integer id; private String lastName; private String email; private String gender; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Override public String toString() { return "Employee{" + "id=" + id + ", lastName='" + lastName + '\'' + ", email='" + email + '\'' + ", gender='" + gender + '\'' + '}'; } } ``` - EmployeeMapper.java ``` package com.sunxiaping.mapper; public interface EmployeeMapper { } ``` - EmployeeMapper.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.sunxiaping.mapper.EmployeeMapper"> </mapper> ``` - 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> <properties resource="db.properties"></properties> <settings> <!-- 开启自动驼峰命名规则映射 --> <setting name="mapUnderscoreToCamelCase" value="true"/> <!-- 开启对jdbcType的NULL的支持 --> <setting name="jdbcTypeForNull" value="NULL"/> <!-- 开启延迟加载 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 开启按需加载 --> <setting name="aggressiveLazyLoading" value="false"/> </settings> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driverClass}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <package name="com.sunxiaping.mapper"/> </mappers> </configuration> ```
上一篇:
第一章:简介
下一篇:
第三章:if 判断
该分类下的相关小册推荐:
java源码学习笔记
深入理解Java虚拟机
Java必知必会-Maven初级
Java语言基础14-枚举和注解
Java语言基础6-面向对象高级
SpringBoot合辑-初级篇
Java语言基础7-Java中的异常
Java语言基础16-JDK8 新特性
Java并发编程实战
Java必知必会-Maven高级
Java语言基础1-基础知识
Java面试指南