mybatis way to operate the database
Article Directory
- mybatis way to operate the database
- Before (a) spring and mybatis integration
- After (b) Integration
- spring and mybatis integrate traditional Dao Development == (this way also need to load mapper.xml file mybatis-config.xml in) == (need to write mappers)
- and spring mybatis use == MapperFactoryBean == integration, mappers may not be defined in mybatis-confi.xml
- and spring mybatis use == MapperScanConfigurer ==, in mybatis-config.xml mappers may not be defined in
Before (a) spring and mybatis integration
-
mybatis most basic usage (need to write mappers)
-
Get SqlSessionFactory
-
Use sqlSessionFactory get sqlSession
-
sql operation
-
sqlSession.selectOne()
-
sqlSession.selectList()
-
sqlSession.update()
-
sqlSession.delete()
-
-
Use mapper interfaces (need to write mappers)
-
Create a mapper Interface
-
Get SqlSessionFactory
-
Use sqlSessionFactory get sqlSession test
-
sqlSession.getMapper (UserMapper.class) generated interface implementation class
-
sql (using a method defined by the interface UserMapper)
-
userMapper.findUserById()
-
userMapper.listUser()
-
userMapper.update()
-
userMapper.delete()
-
After (b) Integration
-
-
spring and mybatis integrate traditional Dao Development == (this way also need to load mapper.xml file mybatis-config.xml in) == (need to write mappers)
-
Configuring SqlSessionFactoryBean in the spring It contains two properties
<bean id="sqlSessionFactory" class="com.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> </bean>
-
UserDao implement interfaces
class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{ public User findUserById(Integer id){ return this.getSqlSession().selectOne("com.tyut.mapper.findUserById",id); } }
-
UserDao configuration in the configuration file
<bean id="userDao" class="com.tyut.Dao.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean>
-
When testing by applicationContext.getBean == (UserDao.class) ==; Get userDao
-
Operating sql
- userDao.findUserById()
- userDao.listUser()
- userDao.update()
- userDao.delete()
-
-
spring and mybatis useFolders Factory BeanIntegration, in mybatis-confi.xml can not be defined mappers
-
The same meaning as defined above Step SqlSessionFactory
-
Only need to manually define the interfaces implemented UserMapper
-
In the spring using the configuration fileFolders Factory BeanAchieve UserMapper Interface
<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.tyut.mapper.UserMapper" /> <property name="sqlSessionFactory" ref="sqlSession"/> </bean>
-
When tested by == applicationContext.getBean == (UserMapper.class); Get userMapper
-
Operating sql
- userMapper.findUserById()
- userMapper.listUser()
- userMapper.update()
- userMapper.delete()
note
When multiple interfaces need to configure multiple MapperFactoryBean mapper in the spring, and do not need to scan the bottom
-
-
spring and mybatis useMapperScanConfigurer, May not be defined in the mappers mybatis-config.xml
-
The same meaning as defined above Step SqlSessionFactory
-
Only need to manually define the interfaces implemented UserMapper
-
In the spring using the configuration file
<bean class="org.mybatis.spring.mapper.MaperScanConfigurer"> <property name="basepackage" value="com.tyut.mapper" /> </bean>
Or use
<mybatis-spring:scan base-package="com.tyut.mapper" />
-
When testing by applicationContext.getBean (UserMapper.class); Get userMapper
-
Operating sql
- userMapper.findUserById()
- userMapper.listUser()
- userMapper.update()
- userMapper.delete()
-