目录
一、准备工作
准备工作:
创建Maven项目
如下操作重新创建项目来测试mybatis 环境 效果图如
如下目录
二、ssm整合
【步骤:】
1.添加所有pom依赖
<?xml version="1.0" encoding="UTF-8"?> <!-- $Id: pom.xml 642118 2008-03-28 08:04:16Z reinhard $ --> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <packaging>war</packaging> <name>ssm</name> <groupId>org.example</groupId> <artifactId>ssm</artifactId> <version>1.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.plugin.version>3.7.0</maven.compiler.plugin.version> <!--添加jar包依赖--> <!--1.spring 5.0.2.RELEASE相关--> <spring.version>5.0.2.RELEASE</spring.version> <!--2.mybatis相关--> <mybatis.version>3.4.5</mybatis.version> <!--mysql--> <mysql.version>5.1.44</mysql.version> <!--pagehelper分页jar依赖--> <pagehelper.version>5.1.2</pagehelper.version> <!--mybatis与spring集成jar依赖--> <mybatis.spring.version>1.3.1</mybatis.spring.version> <!--3.dbcp2连接池相关 druid--> <commons.dbcp2.version>2.1.1</commons.dbcp2.version> <commons.pool2.version>2.4.3</commons.pool2.version> <!--4.log日志相关--> <log4j2.version>2.9.1</log4j2.version> <!--5.其他--> <junit.version>4.12</junit.version> <servlet.version>4.0.0</servlet.version> <lombok.version>1.18.2</lombok.version> <kotlin.version>1.0.0</kotlin.version> </properties> <build> <finalName>ssm</finalName> <plugins> <plugin> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-plugin</artifactId> <version>${kotlin.version}</version> <executions> <execution> <id>compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>test-compile</id> <phase>test-compile</phase> <goals> <goal>test-compile</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <executions> <execution> <id>compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>testCompile</id> <phase>test-compile</phase> <goals> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> </plugins> <resources> <!--解决mybatis-generator-maven-plugin运行时没有将XxxMapper.xml文件放入target文件夹的问题--> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <!--解决mybatis-generator-maven-plugin运行时没有将jdbc.properites文件放入target文件夹的问题--> <resource> <directory>src/main/resources</directory> <includes> <include>jdbc.properties</include> <include>*.xml</include> </includes> </resource> </resources> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven.compiler.plugin.version}</version> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <dependencies> <!--使用Mybatis-generator插件不能使用太高版本的mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> </dependencies> <configuration> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </pluginManagement> </build> <dependencies> <!--1.spring相关--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!--2.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.version}</version> </dependency> <!--pagehelper分页插件jar包依赖--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper.version}</version> </dependency> <!--mybatis与spring集成jar包依赖--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <!--3.dbcp2连接池相关--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>${commons.dbcp2.version}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>${commons.pool2.version}</version> </dependency> <!--4.log日志相关依赖--> <!--核心log4j2jar包--> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>${log4j2.version}</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>${log4j2.version}</version> </dependency> <!--web工程需要包含log4j-web,非web工程不需要--> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-web</artifactId> <version>${log4j2.version}</version> </dependency> <!--5.其他--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib</artifactId> <version>${kotlin.version}</version> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-test</artifactId> <version>${kotlin.version}</version> <scope>test</scope> </dependency> </dependencies> </project>
2.添加并配置整合得配置文件
Spring :spring--context.xml
Mybatis: mybatis.cfg.xml
-->spring-mybatis.xml
2.1扫描注解驱动
添加相关xml配置文件:
javabena交给Spring进行管理
@service --->在实现类上方添加注解
package com.zking.oa.biz;
import com.zking.oa.mapper.BookMapper;
import com.zking.oa.model.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
/**
* @author 锦鲤
* @site www.lucy.com
* @company xxx公司
* @create 2022-08-13 19:21
*/
@Service
public class BookBizImpl implements BookBiz{
@Autowired
// private BookBiz bookbiz;
private BookMapper bookmapper;
public BookMapper getBookmapper() {
return bookmapper;
}
public void setBookmapper(BookMapper bookmapper) {
this.bookmapper = bookmapper;
}
@Override
public int deleteByPrimaryKey(Integer bid) {
return bookmapper.deleteByPrimaryKey(4);
}
//
//
@Override
public Book selectByPrimaryKey(Integer bid) {
return bookmapper.selectByPrimaryKey(44);
}
@Override
public List<Book> selectByIn(List bookIds) {
return bookmapper.selectByIn(bookIds);
}
}
@autowised 自动配置 在属性上方添加
import com.zking.oa.biz.BookBiz;
import com.zking.oa.biz.BookBizImpl;
import com.zking.oa.mapper.BookMapper;
import com.zking.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.List;
/**
* @author 锦鲤
* @site www.lucy.com
* @company xxx公司
* @create 2022-08-10 21:57
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-mybatis.xml"})
public class BookBizImplTest {
@Autowired
private BookBiz bookbiz;
// private SqlSession sqlSession;
@Before
public void setUp() throws Exception {
System.out.println("方法初始化");
// BookBizImpl bookbiz=new BookBizImpl();
// //工具类获取session对象
// sqlSession = SessionUtil.openSession();
// //从session里获取mapper
// BookMapper mapper1= sqlSession.getMapper(BookMapper.class);
// bookbiz.setBookmapper(mapper1);
// this.bookbiz=bookbiz;
}
@After
public void tearDown() throws Exception {
System.out.println("方法测试结束");
}
@Test
public void deleteByPrimaryKey() {
System.out.println(bookbiz.deleteByPrimaryKey(40));
// sqlSession.commit();//提交事务
// sqlSession.close();//关闭
}
@Test
public void selectByPrimaryKey() {
System.out.println("测试的业务方法");
System.out.println(bookbiz.selectByPrimaryKey(44));
}
@Test
public void test3() {
int[] ints={1,2,3,4};
StringBuffer sb=new StringBuffer();
for (int i:ints){
sb.append(i).append(",");
}
System.out.println(sb.toString().substring(1));
}
@Test
public void getBookmapper() {
}
@Test
public void setBookmapper() {
}
@Test
public void selectByIn() {
List<Integer> bookids=Arrays.asList(new Integer[]{31,32,33,34});
bookbiz.selectByIn(bookids).forEach(System.out::println);
}
}
@repository标记接口被spring所接管
2.2 数据源管理并添加(管理sqlSession)
2.3 扫描所有mapper接口
2.4添加事务管理
- 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.zking.oa"/> <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/zking/oa/**/mapper/*.xml"/> <!-- 指定别名 --> <property name="typeAliasesPackage" value="com/javaxl/ssm/**/model"/> <!--配置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/javaxl/ssm/**/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>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" > <generatorConfiguration> <!-- 引入配置文件 --> <properties resource="jdbc.properties"/> <!--指定数据库jdbc驱动jar包的位置--> <classPathEntry location="D:\\initPath\\mvn_repository\\mysql\\mysql-connector-java\\5.1.44\\mysql-connector-java-5.1.44.jar"/> <!-- 一个数据库一个context --> <context id="infoGuardian"> <!-- 注释 --> <commentGenerator> <property name="suppressAllComments" value="true"/><!-- 是否取消注释 --> <property name="suppressDate" value="true"/> <!-- 是否生成注释代时间戳 --> </commentGenerator> <!-- jdbc连接 --> <jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"/> <!-- 类型转换 --> <javaTypeResolver> <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) --> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 01 指定javaBean生成的位置 --> <!-- targetPackage:指定生成的model生成所在的包名 --> <!-- targetProject:指定在该项目下所在的路径 --> <javaModelGenerator targetPackage="com.zking.model" targetProject="src/main/java"> <!-- 是否允许子包,即targetPackage.schemaName.tableName --> <property name="enableSubPackages" value="false"/> <!-- 是否对model添加构造函数 --> <property name="constructorBased" value="true"/> <!-- 是否针对string类型的字段在set的时候进行trim调用 --> <property name="trimStrings" value="false"/> <!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 --> <property name="immutable" value="false"/> </javaModelGenerator> <!-- 02 指定sql映射文件生成的位置 --> <sqlMapGenerator targetPackage="com.zking.mapper" targetProject="src/main/java"> <!-- 是否允许子包,即targetPackage.schemaName.tableName --> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- 03 生成XxxMapper接口 --> <!-- type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象 --> <!-- type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象 --> <!-- type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 --> <javaClientGenerator targetPackage="com.zking.mapper" targetProject="src/main/java" type="XMLMAPPER"> <!-- 是否在当前路径下新加一层schema,false路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!-- 配置表信息 --> <!-- schema即为数据库名 --> <!-- tableName为对应的数据库表 --> <!-- domainObjectName是要生成的实体类 --> <!-- enable*ByExample是否生成 example类 --> <!--<table schema="" tableName="t_book" domainObjectName="Book"--> <!--enableCountByExample="false" enableDeleteByExample="false"--> <!--enableSelectByExample="false" enableUpdateByExample="false">--> <!--<!– 忽略列,不生成bean 字段 –>--> <!--<!– <ignoreColumn column="FRED" /> –>--> <!--<!– 指定列的java数据类型 –>--> <!--<!– <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> –>--> <!--</table>--> <table schema="" tableName="t_mvc_book" domainObjectName="Book" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> <!-- 忽略列,不生成bean 字段 --> <!-- <ignoreColumn column="FRED" /> --> <!-- 指定列的java数据类型 --> <!-- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> --> </table> </context> </generatorConfiguration>
三、aopz整合分页插件使用
添加分页工具类
package com.zking.pagination.entity;
import java.io.Serializable;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
public class PageBean implements Serializable {
private static final long serialVersionUID = 2422581023658455731L;
//页码
private int page=1;
//每页显示记录数
private int rows=10;
//总记录数
private int total=0;
//是否分页
private boolean isPagination=true;
//上一次的请求路径
private String url;
//获取所有的请求参数
private Map<String,String[]> map;
public PageBean() {
super();
}
//设置请求参数
public void setRequest(HttpServletRequest req) {
String page=req.getParameter("page");
String rows=req.getParameter("rows");
String pagination=req.getParameter("pagination");
this.setPage(page);
this.setRows(rows);
this.setPagination(pagination);
this.url=req.getContextPath()+req.getServletPath();
this.map=req.getParameterMap();
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Map<String, String[]> getMap() {
return map;
}
public void setMap(Map<String, String[]> map) {
this.map = map;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public void setPage(String page) {
if(null!=page&&!"".equals(page.trim()))
this.page = Integer.parseInt(page);
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
public void setRows(String rows) {
if(null!=rows&&!"".equals(rows.trim()))
this.rows = Integer.parseInt(rows);
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public void setTotal(String total) {
this.total = Integer.parseInt(total);
}
public boolean isPagination() {
return isPagination;
}
public void setPagination(boolean isPagination) {
this.isPagination = isPagination;
}
public void setPagination(String isPagination) {
if(null!=isPagination&&!"".equals(isPagination.trim()))
this.isPagination = Boolean.parseBoolean(isPagination);
}
/**
* 获取分页起始标记位置
* @return
*/
public int getStartIndex() {
//(当前页码-1)*显示记录数
return (this.getPage()-1)*this.rows;
}
/**
* 末页
* @return
*/
public int getMaxPage() {
int totalpage=this.total/this.rows;
if(this.total%this.rows!=0)
totalpage++;
return totalpage;
}
/**
* 下一页
* @return
*/
public int getNextPage() {
int nextPage=this.page+1;
if(this.page>=this.getMaxPage())
nextPage=this.getMaxPage();
return nextPage;
}
/**
* 上一页
* @return
*/
public int getPreivousPage() {
int previousPage=this.page-1;
if(previousPage<1)
previousPage=1;
return previousPage;
}
@Override
public String toString() {
return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", isPagination=" + isPagination
+ "]";
}
}
package com.zking.oa.aspect; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; /*** * * 返回值 * * ..无限包 * *Service 以service结尾的接口 如biz * *Pager 以pager方法 * 以上配置要生效,代理注解不能少<aop"aspecthj-autoproxy/>不能少 * @author 锦鲤 * @site www.lucy.com * @company xxx公司 * @create 2022-08-14 10:48 */ @Component @Aspect public class PageAspect { @Around("execution(* *..*Biz.*Pager(..))") public Object invoke(ProceedingJoinPoint args) throws Throwable { Object[] params = args.getArgs(); PageBean pageBean = null; for (Object param : params) { if(param instanceof PageBean){ pageBean = (PageBean)param; break; } } if(pageBean != null && pageBean.isPagination()) PageHelper.startPage(pageBean.getPage(),pageBean.getRows()); Object list = args.proceed(params); if(null != pageBean && pageBean.isPagination()){ PageInfo pageInfo = new PageInfo((List) list); pageBean.setTotal(pageInfo.getTotal()+""); } return list; } }
实现类
package com.zking.oa.biz;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zking.oa.mapper.BookMapper;
import com.zking.oa.model.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
import java.util.Map;
/**
* @author 锦鲤
* @site www.lucy.com
* @company xxx公司
* @create 2022-08-13 19:21
*/
@Service
public class BookBizImpl implements BookBiz{
@Autowired
// private BookBiz bookbiz;
private BookMapper bookmapper;
public BookMapper getBookmapper() {
return bookmapper;
}
public void setBookmapper(BookMapper bookmapper) {
this.bookmapper = bookmapper;
}
@Override
public int deleteByPrimaryKey(Integer bid) {
return bookmapper.deleteByPrimaryKey(4);
}
//
//
@Override
public Book selectByPrimaryKey(Integer bid) {
return bookmapper.selectByPrimaryKey(44);
}
@Override
public List<Book> selectByIn(List bookIds) {
return bookmapper.selectByIn(bookIds);
}
//
// public List<Map> listPager(Map map, PageBean pageBean) {
// pageHelper分页插件相关代码
if(pageBean != null && pageBean.isPagination()){
PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
}
List<Map> maps=bookMapper.listPager(map);
if(pageBean != null && pageBean.isPagination()){
// 处理查询结果的前提,是需要分页的
PageInfo info=new PageInfo(maps);
pageBean.setTotal(info.getTotal()+"");
}
return maps;
}
//分页
public List<Map> listPager(Map map, PageBean pageBean) {
}
return bookmapper.listPager(map);
}
进行测试
import com.zking.oa.biz.BookBiz;
import com.zking.oa.biz.BookBizImpl;
import com.zking.oa.mapper.BookMapper;
import com.zking.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;
/**
* @author 锦鲤
* @site www.lucy.com
* @company xxx公司
* @create 2022-08-10 21:57
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-mybatis.xml"})
public class BookBizImplTest {
@Autowired
private BookBiz bookbiz;
// private SqlSession sqlSession;
@Before
public void setUp() throws Exception {
System.out.println("方法初始化");
// BookBizImpl bookbiz=new BookBizImpl();
// //工具类获取session对象
// sqlSession = SessionUtil.openSession();
// //从session里获取mapper
// BookMapper mapper1= sqlSession.getMapper(BookMapper.class);
// bookbiz.setBookmapper(mapper1);
// this.bookbiz=bookbiz;
}
@After
public void tearDown() throws Exception {
System.out.println("方法测试结束");
}
@Test
public void deleteByPrimaryKey() {
System.out.println(bookbiz.deleteByPrimaryKey(40));
// sqlSession.commit();//提交事务
// sqlSession.close();//关闭
}
@Test
public void selectByPrimaryKey() {
System.out.println("测试的业务方法");
System.out.println(bookbiz.selectByPrimaryKey(44));
}
@Test
public void test3() {
int[] ints={1,2,3,4};
StringBuffer sb=new StringBuffer();
for (int i:ints){
sb.append(i).append(",");
}
System.out.println(sb.toString().substring(1));
}
@Test
public void getBookmapper() {
}
@Test
public void setBookmapper() {
}
@Test
public void selectByIn() {
List<Integer> bookids=Arrays.asList(new Integer[]{31,32,33,34});
bookbiz.selectByIn(bookids).forEach(System.out::println);
}
@Test
public void testlistPager() {
Map map=new HashMap();
map.put("bname","圣墟");
PageBean pageBean=new PageBean();
pageBean.setPage(2);
pageBean.setRows(20);
bookbiz.listPager(map,pageBean).forEach(System.out::println);
}
}