mybatis way to operate the database

mybatis way to operate the database

Before (a) spring and mybatis integration

  • mybatis most basic usage (need to write mappers)

    1. Get SqlSessionFactory

    2. Use sqlSessionFactory get sqlSession

    3. sql operation

    • sqlSession.selectOne()

    • sqlSession.selectList()

    • sqlSession.update()

    • sqlSession.delete()

  • Use mapper interfaces (need to write mappers)

    1. Create a mapper Interface

    2. Get SqlSessionFactory

    3. Use sqlSessionFactory get sqlSession test

    4. sqlSession.getMapper (UserMapper.class) generated interface implementation class

    5. 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)

    1. 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>
      

    2. UserDao implement interfaces

      class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{
      
      	public User findUserById(Integer id){
      
      		return this.getSqlSession().selectOne("com.tyut.mapper.findUserById",id);
      
      	}
      
      }
      

    3. UserDao configuration in the configuration file

      <bean id="userDao" class="com.tyut.Dao.UserDaoImpl">
      
      	<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
      
      </bean>
      
    4. When testing by applicationContext.getBean == (UserDao.class) ==; Get userDao

    5. 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

    1. The same meaning as defined above Step SqlSessionFactory

    2. Only need to manually define the interfaces implemented UserMapper

    3. 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>	
      
    4. When tested by == applicationContext.getBean == (UserMapper.class); Get userMapper

    5. 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

    1. The same meaning as defined above Step SqlSessionFactory

    2. Only need to manually define the interfaces implemented UserMapper

    3. 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" />
      
    4. When testing by applicationContext.getBean (UserMapper.class); Get userMapper

    5. Operating sql

      • userMapper.findUserById()
      • userMapper.listUser()
      • userMapper.update()
      • userMapper.delete()
Published 43 original articles · won praise 13 · views 4914

Guess you like

Origin blog.csdn.net/qq_30693057/article/details/90443182