尚筹网项目学习第二天
构建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>