一、导入依赖 (包含junit/spring-test测试)
<properties>
<mybatis.version>3.4.5</mybatis.version>
<mybatis-spring.version>1.3.1</mybatis-spring.version>
<mysql-connector-java.version>5.1.47</mysql-connector-java.version>
<druid.version>1.1.16</druid.version>
<spring-context.version>5.1.6.RELEASE</spring-context.version>
<spring-aspects.version>5.1.6.RELEASE</spring-aspects.version>
<spring-test.version>5.1.6.RELEASE</spring-test.version>
<spring-jdbc.version>5.1.6.RELEASE</spring-jdbc.version>
<junit.version>4.12</junit.version>
</properties>
<dependencies>
<!-- mybatis、-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- mysql、-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
<scope>runtime</scope>
</dependency>
<!-- druid、-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- mybatis-spring、-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<!-- spring-context、-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-context.version}</version>
</dependency>
<!-- spring-aspects、-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring-aspects.version}</version>
</dependency>
<!-- junit、-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<!-- spring-test-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-test.version}</version>
</dependency>
<!-- spring-jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-jdbc.version}</version>
</dependency>
</dependencies>
二、配置applicationContext.xml、这里没有配置mybatis-config,如果要用到具体功能,还是要配置
<context:component-scan base-package="com.wsj.mapper,com.wsj.service"/>
<aop:aspectj-autoproxy/>
<context:property-placeholder location="classpath:db.properties"/>
<!--配置dataSource druid连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置SqlSessionFactoryBean-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源-->
<property name="dataSource" ref="dataSource"/>
<!-- 注入扫描映射文件的路径 resources下的mapper-->
<property name="mapperLocations" value="classpath:com/wsj/mapper/*.xml"/>
<!-- 配置类别名文件 可以减少书写-->
<property name="typeAliasesPackage" value="com.wsj.entity"/>
</bean>
<!--配置mapper接口 java下的mapper-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wsj.mapper"/>
</bean>
|