尚筹网项目学习第二天

尚筹网项目学习第二天

构建Spring 整合 MyBatis

创建 spring-persist-mybatis.xml

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<!--suppress ALL -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
			    http://www.springframework.org/schema/beans/spring-beans.xsd
			    http://www.springframework.org/schema/context
			    http://www.springframework.org/schema/context/spring-context.xsd
			    http://www.springframework.org/schema/aop
			    http://www.springframework.org/schema/tx/spring-tx.xsd
			    http://www.springframework.org/schema/mvc
			    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

Spring 具体配置:第一步 配置数据源(先创建jdbc.properties文件)

    <!-- 加载 jdbc.properties-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--配置连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="username" value="${jdbc.user}"/>
        <!-- 连接数据库的密码 -->
        <property name="password" value="${jdbc.password}"/>
        <!-- 目标数据库的 URL 地址 -->
        <property name="url" value="${jdbc.url}"/>
        <!-- 数据库驱动全类名 -->
        <property name="driverClassName" value="${jdbc.driver}"/>
    </bean>

创建 Spring 的 Junit 测试类 (出现import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;三个jar包无法导入的问题)

在这里插入图片描述

发现自己pom.xml 依赖也存在 但是External librarys类库下并没有导入jar包
解决方式
一、点击IDEA右侧maven 
二、点击项目 
三、点击Unlink Maven projects将maven工程变成普通工程,然后再将普通工程转回maven工程

在这里插入图片描述
一、点击我的项目, 右键,找到Add Framework Support,点击

在这里插入图片描述
二、弹出以下页面,若没有maven,一直下拉,若还没有,检查你的项目中是否已有pom.xml文件,勾选Maven,点击Ok
在这里插入图片描述
三、pom.xml成功创建(已转变成maven项目),可以添加你的依赖了

Spring 具体配置:第二步 配置 SqlSessionFactoryBean

    <!--配置SqlSessionFactoryBean整合Mybatis-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 装配数据源 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 指定 MyBatis 全局配置文件位置 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!-- 指定 Mapper 配置文件位置 -->
        <property name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml"/>
    </bean>

Spring 具体配置:第三步 配置 MapperScannerConfigurer

 <!-- 配置 MapperScannerConfigurer-->
    <!-- 把 MyBatis 创建的 Mapper 接口类型的代理对象扫描到 IOC 容器中 -->
    <bean id="mapperScannerConfigurer"
          class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 使用 basePackage 属性指定 Mapper 接口所在包 -->
        <property name="basePackage" value="com.atguigu.crowd.mapper"/>
    </bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值