Table of Contents
2. Modify the reference in the mappers mapper in the myabtis-conf.xml configuration file
【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();
}
}