温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring整合Mybatis

发布时间:2020-08-02 10:39:32 来源:网络 阅读:2521 作者:QuiteQuiet 栏目:编程语言

Spring & Mybatis

spring与Mybatis整合
核心思想:将SqlSessionFactory和SqlSession交给spring来管理

步骤

  1. 创建包 ,创建表,实体类
  2. 导入jar包
    Spring整合Mybatis

  3. 编辑配置文件

    • spring配置文件

      • 引入外部数据源,数据库相关配置放在config.properties中
        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        一般出现location都要使用classpath
        <property name="location" value="classpath:config.properties"/>
        </bean>
      • 使用连接池配置数据源,连接池用dbcp或者c3p0,不能同时配置,否则冲突
        在mybatis配置文件中不用再配置数据源相关信息
        使用的dbcp连接池:
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        value的值是外部数据文件的key
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
        </bean>

        或者使用c3p0 连接池
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driver}"/>
        <property name="jdbcUrl" value="${url}"/>
        <property name="user" value="${username}"/>
        <property name="password" value="${password}"/>
        </bean>

      • 配置SqlSessionFactory,用于创建sqlSession,使用整合包的类SqlSessionFactoryBean,用于使spring能够提供SqlSessionFactory对象,需要指定配置文件路径以及扫描mapper.xml映射文件
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        引入mybatis的配置文件
        <property name="configLocation" value="classpath:mybatisConfigure.xml"/>
        扫描mapper.xml映射
        <property name="mapperLocations" value="classpath:com/sky/mapperxml/*.xml"/>
        </bean>
      • 配置SqlSession,使用整合包的SqlSessionTemplate对象,使spring能够提供sqlsession对象
        <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        由于SqlSessionTemplate中的sqlSessionFactory属性没有set方法,不能使用set注入依赖,有将SqlSessionFactory作为参数的构造方法,所以使用构造器注入
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
        </bean>
    • mybatis配置文件
      数据源与mapper.xml映射文件扫描已经在spring配置文件中,mybatis配置文件只需要进行一些setting设置和别名设置等.
      <settings>
      <setting name="mapUnderscoreToCamelCase" value="true"/>
      <setting name="logImpl" value="STDOUT_LOGGING"/>
      <setting name="cacheEnabled" value="true"/>
      </settings>
      <typeAliases>
      <!-- type属性表示的是全路径 ,alias属性表示别名-->
      <!-- <typeAlias type="com.homework.pojo.User" alias="User"/>-->
      <package name="com.sky.pojo"/>
      </typeAliases>
  4. 编写mapper接口,并在spring配置文件添加mapperFactoryBean进行管理

    • 编写controller层的mapper接口
      public interface UserMapper {
      User queryUserById(@Param("id") Integer id);
      }
    • 配置spring文件,将mapper接×××给spring管理
      使用整合包的MapperFactoryBean类,mapperInterface属性值为接口路径,需要注入SqlSessionFactory依赖
      <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
      <property name="mapperInterface" value="com.sky.mapper.UserMapper"/>
      <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
      </bean>
      使用Mybatis时使用SqlSessionFactory创建SqlSession对象,通过sqlsession对象的getMapper()方法指定接口名获得mapper接口,所以这里要指定接口路径并依赖SqlSessionFactory.
  5. 创建service类,注入mapper依赖,并在spring配置文件中将其交由spring管理

    • service类:
      public class UserService {@Autowired
      br/>@Autowired
      public User queryUserById(Integer id){
      User user = userMapper.queryUserById(id);
      return user;
      }
      }

    • spring配置:
      <bean id="userService" class="com.sky.service.UserService"/>
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI