Find out how to monitor SQL and ORM frameworks utilizing MyBatis
As we’ve got seen, Hibernate simplifies the persistent layer. Nevertheless, the developer should perceive what’s happening beneath the hood to optimize the default Framework’s conduct. Alternatively, there’s one other fashionable Java Framework that doesn’t do a variety of “black magic” beneath the hood, specifically MyBatis. Based on documentation
MyBatis is a firstclass 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 must be most well-liked). Let’s rewrite the earlier instance utilizing MyBatis. First, we have to get rid of 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’ll rewrite the Repository class that saves the person with corresponding orders (Code examples can be found right here). Right here is the Mapper class:
@Mapper public interface UserMapper { void saveUser(Consumer person); void saveOrders(@Param("orders") Checklist<Order> orders, @Param("userId") lengthy userId); default void saveUserWithOrders(Consumer person) { this.saveUser(person); this.saveOrders(person.getOrders(), person.getId()); } }
and right here is the corresponding XML file:
<mapper namespace="com.fusion.reactor.UserMapper"> <resultMap id="UserResultMap" kind="com.fusion.reactor.Consumer"> <id column="id" property="id"/> <end result column="identify" property="identify"/> </resultMap> <insert id="saveUser" parameterType="com.fusion.reactor.Consumer" useGeneratedKeys="true" keyProperty="id" keyColumn="id"> INSERT INTO customers (identify) VALUES (#{identify}) </insert> <insert id="saveOrders" parameterType="map"> INSERT INTO orders ( worth,user_id,identify ) VALUES <foreach assortment="orders" merchandise="order" index="index" open="(" separator="),(" shut=")"> #{order.worth}, #{userId}, #{order.identify} </foreach> </insert> </mapper>
As you possibly can see, we explicitly wrote two queries, first to save lots of the person and second to save lots of an inventory of orders. No autogenerated queries and no automapping as in Hibernate, and the whole lot needs to be carried out manually. Let’s rerun load assessments and see the JDBC transaction historical past.
FusionReactor will present you that MyBatis generated queries that you simply 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 pair of JDBC queries:
The MyBatis plugin for IntelliJ has language server help to jot down type-safe XML recordsdata. Nevertheless, in order for you compile-time help there’s one other framework to contemplate.