结果集映射:
resultMap解决数据库字段名和属性名不一致的问题
| id name pwd |
| id name password |
| column 是数据库的字段名 property 是实体类的属性名 |
| <?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.kuang.dao.UserMapper"> |
| |
| <resultMap id="UserMap" type="User"> |
| <result column="id" property="id"></result> |
| <result column="name" property="name"></result> |
| <result column="pwd" property="password"></result> |
| </resultMap> |
| <select id="getUserByID" resultMap="UserMap" parameterType="int"> |
| select * from user WHERE id = #{id} |
| </select> |
| </mapper> |
动态SQL
-
什么是动态SQL?
根据不同的条件生成不同的SQL语句
| 官网描述: |
| MyBatis 的强大特性之一便是它的动态 SQL。如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句的痛苦。例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL 这一特性可以彻底摆脱这种痛苦。 |
| 虽然在以前使用动态 SQL 并非一件易事,但正是 MyBatis 提供了可以被用在任意 SQL 映射语句中的强大的动态 SQL 语言得以改进这种情形。 |
| 动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似。在 MyBatis 之前的版本中,有很多元素需要花时间了解。MyBatis 3 大大精简了元素种类,现在只需学习原来一半的元素便可。MyBatis 采用功能强大的基于 OGNL 的表达式来淘汰其它大部分元素。 |
| ------------------------------- |
| - if |
| - choose (when, otherwise) |
| - trim (where, set) |
| - foreach |
| ------------------------------- |
MybatisUtils工具类
| package com.kuang.utils; |
| |
| import org.apache.ibatis.io.Resources; |
| import org.apache.ibatis.session.SqlSession; |
| import org.apache.ibatis.session.SqlSessionFactory; |
| import org.apache.ibatis.session.SqlSessionFactoryBuilder; |
| |
| import java.io.IOException; |
| import java.io.InputStream; |
| |
| public class MybatisUtils { |
| |
| private static SqlSessionFactory sqlSessionFactory; |
| static { |
| try { |
| String resource = "mybatis-config.xml"; |
| |
| |
| |
| InputStream inputStream = Resources.getResourceAsStream(resource); |
| sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| public static SqlSession getSession(){ |
| return sqlSessionFactory.openSession(); |
| } |
| } |
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.jdbc.Driver"/> |
| <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf8"/> |
| <property name="username" value="root"/> |
| <property name="password" value="000429"/> |
| </dataSource> |
| </environment> |
| </environments> |
| <mappers> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <package name="com.kuang.dao"/> |
| </mappers> |
| </configuration> |
测试类
| @Test |
| public void test(){ |
| |
| |
| |
| SqlSession sqlSession = MybatisUtils.getSession(); |
| UserMapper mapper = sqlSession.getMapper(UserMapper.class); |
| |
| User user=mapper.getUserByID(2); |
| System.out.println(user); |
| sqlSession.close(); |
| |
| |
| |
| |
| } |
目录结构

db.properties
| driver=com.mysql.jdbc.Driver |
| url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf8 |
| username=root |
| password=000429 |
在mybatis-config.xml中
| |
| <properties resource="db.properties"/> |
log4j.properties
| |
| log4j.rootLogger=DEBUG,console,file |
| |
| log4j.appender.console = org.apache.log4j.ConsoleAppender |
| log4j.appender.console.Target = System.out |
| log4j.appender.console.Threshold=DEBUG |
| log4j.appender.console.layout = org.apache.log4j.PatternLayout |
| log4j.appender.console.layout.ConversionPattern=[%c]-%m%n |
| |
| log4j.appender.file = org.apache.log4j.RollingFileAppender |
| log4j.appender.file.File=./log/kuang.log |
| log4j.appender.file.MaxFileSize=10mb |
| log4j.appender.file.Threshold=DEBUG |
| log4j.appender.file.layout=org.apache.log4j.PatternLayout |
| log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n |
| |
| log4j.logger.org.mybatis=DEBUG |
| log4j.logger.java.sql=DEBUG |
| log4j.logger.java.sql.Statement=DEBUG |
| log4j.logger.java.sql.ResultSet=DEBUG |
| log4j.logger.java.sql.PreparedStatement=DEBUG |
在mybatis-config.xml中
| |
| <settings> |
| <setting name="logImpl" value="LOG4J"/> |
| </settings> |
多对一
| <?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.StudentMapper"> |
| <select id="getStudent" resultMap="studentTeacher"> |
| select * from mybatis.student |
| </select> |
| <resultMap id="studentTeacher" type="student"> |
| <result property="id" column="id"></result> |
| <result property="name" column="name"></result> |
| |
| |
| |
| |
| <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/> |
| </resultMap> |
| <select id="getTeacher" resultType="Teacher"> |
| select * from teacher where id=#{id}; |
| </select> |
| |
| |
| |
| <select id="getStudent2" resultMap="StudentTeacher2" > |
| select s.id sid, s.name sname , t.name tname |
| from student s,teacher t |
| where s.tid = t.id |
| </select> |
| <resultMap id="StudentTeacher2" type="Student"> |
| <result property="id" column="sid"/> |
| <result property="name" column="sname"/> |
| |
| <association property="teacher" javaType="Teacher"> |
| <result property="name" column="tname"/> |
| </association> |
| </resultMap> |
| </mapper> |
一对多
| <?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.kuang.dao.TeacherMapper"> |
| <select id="getTeacher1" resultType="Teacher"> |
| select * from mybatis.teacher |
| </select> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <select id="getTeacher2" resultMap="TeacherStudent"> |
| select s.id sid, s.name sname , t.name tname, t.id tid |
| from student s,teacher t |
| where s.tid = t.id and t.id=#{id} |
| </select> |
| <resultMap id="TeacherStudent" type="Teacher"> |
| <result property="id" column="tid"></result> |
| <result property="name" column="tname"/> |
| <collection property="students" ofType="Student"> |
| <result property="id" column="sid" /> |
| <result property="name" column="sname" /> |
| <result property="tid" column="tid" /> |
| </collection> |
| </resultMap> |
| </mapper> |
标准mapper.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.kuang.mapper.BlogMapper"> |
| |
| </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> |
| |
| |
| </configuration> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2020-11-22 JDBC补充知识