m0_ sixty-four million eight hundred and sixty-seven thousand t 2022-01-26 19:35:56 阅读数:197
SqlSession It's a user oriented ( The programmer ) The interface of .
SqlSession There are many ways to operate the database : Such as :selectOne( Return a single object )、selectList( Returns a single or multiple object ).
SqlSession It's not thread safe , stay SqlSesion In addition to the methods in the interface in the implementation class ( How to operate database ) And data field properties .
SqlSession**** The best application is in the method body , Defined as a local variable using .
** Two 、** original dao development method ( Programmers need to write dao Interface and dao Implementation class )
===================================
2.1、 Ideas
Programmers need to write dao Interface and dao Implementation class .
stay dao Write in interface sql Statements correspond to abstract methods
towards dao Get... From the implementation class SqlSession, utilize SqlSession The instance selectOne()、selectList()、insert()、update()、delete() Other methods , and po Class xml In the mapping file sql Statement to establish a connection .
2.2、dao Interface
package mybatis.dao;
import mybatis.po.User;
import java.util.List;
public interface UserDao {
User findUserById(int id) throws Exception;
List findUserByName(String name) throws Exception;
void insertUser(User user) throws Exception;
void deleteUser(int id) throws Exception;
void updateUser(User user) throws Exception;
}
2.3、dao Interface implementation class
package mybatis.dao;
《 A big factory Java Analysis of interview questions + Back end development learning notes + The latest architecture explanation video + Practical project source code handout 》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 Full content open source sharing
import mybatis.po.SqlSessionUtils;
import mybatis.po.User;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
public class UserDaoImpl implements UserDao{
@Override
public User findUserById(int id) throws Exception {
SqlSession sqlSession= SqlSessionUtils.getSqlSession();
User user=sqlSession.selectOne(“user.findUserById”,id);
sqlSession.close();
return user;
}
@Override
public List findUserByName(String name) throws Exception {
SqlSession sqlSession=SqlSessionUtils.getSqlSession();
List users= sqlSession.selectList(“user.findUserByName”,name);
sqlSession.close();
return users;
}
@Override
public void insertUser(User user) throws Exception {
SqlSession sqlSession=SqlSessionUtils.getSqlSession();
sqlSession.insert(“user.insertUser”,user);
sqlSession.commit();
sqlSession.close();
}
@Override
public void deleteUser(int id) throws Exception {
SqlSession sqlSession=SqlSessionUtils.getSqlSession();
sqlSession.delete(“user.deleteUser”,id);
sqlSession.commit();
sqlSession.close();
}
@Override
public void updateUser(User user) throws Exception {
SqlSession sqlSession=SqlSessionUtils.getSqlSession();
sqlSession.update(“user.updateUser”,user);
sqlSession.commit();
sqlSession.close();
}
}
2.4、 The test program
package test;
import mybatis.dao.UserDao;
import mybatis.dao.UserDaoImpl;
import mybatis.po.User;
import org.junit.Test;
import java.util.List;
public class UserDaoImplTest {
@Test
public void findUserByIdTest() throws Exception {
UserDao userDao=new UserDaoImpl();
User user=userDao.findUserById(1);
System.out.println(user);
}
@Test
public void findUserByNameTest() throws Exception {
UserDao userDao=new UserDaoImpl();
List users=userDao.findUserByName(“ Xiao Ming ”);
System.out.println(users);
}
@Test
public void insertUserTest() throws Exception {
UserDao userDao=new UserDaoImpl();
User user = new User();
user.setUsername(“ Zhou Zhiruo ”);
user.setAddress(“ emei ”);
user.setSex(“ Woman ”);
userDao.insertUser(user);
}
@Test
public void updateUser() throws Exception {
UserDao userDao=new UserDaoImpl();
User user = new User();
user.setId(1);
user.setUsername(“ Lu Han ”);
user.setSex(“ male ”);
userDao.updateUser(user);
}
@Test
public void deleteUser() throws Exception {
UserDao userDao=new UserDaoImpl();
userDao.deleteUser(40);
}
}
2.5、 original dao Summary of development methods
The main steps are :
1、 Import what you need jar package
2、 The driver、url、user、password Such information is encapsulated in db.properties
3、 establish mybatis Of xml file SqlMapConfig.xml, And load the database information file db.properties, And complete the construction of the environment , So that you can connect to the database
4、 Corresponding when creating operation database tables po class , The columns of the query consist of po Attributes of a class
5、 To write po Class , Write... In it sql sentence
6、 stay mybatis Of xml file SqlMapConfig.xml Load in po Class
( The first six steps refer to MyBatis Detailed explanation ( One ): Entry procedure )
7、 To write dao Interface , And write po class xml In the mapping file sql The abstract method corresponding to the statement
8、 To write dao Implementation class of interface , stay dao Get... From the implementation class SqlSession, utilize SqlSession The instance selectOne()、selectList()、insert()、update()、delete() Other methods , and po Class xml In the mapping file sql Statement to establish a connection .
9、 Writing test classes , Get... In the test class dao The interface implements the object of the class , Call one of the methods .
2.6、 original dao Development issues
1、dao There are a lot of template methods in interface implementation class methods , Imagine if you can extract this code , Greatly reduce the workload of programmers .
2、 utilize SqlSession The instance selectOne()、selectList()、insert()、update()、delete() And so on statement Of id The hard coded
3、 call sqlsession Method , because sqlsession Method uses generics , Even if the variable type is passed in incorrectly , No errors are reported in the compile phase , It's not good for programmers to develop .
3、 ... and 、mapper Proxy method ( Programmers only need mapper Interface ( Quite a On dao Interface ))
=======================================
3.1、mapper Agent development specification
Programmers also need to write mapper.xml The mapping file
Written by programmer mapper The interface needs to follow some development specifications ,mybatis Can be generated automatically mapper Interface implementation class proxy object .
The development of specification :
1、 stay mapper.xml in namespace be equal to mapper Address of the interface ;
2、mapper.java Interface Method name and mapper.xml in statement Of id Agreement ;
3、mapper.java Interface Method input parameter type and mapper.xml in statement Of parameterType The specified type is consistent ;
( When the input parameter types are inconsistent, an error will be reported , Solve original dao Development issues :, because sqlsession Method uses generics , Even if the variable type is passed in incorrectly , No errors are reported in the compile phase );
4、mapper.java Methods in interfaces return type and mapper.xml in statement Of resultType The specified type is consistent .
3.2、 To write mapper Mapping file corresponding to the interface mapper.xml
According to the user id Find the user sql Statements, for example
<?xml version="1.0" encoding="UTF-8" ?>SELECT * FROM USER WHERE id= #{id}
3.3、 To write mapper Interface mapper.java
package mybatis.mapper;
import mybatis.po.User;
public interface UserMapper {
public User findUserById(int id) throws Exception;
}
3.4、 stay SqlMapConfig.xml Load in mapper.xml
Here are two methods in SqlMapConfig.xml Load the mapping file in mapper.xml
1、 adopt mapper Interface load single The mapping file
Specifications to be followed : Need to put mapper Interface class name and mapper.xml Map file names consistently , And in a directory
The premise of the above norm is : It uses mapper Proxy method
2、 Bulk load mapper
copyright:author[m0_ sixty-four million eight hundred and sixty-seven thousand t],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201261935550052.html