mybatis learning notes, 2.MyBatisTest
1. Introduction
1. Interface programming
- Native: Dao = = = = > daoimpl
- mybatis: Mapper ====> xxMapper.xml
2. SqlSession represents a session with the database; It must be closed after use;
3. SqlSession, like connection, is non thread safe. You should get a new object every time you use it.
4. The mapper interface does not implement classes, but mybatis will generate a proxy object for this interface. (bind interface to xml)
EmployeeMapper empMapper = sqlSession.getMapper(EmployeeMapper.class);
5. Two important profiles:
- Global configuration file of mybatis: contains database connection pool information, transaction manager information, etc... system operation environment information
- sql mapping file: saves the mapping information of each sql statement: extract the sql.
2. Get SqlSessionFactory
public SqlSessionFactory getSqlSessionFactory() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); return new SqlSessionFactoryBuilder().build(inputStream); }
- 1. Create a SqlSessionFactory object according to the xml configuration file (global configuration file), which has some running environment information of the data source
- 2. sql mapping file; Configure each sql and sql encapsulation rules.
- 3. Register the sql mapping file in the global configuration file
- 4. Write code:
- 1) . obtain SqlSessionFactory according to the global configuration file;
- 2) . use the sqlSession factory to obtain the sqlSession object and use it to perform addition, deletion, modification and query. An sqlSession represents a session with the database and is closed after use
- 3) . use the unique flag of sql to tell MyBatis which sql to execute. sql is saved in the sql mapping file.
3. Test test01() directly implements the SqlSession object
- Get sqlSessionFactory object
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); - Get SqlSession object according to sqlSessionFactory
SqlSession openSession = sqlSessionFactory.openSession(); - Implement sql query according to SqlSession. Method (method, parameter).
openSession.selectOne("mybatis.dao.EmployeeMapper.getEmpById", 3);
@Test public void test() throws IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(); try { Employee employee = openSession.selectOne( "mybatis.dao.EmployeeMapper.getEmpById", 3); System.out.println(employee); } finally { openSession.close(); } }
Get the sqlSession instance and directly execute the mapped sql statements
Unique identifier of sql: statement Unique identifier matching the statement to use
parameter A parameter object to pass to the statement
4. Test test02() to obtain the implementation class object of the interface getMapper
-
Get sqlSessionFactory object
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); -
Get SqlSession object according to sqlSessionFactory
SqlSession openSession = sqlSessionFactory.openSession(); -
Gets the implementation class object of the interface
A proxy object will be automatically created for the interface, and the proxy object will execute the addition, deletion, modification and query methodsEmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
Employee employee = mapper.getEmpById(2);
@Test public void test02() throws IOException { // 1. Get sqlSessionFactory object SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); // 2. Get sqlSession object SqlSession openSession = sqlSessionFactory.openSession(); try { // 3. Gets the implementation class object of the interface //A proxy object will be automatically created for the interface, and the proxy object will execute the addition, deletion, modification and query methods EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); Employee employee = mapper.getEmpById(2); System.out.println(mapper.getClass()); System.out.println(employee); } finally { openSession.close(); } }