1. 程式人生 > >mybatis-spring整合過程

mybatis-spring整合過程

  • 包含 mybatis-spring-x.x.x.jar 文 件
  • Spring XML配置檔案 配置dataSource
<!-- 1. 資料來源 : DriverManagerDataSource -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property
name="url" value="jdbc:mysql://localhost:3306/mybatis" />
<property name="username" value="root" /> <property name="password" value="123" /> </bean>

配置sqlSessionFactory

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property
name="dataSource" ref="dataSource" /> </bean>

配置MapperFactoryBean

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
  <property name="mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper" />
  <property name="sqlSessionFactory" ref="sqlSessionFactory"
/> </bean>

上述可以升級成配置MapperScannerConfigurer

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.neusoft.mapper"></property>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    <!--    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>-->
</bean>

配置事務管理器

<bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
</bean>

使用宣告式事務的註解方式,之後Service層使用@Transactional註解方式

<tx:annotation-driven transaction-manager="txManager" />

注意: 只有mybatis時,要在mybatis的配置檔案中<mapper>指定mapper.xml。 Spring-Mybatis時,如果mapper.java和mapper.xml不在同一包下,要麼在mybatis的配置檔案中<mapper>指定,要麼在<bean id="sqlSessionFactory">的mapperLocations屬性中配置; 如果mapper.java和mapper.xml在同一包下,它會被MapperFactoryBean(如果升級成MapperScannerConfigurer一樣的)自動解析。

  • Mybatis的配置檔案相應的配置部分被Spring代替了,就不用再寫了,只剩下一些簡單的
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!-- 實體類,簡稱 -設定別名 -->
    <typeAliases>
        <!--  <typeAlias alias="User" type="com.neusoft.model.User" />-->
        <!--  <package name="com.neusoft.model"/>-->
        <typeAlias alias="Userinfo" type="cn.neusoft.pojo.Userinfo" />
    </typeAliases>
    <!-- 實體介面對映資源 -->
    <!--
        說明:如果xxMapper.xml配置檔案放在和xxMapper.java統一目錄下,mappers也可以省略,因為org.mybatis.spring.mapper.MapperFactoryBean預設會去查詢與xxMapper.java相同目錄和名稱的xxMapper.xml(Spring 有的)
    -->
    <!--    
     <mappers>
        <mapper resource="com/neusoft/mapper/userMapper.xml" />
        <mapper resource="com/neusoft/mapper/TUserMapper.xml" />
        <mapper resource="com/neusoft/mapper/TblAddressMapper.xml" />
        <mapper resource="com/neusoft/mapper/TblUserinfoMapper.xml" />
    </mappers>
    -->
</configuration>  
  • 然後是在Service層 當使用MapperFactoryBean時: Spring XML配置檔案中
<bean id="fooService" class="org.mybatis.spring.sample.mapper.FooServiceImpl">
  <property name="userMapper" ref="userMapper" />
</bean>

Service層使用mapper程式碼(使用Sevice bean的屬性mapper 或者@Autowired)

public class FooServiceImpl implements FooService {

  private UserMapper userMapper;

  public void setUserMapper(UserMapper userMapper) {
    this.userMapper = userMapper;
  }

  public User doSomeBusinessStuff(String userId) {
    return this.userMapper.getUser(userId);
  }
}

當使用MapperScannerConfigurer時:應該Service層@Autowired註解使用mapper.

@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUser(User user) {
        return userMapper.getUser(user);
    }
}
  • 當執行的操作要提交事務時(不是每種操作都要提交事務的,但是寫肯定沒錯):
@Service
@Transactional                    
public class UserinfoServiceImpl implements UserinfoService {

    @Resource       
    private UserinfoMapper userinfoMapper;//把Mapper資源引入

    public List<Userinfo> findAllUsers() {
        // 查詢所有使用者資訊
        List<Userinfo> users = userinfoMapper.listAll();
        return users;
    }

}