In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to use MyBatis in the ssm framework. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
MyBatis introduction
1. MyBatis is an excellent persistence layer framework that supports customized SQL, stored procedures, and high-level mapping. MyBatis avoids almost all JDBC code and manually setting parameters and getting collections.
2Magol MyBatis can use simple XML or annotations for configuration and native Map to map interface and Java entity classes to records in the database.
Advantages of MyBatis
MyBatis is a persistence layer framework, it is the encapsulation of JDBC, to learn MyBatis, the first thing is to have a deeper understanding of JDBC, here by comparing JDBC to have a preliminary understanding of MyBatis, that is, why we use MyBatis.
There are some problems in using JDBC to operate the database, such as code redundancy, need to write a lot of repetitive code, not easy to maintain and so on. MyBatis was born to solve these pain points.
Here are several typical scenarios
JDBC (problem)
1. Database connections are created when they are in use, and do not use immediate release, and frequent connections to the database are opened and closed, resulting in a waste of database resources and affecting database performance.
2. Hard-code the sql statement into the Java code. If the sql statement is modified, the java code needs to be recompiled, which is not conducive to system maintenance.
3, setting parameters to preparedStatement, and hard-coding the placeholder position and setting parameter values in java code, which is not conducive to system maintenance.
4. When traversing the result set data from resutSet, there is hard coding, and the fields that get the table are hard-coded, which is not conducive to system maintenance.
MyBatis (countermeasure)
1. Use database connection pooling to manage database connections.
2, configure the sql statement in the xml configuration file, even if the sql changes, there is no need to recompile the java code.
3. Configure the sql statement and placeholder symbols and parameters in xml.
4. Automatically map the result set of the query to java objects.
Compared with the JDBC persistence process, a lot of MyBatis work is set in the configuration file, which reduces a lot of code, and makes the code structure clearer and easier to maintain.
The workflow of JDBC
1. Load the database driver
2, create and get database links
3, create a jdbc statement object
4, set the sql statement
5. Set parameters in the sql statement
6. Execute sql through statement and get the result
7. Parse the execution result of sql
8, release resources
The workflow of MyBatis
1. Configure the configuration file of mybatis, SqlMapConfig.xml
2, through the configuration file, load the mybatis runtime environment, and create the SqlSessionFactory session factory.
3. Create SqlSession through SqlSessionFactory. SqlSession is a user-oriented interface (provides methods to manipulate the database), the implementation object is thread-unsafe, it is recommended that the sqlSession application is in the method body.
4. Call the method of sqlSession to manipulate the data. If you need to commit the transaction, you need to execute the commit () method of SqlSession.
5. Release resources and close SqlSession
The use of MyBatis (brief introduction)
Write a mapper interface and the corresponding XML file mapping, and give all the SQL statements to XML for management.
The following specifications need to be followed when writing mapper.xml (mapping files) and mapper.java:
1 namespace in mapper.java mapper. XML is the full path of the class.
2the legal names of id and mapper.java in statement in mapper.java mapper.xml are the same.
3The type of the input parameter specified by the parameterType of statement in statement is the same as that of the method input parameter of mapper.java.
4the resultType of statement in statement specifies that the type of output is the same as the type of value returned by mapper.java 's method.
Here is an example of a simple use of mapper
Mapper.java
Public interface CommitMapper {
/ * add comments * / public void addCommit (Commit commit) throws Exception
/ * * Delete comments * / public List findCommitList (Integer commit_id) throws Exception;}
Mapper.xml
Insert into tb_commit
(
Commit_content
Commit_user_id
Commit_goods_id
)
Value (
# {commit_content}
# {commit_user_id}
# {commit_goods_id}
) select c.commit_id
C.commit_content from
Tb_commit c
Tb_user u
Tb_goods g
Where
C.commit_user_id=u.user_id and c.commit_id=g.id
And c.commit_id=# {commit_id}
Noun interpretation
1ParameterType MyBatis specifies the input parameter type through parameterType
2. The output result is specified by resultType in the resultType MyBatis.
3Jing # {}: indicates a placeholder. The parameters received by # {} are input parameters, and the types can be simple type, pojo, hashmap.
# {} if a simple type is received, the value in it can be value or other name
# {} receives the pojo object, reads the attribute values in the object through OGNL, and then reads the attribute values through the attribute. Property is obtained by means of the.
4 selectone selectOne: indicates that a record is queried. If the result can be achieved using selectone, selectList can also be achieved (there is only one object in list).
5SelectList: indicates that a list (multiple records) is queried for mapping. It is not possible to use selectOne.
Thank you for reading! This is the end of the article on "how to use MyBatis in the ssm framework". 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, you can 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.