The way to monitor SQL and ORM frameworks utilizing MyBatis
As we’ve got seen, Hibernate simplifies the persistent layer. Nonetheless, the developer should perceive what’s occurring underneath the hood to optimize the default Framework’s conduct. Alternatively, there may be one other in style Java Framework that doesn’t do lots of “black magic” underneath the hood, particularly MyBatis. In accordance with documentation
MyBatis is a first-class persistence framework with help for customized SQL, saved procedures and superior mappings
The builders who use MyBatis have full management over the SQL queries utilizing a user-friendly XML or an annotation-based API (with an introduction of multiline Strings in java 14, the Annotation API ought to be most well-liked). Let’s rewrite the earlier instance utilizing MyBatis. First, we have to remove all JPA-specific annotations from Entity lessons. Subsequent, we’ve got to jot down SQL on our personal utilizing XML recordsdata. For this instance, we are going to rewrite the Repository class that saves the consumer with corresponding orders (Code examples can be found right here). Right here is the Mapper class:
@Mapper public interface UserMapper { void saveUser(Person consumer); void saveOrders(@Param("orders") Listing<Order> orders, @Param("userId") lengthy userId); default void saveUserWithOrders(Person consumer) { this.saveUser(consumer); this.saveOrders(consumer.getOrders(), consumer.getId()); } }
and right here is the corresponding XML file:
<mapper namespace="com.fusion.reactor.UserMapper"> <resultMap id="UserResultMap" kind="com.fusion.reactor.Person"> <id column="id" property="id"/> <consequence column="identify" property="identify"/> </resultMap> <insert id="saveUser" parameterType="com.fusion.reactor.Person" useGeneratedKeys="true" keyProperty="id" keyColumn="id"> INSERT INTO customers (identify) VALUES (#{identify}) </insert> <insert id="saveOrders" parameterType="map"> INSERT INTO orders ( value,user_id,identify ) VALUES <foreach assortment="orders" merchandise="order" index="index" open="(" separator="),(" shut=")"> #{order.value}, #{userId}, #{order.identify} </foreach> </insert> </mapper>
As you possibly can see, we explicitly wrote two queries, first to avoid wasting the consumer and second to avoid wasting an inventory of orders. No autogenerated queries and no automapping as in Hibernate, and the whole lot must be executed manually. Let’s rerun load assessments and see the JDBC transaction historical past.
FusionReactor will present you that MyBatis generated queries that you just specified with all of the parameters displayed, so you possibly can confirm that mappers are working as anticipated Going to the Relations web page in transaction element for one of many requests, we will see {that a} single request used 1 transaction, and a couple of JDBC queries:
The MyBatis plugin for IntelliJ has language server help to jot down type-safe XML recordsdata. Nonetheless, if you need compile-time help there may be one other framework to contemplate.