
MyBatis总结 Day02
发布日期:2021-05-08 20:50:32
浏览次数:14
分类:原创文章
本文共 5133 字,大约阅读时间需要 17 分钟。
MyBatis优化以及问题处理
1.MyBatis的优化
经过快速入门以后,我们可以对之前的入门代码进行一些改进
①把数据库连接信息抽取至properties文件中
driverName=com.mysql.jdbc.Driver (根据驱动的依赖版本来决定com.mysql.cj.jdbc.Driver)url=jdbc:mysql://localhost:3306/XXXusername=rootpassword=root
②在mybatis-config配置文件中引入资源文件
<properties resource="db.properties"/> <typeAliases> <package name="com.fyk.entity"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${driverName}" /> <property name="url" value="${url}"/> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </dataSource> </environment> </environments>
③添加log4j日志(用于打印sql语句)
<1>引入相关依赖log4j
<2>创建日志的配置文件 log4j.properties
log4j.rootLogger=DEBUG, Console#Consolelog4j.appender.Console=org.apache.log4j.ConsoleAppenderlog4j.appender.Console.layout=org.apache.log4j.PatternLayoutlog4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%nlog4j.logger.java.sql.ResultSet=INFOlog4j.logger.org.apache=INFOlog4j.logger.java.sql.Connection=DEBUGlog4j.logger.java.sql.Statement=DEBUGlog4j.logger.java.sql.PreparedStatement=DEBUG
④可以为自己的实体类起别名
<typeAliases> <!--方式一:为单个实体类起别名,这样很麻烦--> <typeAlias type="com.xxx.entity.Teacher" alias="t"/> <!--方式二:通过package扫描的方式加载实体类--> <package name="com.xxx.entity"/> </typeAliases>
相对应的在Mapper映射文件中的体现
2.当实体类的属性名和数据库表中的字段不一致时的处理方式
①为查询的结果起别名(与属性名一致)
<select id="queryOneTeacher" resultType="Teacher"> select t_id as tid,t_name as tname from teacher where t_id=#{tid}</select>
②使用resultMap创建属性与字段的映射关系
<resultMap id="TeacherMap" type="Teacher"> <!--这里是主键列--> <id column="t_id" property="tid"/> <result column="t_name" property="tname"/> </resultMap> <select id="queryOneTeacher" resultMap="TeacherMap"> select t_id,t_name from teacher where t_id=#{tid} </select>
select中的resultMap与上方的resultMap中的id要对应!
3.MyBatis的联表查询
3.1 多对一
class表:
teacher表:
①方式一:直接进行多表联查
<!--直接联查方式--> <resultMap id="ClazzMap" type="Clazz"> <id column="classid" property="cid"/> <result column="classname" property="cname"/> <!--这里使用的是一个Teacher对象接收查询出来的Teacher参数--> <association property="teacher" javaType="Teacher"> <id column="t_id" property="tid"/> <result column="t_name" property="tname"/> </association> </resultMap> <select id="queryOneClazz" resultMap="ClazzMap"> select * from class c,teacher t where c.teacherid=t.t_id and c.classid=#{cid} </select>
控制台日志输出:
②方式二:通过两次查询得到相应的结果
<!--两次查询 ClazzMapper中的配置--> <resultMap id="ClazzMap" type="Clazz"> <id column="classid" property="cid"/> <result column="classname" property="cname"/> <!--这里使用TeacherDao中的Teacher查询方法,通过第一次查询的teacherid进行第二次查询--> <association property="teacher" javaType="Teacher" column="teacherid" select="com.xxx.dao.TeacherDao.queryOneTeacher"> <id column="t_id" property="tid"/> <result column="t_name" property="tname"/> </association> </resultMap> <select id="queryOneClazz" resultMap="ClazzMap"> select * from class where classid=#{cid} </select>
<!--两次查询 TeacherMapper中的配置--> <resultMap id="TeacherMap" type="Teacher"> <id column="t_id" property="tid"/> <result column="t_name" property="tname"/> </resultMap> <select id="queryOneTeacher" resultMap="TeacherMap"> select t_id,t_name from teacher where t_id=#{tid} </select>
控制台日志输出:
3.1 一对多
例如查询一个班级中的多个学生
Clazz实体类
<!--一对多联查方式--> <resultMap id="ClazzMap" type="Clazz"> <id column="classid" property="cid"/> <result column="classname" property="cname"/> <!-- Teacher --> <association property="teacher" javaType="Teacher"> <id column="t_id" property="tid"/> <result column="t_name" property="tname"/> </association> <!-- List<Student> ofType:表示集合的泛型 --> <collection property="students" ofType="Student"> <id column="sid" property="sid"/> <result column="sname" property="sName"/> <result column="ssex" property="sSex"/> <result column="classid" property="classId"/> </collection> </resultMap> <select id="queryOneClazz" resultMap="ClazzMap"> select * from class c,teacher t,student s where c.teacherid=t.t_id and c.classid=s.classid and c.classid=#{cid} </select>
控制台输出查询结果:
Clazz(cid=1,
cname=1班,
teacher=Teacher(tid=1, tname=LS1),
students=[Student(sid=2, sName=1, sSex=1, classId=1),
Student(sid=5, sName=1, sSex=1, classId=1),
Student(sid=12, sName=海绵宝宝, sSex=男, classId=1)])
4.MyBatis书写Sql语句使用特殊符号问题
举个栗子~
解决方法:
①使用转义字符
这里贴上常用的几个字符:
②使用CDATA
格式为:<![CDATA[sql]]>
同样举个栗子~
<select id="queryOneClazz" resultMap="ClazzMap"> <![CDATA[ select * from class c,teacher t,student s where c.teacherid=t.t_id and c.classid=s.classid and c.classid < 1 ]]> </select>
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月06日 15时17分24秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
openstack安装(九)网络服务的安装--控制节点
2021-05-08
shell编程(六)语言编码规范之(变量)
2021-05-08
vimscript学习笔记(二)预备知识
2021-05-08
Android数据库
2021-05-08
HTML基础,块级元素/行内元素/行内块元素辨析【2分钟掌握】
2021-05-08
STM8 GPIO模式
2021-05-08
omnet++
2021-05-08
23种设计模式一:单例模式
2021-05-08
Qt中的析构函数
2021-05-08
C语言实现dijkstra(adjacence matrix)
2021-05-08
三层框架+sql server数据库 实战教学-徐新帅-专题视频课程
2021-05-08
【单片机开发】智能小车工程(经验总结)
2021-05-08
【单片机开发】基于stm32的掌上游戏机设计 (项目规划)
2021-05-08
C++&&STL
2021-05-08
子集(LeetCode 78)
2021-05-08
微信js-sdk使用简述(分享,扫码功能等)
2021-05-08
mxsrvs支持thinkphp3.2伪静态
2021-05-08
c++中ifstream及ofstream超详细说明
2021-05-08
vuex modules
2021-05-08