HOME
Search
Notification
Login
What is difference between MyBatis and JPA?
Author:
| Modified:
2023-11-05 04:36
| Viewed:
288
In JPA(Hibernate) and ORM frameworks, you map Objects/Entities to the corresponding sql tables and you work on objects and not on tables directly unless you use their native queries. In mybatis, you play directly with sql data. You can use Spring Data JPA for writing (INSERT, UPDATE, DELETE query) and use MyBatis for reading (SELECT query). Following are steps for using MyBatis in Spring: 1. Use annotation to bind SQL ``` public class Category { private long id; private String name; } @Mapper public interface CategoryMapper { @Select("select id, name, type, active from category where id = #{id}") Category findById(@Param("id") long id); } ``` 2.Setup SQL mapping in xml ``` <mapper namespace="com.demo.mapper.CategoryMapper"> <select id="findById" resultType="Category"> select id, name from category where id = #{id} </select> </mapper> ``` 3.Add xml path to application.properties: mybatis.mapper-locations=classpath*:/mappers/*.xml
java
sql
spring
1
reply
@Transactional 事务不要滥用。事务会影响数据库的 QPS,另外使用事务的地方需要考虑各方面的回滚方案,包括缓存回滚、搜索引擎回滚、消息补偿、统计修正等
0
2023-11-05
Comment
Submit
Anonymous
Reply
Author
Related Topic
How to prevent useEffect re-render same value?
Why Realm database?
What you need to prepare to be a fullstack?
test
Zustand for State Management in React