目录
1.1 添加spring相关依赖(5.0.2.RELEASE)
第三步:编写配置文件applicationContext-mybatis.xml
第二步:利用mybatis逆向工程生成模型层层代码
第三步:编写配置文件applicationContext-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<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:aop="http://www.springframework.org/schema/aop"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--1. 注解式开发 -->
<!-- 注解驱动 -->
<context:annotation-config/>
<!-- 用注解方式注入bean,并指定查找范围:com.javaxl.ssm及子子孙孙包-->
<context:component-scan base-package="com.lj"/>
<!-- <context:property-placeholder location="classpath:jdbc.properties"/>-->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!--初始连接数-->
<property name="initialSize" value="10"/>
<!--最大活动连接数-->
<property name="maxTotal" value="100"/>
<!--最大空闲连接数-->
<property name="maxIdle" value="50"/>
<!--最小空闲连接数-->
<property name="minIdle" value="10"/>
<!--设置为-1时,如果没有可用连接,连接池会一直无限期等待,直到获取到连接为止。-->
<!--如果设置为N(毫秒),则连接池会等待N毫秒,等待不到,则抛出异常-->
<property name="maxWaitMillis" value="-1"/>
</bean>
<!--4. spring和MyBatis整合 -->
<!--1) 创建sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 自动扫描XxxMapping.xml文件,**任意路径 -->
<property name="mapperLocations" value="classpath*:com/lj/**/mapper/*.xml"/>
<!-- 指定别名 -->
<!-- <property name="typeAliasesPackage" value="com/lj/**/model"/>-->
<!--设置mybaits对缓存的支持-->
<property name="configurationProperties">
<props>
<!-- 全局映射器启用缓存 *主要将此属性设置完成即可-->
<prop key="cacheEnabled">true</prop>
<!-- 查询时,关闭关联对象即时加载以提高性能 -->
<prop key="lazyLoadingEnabled">false</prop>
<!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指 定),不会加载关联表的所有字段,以提高性能 -->
<prop key="aggressiveLazyLoading">true</prop>
</props>
</property>
<!--配置pagehelper插件-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
helperDialect=mysql
</value>
</property>
</bean>
</array>
</property>
</bean>
<!--2) 自动扫描com/javaxl/ssm/**/mapper下的所有XxxMapper接口(其实就是DAO接口),并实现这些接口,-->
<!-- 即可直接在程序中使用dao接口,不用再获取sqlsession对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--basePackage 属性是映射器接口文件的包路径。-->
<!--你可以使用分号或逗号 作为分隔符设置多于一个的包路径-->
<property name="basePackage" value="com/lj/**/mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<aop:aspectj-autoproxy/>
</beans>
第四步:编写测试(Spring Test+junit整合)
1.新建一个java类并且加入注解
@Autowired:是可以让spring完成自动装配的用途
@RunWith(SpringJUnit4ClassRunner.class):用于指定junit运行环境,是junit提供给其他框架测试环境接口扩展,为了便于使用spring的依赖注入
@ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"}):可以用classes来直接导入同包下写的配置类。或者导入配置文件。
2.测试类
package impl;
import com.lj.mapper.BookMapper;
import com.lj.model.Book;
import com.lj.model.BookVo;
import com.lj.service.BookService;
import com.lj.service.impl.BookServiceImpl;
import com.lj.util.PageBean;
import com.lj.util.SessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class BookServiceImplTest2 {
@Autowired
private BookService bookService;
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void deleteByPrimaryKey() {
System.out.println("业务方法删除。。。。");
this.bookService.deleteByPrimaryKey(65);
}
@Test
public void selectBooksIn() {
System.out.println("mybatis中的foreach标签。。。。");
List<Book> books = this.bookService.selectBooksIn(Arrays.asList(new Integer[]{66, 67, 68}));
books.forEach(System.out::println);
}
//模糊查询
@Test
public void selectBooksLike1() {
System.out.println("mybatis中模糊查询#与$的区别。。。。#");
List<Book> books = this.bookService.selectBooksLike1("%圣墟%");
books.forEach(System.out::println);
}
@Test
public void selectBooksLike2() {
System.out.println("mybatis中模糊查询#与$的区别。。。。$");
List<Book> books = this.bookService.selectBooksLike2("%圣墟%");
books.forEach(System.out::println);
}
@Test
public void ehcacheMany() {
List<Book> books = this.bookService.selectBooksLike1("%圣墟%");
books.forEach(System.out::println);
books = this.bookService.selectBooksLike1("%圣墟%");
books.forEach(System.out::println);
}
}