1, What is mybatis
Many IT xiaomengxin will learn mybatis in the process of learning programming, but when they first come into contact, they may have these questions. What is mybatis, what are its characteristics, and what is the use of learning. With your questions, let's see Xiaochen's next!
1. Mybatis is an excellent persistence layer framework, which supports customized SQL, stored procedures and advanced mapping.
2. Mybatis avoids almost all JDBC code, manually setting parameters and obtaining result sets.
3. Mybatis can use simple XML or annotations to configure and map native information, and map interfaces and Java POJOs (plain ordinary Java objects) to records in the database.
In general, mybatis is a semi-finished product that encapsulates the interaction with the database. The following is the architecture diagram of mybatis.
2, Why do you need Mybatis
1. Mybatis is to help the program store data in the database and get data from the database. Traditional jdbc operations have many repeated code blocks, such as encapsulation when data is taken out, establishment of database connection, etc.. The framework can reduce repeated code and improve development efficiency
2. Mybatis is a semi-automatic ORM framework (object relationship mapping) - > object relationship mapping
All things can still be done without Mybatis, but with it, all the implementation will be easier! There is no difference between high and low technology, only the people who use it.
3, Advantages of Mybatis
1. Compared with JDBC, the amount of code is reduced by more than 50%.
2. MyBatis is the simplest persistence framework, small and easy to learn.
3. MyBatis is flexible and will not impose any impact on the existing design of the application or database. SQL is written in XML, completely separated from the program code, reducing the degree of coupling, facilitating unified management and optimization, and reusable.
4. Provide XML tags to support writing dynamic SQL statements (if and else are used in XML).
5. Provide mapping labels to support object and database ORM field relational mapping (configure the mapping relationship in XML, or use annotations).
To sum up: with MyBatis, you don't have to write native JDBC code anymore. Do you think it smells good!
4, Use of Mybatis
The above is a simple introduction to Mybatis. I believe tietie here can't wait to use Mybatis to write programs. Let's talk less and go directly to the code!
1. First create a maven java project;
2. Introduce mybatis jar and mysql driver jar package.
<dependencies> <!--mysql Drive dependency--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> </dependency> <!--mybatis Dependence of--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.22</version> </dependency> </dependencies>
3. Create the corresponding entity class
4. Configure the configuration file of mybatis -- mybatis will read the contents of the file and complete the function of connecting to the database.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatis" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> </configuration>
5. Write the corresponding mapping file----- Mapping between sql statement entity class and table.
<mapper namespace="a"> <select id="getUserById" resultType="com.ykq.entity.User"> select * from users where id=# </select> </mapper>
6. Import the mapping file into the mybatis configuration file.
7. Test plus CRUD operation
public class TestUser { @Test public void testSellAll() throws Exception { Reader reader = Resources.getResourceAsReader("mybatis.xml"); SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader); SqlSession session=sqlSessionFactory.openSession(); List<User> list = session.selectList("a.selectAll"); System.out.println(list); } @Test public void testSelectById() throws Exception { Reader reader = Resources.getResourceAsReader("mybatis.xml"); SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader); SqlSession session=sqlSessionFactory.openSession(); User user = new User(); user.setId(3); List<User> list = session.selectList("a.getUserById", user); System.out.println(list); } @Test public void testUpdate() throws Exception { Reader reader = Resources.getResourceAsReader("mybatis.xml"); SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader); SqlSession session=sqlSessionFactory.openSession(); User user = new User(); user.setName("zx"); user.setPassword("345"); user.setRealname("Small dust"); user.setId(2); session.update("a.updateUser",user); session.commit(); } @Test public void testInsert() throws Exception { Reader reader = Resources.getResourceAsReader("mybatis.xml"); SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader); SqlSession session=sessionFactory.openSession(); User user = new User(); user.setName("cc"); user.setPassword("321"); user.setRealname("Mo Xiaochen"); int row = session.insert("a.addUser",user); System.out.println(); session.commit(); } @Test public void testdelete() throws Exception { Reader reader = Resources.getResourceAsReader("mybatis.xml"); SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader); SqlSession session=sessionFactory.openSession(); User user = new User(); user.setId(3); int delete = session.delete("a.deleteUser", user); System.out.println(delete); session.commit(); } }