In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail about the choice of MyBatis or JPA. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
I threw away JPA.
I thought about it carefully, and there are several reasons why JPA can't play in many teams at all.
JPA is suitable for scenarios with fixed business models and stable requirements. However, this capricious demand style in China and the microphone design pattern of product manager have caused the flooding and uncertainty of demand. JPA is scum in this mode.
The technical requirement of JPA is high. Don't doubt that it may be very easy for you to use it at first. But with your in-depth use, you will find that this is a disaster. The various conversions and caches inside will make people dizzy. And most fast-food programmers don't want to know about this.
Many programmers are good at writing SQL, so many SQL statements are fatter and fatter. Business chaos, multiple tables associated, I have even seen hundreds of business tables associated with complex business. Under DBA's helplessness, there is usually an sql audit. JPA does sql audit? Still a little weak.
Therefore, it is not that JPA is bad, but that it is not in line with the national conditions. If you want to promote JPA within the company, you need to give me a stable product team and an awesome technical team.
So, most companies would rather write a bunch of repetitive, messy MyBatis code than try JPA easily, which is logical and consistent with the law of things.
So, our next article is about MyBatis, let's take a look at how MyBatis should be written to be elegant.
Why doesn't MyBatis work well?
Good programmers are lazy. So a lot of people don't want to design physical sql. JPA can generate the library table of Java directly from the entity code of sql, which is the envy of people who use MyBatis.
To use MyBatis, do it backwards. You need to design the library table first, and then reverse generate a pile of Java code and configuration files from the library table.
The code generator is mybatis-generator.
However, please pay attention. There are four modes for the code generated by this generator! This is the most uncomfortable part for beginners. If you are new to MyBatis, it is highly recommended to focus only on the first mode below.
The MyBatis3 pattern is our common way to generate domain classes, example classes, mapper mapping files, and so on. The information it generates is verbose, and the content can hardly be changed. For the sql written by yourself in the project, it is common to write another copy by hand instead of changing the original file.
The simple code generation mode of the above pattern in MyBatis3Simple lacks something, but it is very concise. No experience with MyBatis and it is not recommended.
MyBatis3DynamicSql this is a dynamic SQL feature implemented through the Builder pattern, and you need to add additional jar packages. After adding it, it is actually a bit similar to JPA. In that case, why not just use JPA? So although this DSQL is the default generation behavior, it is highly deprecated.
MyBatis3Kotlin, this is nonsense. Just generate some configuration and code information for the Kotlin version.
So, here's just the code generation for the MyBatis3 pattern.
To use it, you need to add its dependencies to pom.xml.
Org.mybatis.generator mybatis-generator-core true test 1.4.0
I personally like to use Java code to manipulate the code generation process, so here is the code to generate the code.
Public class MBGTool {public static void main (String [] args) throws Exception {List warnings = new ArrayList (); boolean overwrite = true; InputStream configFile = MBGTool.class.getResourceAsStream ("/ generator/generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser (warnings); Configuration config = cp.parseConfiguration (configFile); DefaultShellCallback callback = new DefaultShellCallback (overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator (config, callback, warnings); myBatisGenerator.generate (null) }}
From the code, we can see that we need to configure a generatorConfig.xml file to specify how to generate the code file.
After running our MBGTool file, we can generate the code for MyBatis.
How to write the most elegant code
However, I am not here to recommend you to use this mode. Because it generates a lot of useless files. If your project uses a code quality review tool like sonar, you will find a lot of red spots and fatal coverage issues.
What shall I do?
After years of exploration, I now recommend a very easy-to-use way of writing. I haven't changed it since I adopted this method.
First, you don't need a code generator.
The design of the datasheet, as well as the writing of domain, are all manual. So that our code, if necessary, can be migrated to JPA. By the way, this mode can also learn how the data types in Java correspond to the data types in SQL. When designing a watch, you can learn some of the principles behind it.
Second, there is no need to write mapping files.
What the generator generates does have a bunch of useless logic. For example, one of my datasheets does not need to provide query all and delete actions at all, but it is provided by default.
In this minimalist mode, we hand-write the Mapper file directly, and then declare only the interface methods we need.
@ Mapperpublic interface AccountBasicMapper {@ Insert ("AccountBasicMapper/insert.sql") void insert (@ Param ("r") AccountBasic record);}
As you can see, there is an Insert comment, and we pass in a specific domain, and then we can write the specific sql statement in the insert.sql file in the AccountBasicMapper directory.
A sample sql statement is as follows:
INSERT INTO account_basic (account_id, nick_name, password, sex, state, photo_url, created, modified, version) VALUES (,)
So what kind of grammar is this? How does it know it is configured in this way? This requires the introduction of the scripting language configuration feature of MyBatis. Here, we use the freemark template.
Don't forget to add its dependency.
Org.mybatis.scripting mybatis-freemarker 1.2.2
Then, make the corresponding configuration in the yaml file and ok.
Advantages and disadvantages of mybatis: check_config_location: false scripting-language-driver: freemarker: template-file: base-dir: mappers/ path-provider: includes-package-path: false separate-directory-per-mapper: false
Personally, I like this model very much. Because it has the following benefits:
What to write with, less code, concise and elegant.
SQL is centralized, not scattered in code, xml, or annotations. It is convenient for DBA to conduct SQL audit. Without the interference of xml, SQL is more concise.
One DAO method and one sql file, the mode is single and controllable.
The functional advantages of MyBatis can be brought into full play and seamlessly integrated.
Of course, the shortcomings are also obvious.
Even if you change a parameter, you have to modify a lot of sql files.
You need a sql file for each method, even if it's a stupid insert query method.
But I don't think that's a problem. Each method is equipped with a sql file, which makes the code easier to write. When there is a problem, there is no need to trace and locate the spliced SQL statement according to logic. Now, I just need to get the SQL file of the corresponding method, and I can change it and debug it directly in the sql terminal. In this way, sql optimization becomes easier.
Of course, one person has a habit. I personally like this model, and I implemented it in my team and found that it worked very well. In addition, programmers are more careful when designing Dao interfaces in order to write less repetitive sql code.
This is the end of the article on "choose MyBatis or JPA". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.