目录
1.添加相关依赖
2.添加并配置整合的配置文件
spring:spring-context.xml
mybatis:mybatis.cfg.xml
spring-mybatis.xml
(1)扫描注解驱动
(2)添加并管理数据源——>管理sqlsession
(3)扫描所有的Mapper接口
(4)添加事务管理器
编写配置文件applicationContext-mybatis.xml:
"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"> <context:annotation-config/> <context:component-scan base-package="com.zking.ssm"/> <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"/> <property name="maxWaitMillis" value="-1"/> bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath*:com/zking/**/*.xml"/> <property name="typeAliasesPackage" value="com/zking/**/model"/> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <value> helperDialect=mysql value> property> bean> array> property> bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com/**/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>要注意更改路径:
javabean交给Spring进行管理?
@service
在BookBizImpl上方添加@service注解
@autowised
在属性上方添加@autowised
@Respository
标记Mapper接口被Spring所接管
测试:
- @Test
- public void selectByPrimaryKey() {
- System.out.println("测试的业务方法...");
- // System.out.println(bookBiz.getBookMapper());
- System.out.println(bookBiz.selectByPrimaryKey(43));
-
-
- }
运行效果:
ssm整合成功!
注意:记得开动态代理
解决的问题:
利用pageHelper分页,pageHelper.start与后续结果处理即PageInfo处理代码是完全重复的
根据Spring aop所学,共性的非核心的业务代码利用通知来解决,核心业务代码前后都需要添加共性的非核心业务代码,利用环绕通知来解决。
分析:
1、找到目标对象——>*Biz.*Pager
2、写通知
2.1、目标对象该方法一定携带了PageBean对象 Instance of
2.2、执行
if(pageBean != null && pageBean.isPagination()){
PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
}代码
2.3、执行目标方法
2.4、 if(pageBean != null && pageBean.isPagination()){
// 处理查询结果的前提,是需要分页的
PageInfo info=new PageInfo(maps);
pageBean.setTotal(info.getTotal()+"");
}
@aspect——>代表了通知/切面
@component——>交给Spring进行管理
@Around 代表环绕通知
- package com.zking.ssm;
-
- import com.model.Book;
- import com.model.BookVo;
- import com.zking.mapper.BookMapper;
- import com.zking.mapper.biz.BookBiz;
- import com.zking.mapper.util.PageBean;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import java.util.List;
- import java.util.Map;
-
- /**
- * @author 杨总
- * @create 2022-08-10 23:07
- */
- @Service
- public class BookBizImpl implements BookBiz {
- @Autowired
- private BookMapper bookMapper;
-
- //alt+insert 快速提供set/get/toString/构造方法
- //alt+enter 快速构建实现类,填充代码的前半部分 Ctrl+1
- // public BookMapper getBookMapper() {
- // return bookMapper;
- // }
- //
- // public void setBookMapper(BookMapper bookMapper) {
- // this.bookMapper = bookMapper;
- // }
-
- @Override
- public int deleteByPrimaryKey(Integer bid) {
- return bookMapper.deleteByPrimaryKey(bid);
- }
-
- @Override
- public Book selectByPrimaryKey(Integer bid) {
- return bookMapper.selectByPrimaryKey(bid);
- }
-
- @Override
- public List<Book> selectByIn(List bookIds) {
- return bookMapper.selectByIn(bookIds);
- }
-
- @Override
- public List<Map> listPager(Map map, PageBean pageBean){
- return bookMapper.listPager(map);
- }
- // public List
- // pageHelper分页插件相关代码
- // if(pageBean != null && pageBean.isPagination()){
- // PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
- // }
- //
- // List
- //
- // if(pageBean != null && pageBean.isPagination()){
- 处理查询结果的前提,是需要分页的
- // PageInfo info=new PageInfo(maps);
- // pageBean.setTotal(info.getTotal()+"");
- // }
- //
- // return maps;
- // }
-
- @Override
- public List<Book> list6(BookVo vo) {
- return bookMapper.list6(vo);
- }
-
- @Override
- public List<Book> list7(BookVo vo) {
- return bookMapper.list7(vo);
- }
-
- @Override
- public List<Book> selectBooksLike1(String bname) {
- return bookMapper.selectBooksLike1(bname);
- }
-
- @Override
- public List<Book> selectBooksLike2(String bname) {
- return bookMapper.selectBooksLike2(bname);
- }
-
- @Override
- public List<Book> selectBooksLike3(String bname) {
- return bookMapper.selectBooksLike3(bname);
- }
-
- @Override
- public List<Book> list1() {
-
- return bookMapper.list1();
- }
-
-
-
- @Override
- public List<Book> list2() {
- return bookMapper.list2();
- }
-
- @Override
- public List<Book> list3(BookVo vo) {
- return bookMapper.list3(vo);
- }
-
- @Override
- public List<Map> list4() {
- return bookMapper.list4();
- }
-
- @Override
- public Map list5(Map map) {
- return bookMapper.list5(map);
- }
-
- public BookBizImpl() {
- super();
- }
-
- }
- package com.aspect;
-
- import com.github.pagehelper.PageHelper;
- import com.github.pagehelper.PageInfo;
- import com.zking.mapper.util.PageBean;
- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.annotation.Around;
- import org.aspectj.lang.annotation.Aspect;
- import org.springframework.stereotype.Component;
-
- import java.util.List;
-
- /**
- * @author 杨总
- * @create 2022-08-13 16:11
- * 切面类
- */
- @Component
- @Aspect
- public class PagerAspect {
-
- /**
- * *:返回值类型
- * *..:无线包
- * *Service:以Service结尾接口名
- * *Pager:以Pager方法
- * 只要同时匹配上诉四个条件,就会被列为目标对象
- * 上诉配置要生效,代理注解
不能少 - * @param args
- * @return
- * @throws Throwable
- */
- @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.ssm;
-
- import com.zking.mapper.biz.BookBiz;
- import com.zking.mapper.util.PageBean;
- 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 杨总
- * @create 2022-08-10 23:21
- */
-
-
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
- public class BookBizImplTest2 {
- @Autowired
- private BookBiz bookBiz;
- // private SqlSession sqlSession;
-
- @Before
- public void setUp() throws Exception {
- System.out.println("初始化方法...");
-
- }
-
- @After
- public void tearDown() throws Exception {
- System.out.println("方法测试结束...");
- }
-
- @Test
- public void deleteByPrimaryKey() {
- bookBiz.deleteByPrimaryKey(43);
- }
-
-
-
- @Test
- public void selectByPrimaryKey() {
- System.out.println("测试的业务方法...");
- // System.out.println(bookBiz.getBookMapper());
- System.out.println(bookBiz.selectByPrimaryKey(43));
-
-
- }
-
- @Test
- public void test3() {
- int[] ints={1,2,3,4,5,6};
- // 将数据编程字符串 1,2,3,4,5,6
- StringBuffer sb=new StringBuffer();
- for (int i:ints){
- sb.append(",").append(i);
- }
- String s=sb.toString();
- System.out.println(s.substring(1));
- }
-
-
- @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 getBookMapper() {
- }
-
- @Test
- public void setBookMapper() {
- }
-
- @Test
- public void testlistPager(){
- Map map=new HashMap();
- map.put("bname","圣墟");
- PageBean pageBean=new PageBean();
- pageBean.setPage(2);
- pageBean.setPage(20);
- bookBiz.listPager(map,pageBean).forEach(System.out::println);
-
- }
-
- }
分页运行效果:
今日分享就此结束,再见~