> 文章列表 > Mybatis手动配置ORM,不用自动ORM等操作

Mybatis手动配置ORM,不用自动ORM等操作

Mybatis手动配置ORM,不用自动ORM等操作

一、解决mapper.xml存放在resources以外路径中的读取问题

在pom.xml文件最后追加< build >标签,以便可以将xml文件复制到classes中,并在程序运行时正确读取。

    <build><!-- 如果不添加此节点src/main/java目录下的所有配置文件都会被漏掉。 --><resources><resource><directory>src/main/java</directory><includes><include>/*.xml</include></includes></resource><resource><directory>src/main/resources</directory><includes><include>/*.xml</include><include>/*.properties</include><include>/*.ini</include></includes></resource></resources></build>

二、模糊查询

concat('%',#{keyword},'%')  <!-- 拼接'%' -->

<mapper namespace="com.zhp.mapper.UserMapper"><select id="selectUsersByKeyword" resultType="user">SELECT * FROM t_users WHERE name LIKE concat('%',#{keyword},'%') <!-- 拼接'%' --></select>
</mapper>

三、主键回填

标签:< selectKey id="" parameterType="" order="AFTER|BEFORE">

<!--namespace = 所需实现的接口全限定名-->
<mapper namespace="com.zhp.mapper.ProductMapper"><insert id="insertProduct">/*主键回填    在插入语句执行之后  查询刚刚插入的 记录 的 id  赋值给 productId */<selectKey keyProperty="productId" resultType="int" order="AFTER" >select last_insert_id()</selectKey>insert into t_product (productId,productName,brand) values (null,#{productName},#{brand})</insert>
</mapper>

四、MyBatis自动ORM失效

表的字段和实体类的属性名不同时,自动ORM就会失效,那么这时候我们就可以通过手动配置ORM来解决了

 结果映射(ResultMap - 查询结果的封装规则):

通过 < resultMap id="" type="" > 映射,匹配列名与属性名。

<?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"><!--namespace = 所需实现的接口全限定名-->
<mapper namespace="com.zhp.mapper.ManagerMapper"><resultMap id="managerResultMap" type="manager"><id property="id" column="mgr_id"></id><result property="name" column="mgr_name"></result><result property="password" column="mgr_pwd"></result></resultMap><select id="selectManagerByIdAndPwd2" resultMap="managerResultMap">select * from t_managers where mgr_id = #{id} and mgr_pwd=#{pwd}</select>
</mapper>

五、MyBatis处理关联关系-多表连接【重点】   

实体间的关系:关联关系(拥有 has、属于 belong)

  • OneToOne:一对一关系(Passenger--- Passport)

  • OneToMany:一对多关系(Employee --- Department)

  • ManyToMany:多对多关系(Student --- Subject)

 1.一对一

数据准备

DROP TABLE IF EXISTS `t_passenger`;CREATE TABLE `t_passenger` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(32) DEFAULT NULL,`sex` varchar(32) DEFAULT NULL,`birthday` date DEFAULT NULL,PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='乘客';/*Data for the table `t_passenger` */insert  into `t_passenger`(`id`,`name`,`sex`,`birthday`) values (1,'zs','男','2021-07-29'),(2,'lss','女','2021-07-28');DROP TABLE IF EXISTS `t_passport`;CREATE TABLE `t_passport` (`id` int(11) NOT NULL AUTO_INCREMENT,`nationality` varchar(32) DEFAULT NULL,`expire` date DEFAULT NULL,`passenger_id` int(11) DEFAULT NULL,PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;/*Data for the table `t_passport` */insert  into `t_passport`(`id`,`nationality`,`expire`,`passenger_id`) values (1,'中国','2023-07-29',1),(2,'韩国','2023-07-28',2);

实体类

public class Passenger {private Integer id;private String name;private String sex;private Date birthday;private Passport passport;
}public class Passport {private Integer id;private String nationality;private Date expire;private  Integer passenger_id;
}

.xml中的sql和手动的ORM

<mapper namespace="com.glls.mybatis.mapper.PassengerMapper"><!-- 结果映射(查询结果的封装规则) --><resultMap id="passengerResultMap" type="passenger"><id property="id" column="id"/><result property="name" column="name" /><result property="sex" column="sex" /><result property="birthday" column="birthday" /><!-- 关系表中数据的封装规则 -->	 <!-- 指定关系表的实体类型 --><association property="passport" javaType="passport"><id property="id" column="passportId"></id><result property="nationality" column="nationality"></result><result property="expire" column="expire"></result><result property="passenger_id" column="passenger_id"></result></association></resultMap><!-- 多表连接查询 -->					  	<!-- 结果映射(查询结果的封装规则)--><select id="selectPassengerById" resultMap="passengerResultMap"><!-- 别名(避免与p1.id冲突) -->SELECT p1.id , p1.name , p1.sex , p1.birthday , p2.id as passportId , p2.nationality , p2.expire 			, p2.passenger_idFROM t_passengers p1 LEFT JOIN t_passports p2ON p1.id = p2.passenger_idWHERE p1.id = #{id}</select>
</mapper>

注意:指定“一方”关系时(对象),使用 < association javaType=" "> 

2.待续...