MyBatis优化以及问题处理
1.MyBatis的优化
经过快速入门以后,我们可以对之前的入门代码进行一些改进
①把数据库连接信息抽取至properties文件中
driverName=com.mysql.jdbc.Driver (根据驱动的依赖版本来决定com.mysql.cj.jdbc.Driver)
url=jdbc:mysql://localhost:3306/XXX
username=root
password=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
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.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>