今日学习之mybatis 整合Spring (bean 组件)
1.spring整合了mybatis-config.xml的配置
-
1.数据源
-
2.数据库连接信息
-
3.sqlSessionFactory工厂类
-
4.DAO的翻译代码业务
- [✔] 这一块接管了 数据源和数据库连接信息
原来配置 <datasource type="POOLED"/> //使用spring替换 //com.alibaba afr druid <bean id="dataSources" class="com.alibaba.druid.pool.druidDatasource" > //这开始配置数据库连接信息 //要设置的属性名为name 值为value <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/databaseName" <property name="username" value="root"/> <property name="password" value="hr"/> </bean>
- [✔] 这一块接管了工厂对象的创建
原来配置 通过Mybatis提供的Resources类加载mybatis-config.xml进行创建工厂对象 // 加载配置文件 InputStream inputStream = Resources.class.getResourceAsStream("/mybatis-config.xml"); sqlFactory = new SqlSessionFactoryBuilder().build(inputStream); 配置一个组件 <bean id="sqlSessionFactory" class="org.mybaits.spring.sqlSessionFactoryBean" > //mybatis-spring aft id //org,mybatis.spring.sqlSessionFactroy //org.mybaits.spring.sqlSessionFactory //org.mybaits.spring.sqlSessionFactory //注入 mybatis-config 其实就是连接源信息,阿里爸爸那个 1.ref 引用对象 bean 算是一个对象 <property name="dataSource" ref="dataSource"/> 2.注入map文件位置 原先map文件是配置在 mybaits-config.xml文件中 { <mappers> <!-- dao实现资源路径 --> <mapper resource="com/baizhi/dao/impl/adminDaoMapper.xml"></mapper> <mapper resource="com/baizhi/dao/impl/empDaoMapper.xml"></mapper> </mappers> } //spring 接管 p小写 <property name="mapperLocations" value="classpath:[maper文件夹路径]/*.xml" /> </bean>
- [✔]这一块接管了dao文件的翻译
原来配置 通过工厂返回的sqlSession进行翻译DAO 并返回实现类 openSession().getMapper(clazz); //spring //获取mapper需要 sqlsession 对象 dao所在包 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name="sqlSessionFactory" ref="sqlSessionFactory"/> .xx.dao"/> </bean>