1024programmer Blog [Mybatis source code analysis 3] CRUD operations based on Mybatis using annotations_mybatis annotation source code_OneTask’s blog

[Mybatis source code analysis 3] CRUD operations based on Mybatis using annotations_mybatis annotation source code_OneTask’s blog

Table of Contents

1. Define the database interface and define the interface method , corresponding to the method in mapper.xml

2. Modify the reference in the mappers mapper in the myabtis-conf.xml configuration file

3. Through the Sqlsession object The getMapper method instantiates the specified interface object under the package referenced by the mappers mapper in the myabtis-conf.xml configuration file, and calls the interface method through the interface object to perform CRUD operations

【Mybatis source code analysis 2】Based on Mybatis using mapper.xml to implement CRUD operations

It is still troublesome to use .xml to write sql method

mybatis provides @Insert, @Select, @Update, @Delete annotations to describe the method of adding, deleting, modifying and checking to realize CRUD operations (mybatis3 starts to provide @InsertProvider, @SelectProvider, @UpdateProvider, @DeleteProvider annotations, It is used to dynamically generate SQL statements based on parameters without method overloading, so let’s not talk about this here)

1. Define the database interface, define the interface method, and correspond to Method in mapper.xml

package DAO;

 import Entity. User;
 import org.apache.ibatis.annotations.Delete;
 import org.apache.ibatis.annotations.Insert;
 import org.apache.ibatis.annotations.Select;
 import org.apache.ibatis.annotations.Update;

 import java.util.List;

 public interface UserDAO {

     @Select("select * from manage where id = #{id}")
     User selectById(int id) ;

     @Select("select * from manage where name = #{name}")
     User selectByName(String name);

     @Select("select * from manage")
     List selectAll();

     @Insert("insert manage(id,name) values(#{id}, #{name})")
     int insertUser(User user);

     @Update("update manage set name=#{name} where id=#{id}")
     int updateUserById(User user);

     @Delete("delete from manage where id = #{id}")
     int deleteUserById(int id);

 }
 

2 . Modify the references in the mappers mapper in the myabtis-conf.xml configuration file

 
     
         
         
 <!-- -->
     

3. Instantiated by Sqlsession object getMapper method The interface object specified under the package referenced by the mappers mapper in the myabtis-conf.xml configuration file, calls the interface method through the interface object to perform CRUD operations

package Service;

 import DAO. UserDAO;
 import Entity. User;
 import org.apache.ibatis.io.Resources;
 import org.apache.ibatis.session.SqlSession;
 import org.apache.ibatis.session.SqlSessionFactory;
 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 import org.junit.Test;

 import java.io.IOException;
 import java.io.InputStream;

 public class UserService {


     @Test
     public void test() throws IOException {

         String resource = "mybatis-conf.xml";
         InputStream inputStream = Resources. getResourceAsStream(resource);
         SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
         SqlSession sqlSession = sqlSessionFactory. openSession();

         //Get the mapper interface specified in the package where the interface referenced in the mapper of mybatis-conf.xml is located through SqlSession
         UserDAO mapper = sqlSession. getMapper(UserDAO. class);

         User newUser = new User();
         newUser.setId(8);
         newUser.setName("p7");
 // sqlSession.insert("insertUser", newUser);
         mapper.insertUser(newUser);

 // System.out.println(sqlSession.selectList("selectAll"));
         System.out.println(mapper.selectAll());


         newUser.setName("p8");
 // sqlSession. update("updateUserById", newUser);
         mapper.updateUserById(newUser);

 // System.out.println(sqlSession.selectList("selectAll"));
         System.out.println(mapper.selectAll());

 // sqlSession. delete("deleteUserById", 8);
         mapper.deleteUserById(8);

 // System.out.println(sqlSession.selectList("selectAll"));
         System.out.println(mapper.selectAll());

         sqlSession.commit();
         sqlSession. close();
     }


 }
 

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/mybatis-source-code-analysis-3-crud-operations-based-on-mybatis-using-annotations_mybatis-annotation-source-code_onetasks-blog/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索