Mybatis整合spring

整合步骤

1. 创建工程

2. 导入jar包(引入依赖)

3. 加入配置文件

  • spring的配置文件 -- applicationContext.xml
    • 加载数据库配置的属性文件,并配置数据库连接池(数据源)
    • 配置sqlSessionFactory<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      • 注入mybatis核心配置文件<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
      • 注入数据源<property name="dataSource" ref="dataSource"/>
  • mybatis的配置文件 -- sqlMapConfig.xml
    • 设置别名(可选)<typeAliases>
    • 配置映射<mapper>
  • 导入log4j.properties

Dao的开发

两种实现方式:

1. 原始dao的开发方式

2. 使用Mapper代理形式的开发方式

  1. 直接配置Mapper代理
  2. 使用包扫描配置Mapper代理

原始dao的开发方式

接口+实现类来完成,需要dao实现类继承SqlSessionDaoSupport类

步骤:

  1. 创建实体类,提供get,set方法
  2. 创建映射文件
  3. 在sqlMapConfig.xml中加载映射文件
  4. 创建接口
  5. 创建dao继承SqlSessionDaoSupport,并实现接口
  6. 在applicationContext中配置dao
    1. 注入sqlSessionFactory
      <bean id="userDao" class="com.jxust.ssm.dao.impl.UserDaoImpl">
          <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
      </bean>

       

  7. 在dao中获取sqlSession,使用sqlSession执行操作(dao将会交给spring管理,不要手动关闭sqlSession,不用手动提交事务)
    public class UserDaoImpl extends SqlSessionDaoSupport implments UserDao{
        public User queryUserById(int id){
            //获取sqlSession
            SqlSession sqlSession=super.getSqlSession();
    
            //使用SqlSession执行操作
            User user=sqlSession.selectOne("queryUserById",id);
    
            //不要关闭sqlSession
    
            return user;
        }
    }

Mapper代理形式开发dao

步骤:

  1. 创建实体类,提供get,set方法
  2. 创建映射文件
    1. namespace最好是对应接口的全路径
  3. 创建接口
    1. 接口与对应的映射文件应在同一个目录下
    2. 接口方法名与映射文件中sql的id相同
    3. 接口方法的参数与映射文件中配置的入参相同
    4. 接口方法的返回值与映射文件配置的返回值相同
  4. 在applicationContext中配置mapper
    1. 方式一:配置mapper代理
      1. <!--Mapper代理的开发方式一,配置mapper代理对象-->
        	<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        
        		<property name="mapperInterface" value="com.jxust.ssm.mapper.UserMapper"/>
        		<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        	</bean>

         

    2. 方式二:包扫描形式配置mapper
      1. <!--Mapper代理的开发方式二,包扫描形式配置mapper-->
        	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        		<!--配置mapper接口-->
        		<property name="basePackage" value="com.jxust.ssm.mapper"/>
        	</bean>

         

  5. 在service或测试方法中使用mapper
    1.  @Test
          public void test(){
              UserMapper userMapper=applicationContext.getBean(UserMapper.class);
              User user = userMapper.queryUserById(33);
              System.out.println(user);
          }

       

 事务管理:

DataSourceTransactionManager

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 

   <property name="dataSource" ref="dataSource"/>  

</bean>
 

使用声明式事务:

<tx:annotation-driven transaction-manager="transactionManager"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值