What are the similarities and differences between Mybatis and hibernate?

This article is here to give you a popular science about MyBatis and Hibernate to see their similarities and differences.

SQL processing

First, let's look at their SQL handling. MyBatis adopts the method of SQL mapping file. Like seasoning, developers need to provide SQL statements and variables to make themselves well-fed; while Hibernate adopts the method of ORM, which directly converts Java objects into SQL for execution like cooking. The first is the code snippet of MyBatis. You can see that SQL statements need to be manually processed when using MyBatis:

public interface StudentDao {
    @Select("SELECT * FROM student WHERE id = #{id}")
    Student findById(@Param("id") long id);
}

Hibernate is based on the implementation of JPA annotations, and many SQL operations are automatically generated:

@Entity
@Table(name = "student")
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "name")
    private String name;

    @Column(name = "age")
    private int age;
}

Hibernate performs sql operations:

@Service
@Transactional
public class StudentServiceImpl implements StudentService {

    private StudentDao studentDao;

    @Autowired
    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }

    @Override
    public Student findById(long id) {
        return studentDao.findById(id).orElse(null);
    }

    @Override
    public void save(Student student) {
        studentDao.save(student);
    }

    @Override
    public List<Student> findAll() {
        return (List<Student>) studentDao.findAll();
    }

    // ...
}

cache

Then let's talk about caching. MyBatis uses a mechanism based on the second-level cache to allow SQL statements to be executed in memory. Hibernate's advanced caching mechanism is more powerful and can instantly improve system response speed.

In terms of caching, MyBatis can manually choose to enable the first-level and second-level caches:

<cache eviction="LRU" flushInterval="10000" size="1024"/>

Hibernate has more caching options, including second-level caching and query caching:

<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>

Let’s look at the data access method again. MyBatis data access is implemented based on DAO. Developers need to manually implement and maintain the DAO interface, like chopping vegetables, and handle the DAO interface by themselves; Hibernate is implemented based on the JPA specification, using annotation marks Entity classes, like making a big meal by yourself, don't require much patience and energy.

In terms of data access, MyBatis and Hibernate use different methods. The data access of MyBatis is implemented based on DAO, and the DAO interface needs to be implemented manually:

public interface StudentMapper {
    Student selectById(Long id);
}

Hibernate is directly annotated on the entity class, and does not need to explicitly define the DAO interface.

Mapping method

Let's talk about how they are mapped. The MyBatis SQL mapping file needs to manually specify each database field, there is no default value, and it can be customized as in the kitchen; Hibernate usually maps to the database column according to the object attribute, if not specified, use the annotation or the definition in the configuration file The default value of , like automatic home delivery, will save you a lot of work.

Take a look at the mapping method, this is a MyBatis SQL mapping file:

<select id="findById" parameterType="long" resultMap="studentResultMap">
    SELECT * FROM student WHERE id = #{id}
</select>

Hibernate, on the other hand, maps to columns in the database according to object properties, and generally does not need to be processed:

@Entity
@Table(name = "student")
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "name")
    private String name;

    @Column(name = "age")
    private int age;
}

The above are some simple code snippets, which illustrate several differences between MyBatis and Hibernate. I believe that through these examples, you can better understand the difference between the two and choose the appropriate ORM framework.

Summarize

MyBatis and Hibernate have their own advantages in use. It is necessary to choose the most suitable ORM framework according to the actual project requirements. Just like cooking a good dish, good kitchen utensils and seasoning are the foundation, but craftsmanship is also very important. Everyone uses a kitchen knife, the important thing is the taste of the dish.

Guess you like

Origin blog.csdn.net/weixin_39570751/article/details/130014322
Recommended