• mybatis与spring集成


    今天给大家分享spring与mybatis集成,大家都知道mybatis是xml文件与java实现的半自动化实现的数据看CRUD操作的,不理解的小伙伴欢迎参考之前小编写的关于mybastis的博客 

    1. 引入依赖包

    1. <dependency>   
    2. <groupId>org.mybatisgroupId>   
    3. <artifactId>mybatis-springartifactId>   
    4. <version>1.3.2version>
    5. dependency>
    6. <dependency>
    7. <groupId>org.apache.commonsgroupId>
    8. <artifactId>commons-dbcp2artifactId>
    9. <version>2.1.1version>
    10. dependency>
    11. <dependency>
    12. <groupId>org.apache.commonsgroupId>
    13. <artifactId>commons-pool2artifactId>
    14. <version>2.4.3version>
    15. dependency>
    16. <dependency>
    17. <groupId>org.springframeworkgroupId>
    18. <artifactId>spring-txartifactId>
    19. <version>5.3.18version>
    20. dependency>
    21. <dependency>
    22. <groupId>org.springframeworkgroupId>
    23. <artifactId>spring-aspectsartifactId>
    24. <version>5.3.18version>
    25. dependency>
    26. <dependency>
    27. <groupId>org.mybatisgroupId>
    28. <artifactId>mybatis-springartifactId>
    29. <version>1.3.2version>
    30. dependency>

    2. 集成配置文件

    该文件用于spring与mybatis的集成,具体配置文件及注释见的applicationContext-base.xml

    1):
    扫描器,用注解方式注入bean,并指定查找范围:com.zking.oa及子子孙孙包,使用了该注解后,context:annotation-config注解可以省略 

    2) context:annotation-config/: 注解驱动,用于激活已经在spring容器中注册过的bean上面的注解

    用于激活那些已经在spring容器里注册过的bean上面的注解,使用该注解相当于传统方式

    3)开启注解式事务
    : manager属性指定的是事务管理器,具体配置见课件中的完整配置文件 

    3. spring整合mybatis

    1. @Service
    2. public class Userserviceimpl implements Iuserservice {
    3. @Autowired
    4. private Iusermapper iusermapper;
    5. /**
    6. * 分页
    7. * @param u
    8. * @param pagebean
    9. * @return
    10. */
    11. @Override
    12. public List queryPagePaging(User u, PageBean pagebean) {
    13. /*if (pagebean != null && pagebean.isPagination()) {
    14. PageHelper.startPage(pagebean.getPage(), pagebean.getRows());
    15. }
    16. List list = iusermapper.queryPage(u);
    17. if (pagebean != null && pagebean.isPagination()) {
    18. PageInfo info = new PageInfo(list);
    19. pagebean.setTotal((int) info.getTotal());
    20. }*/
    21. return iusermapper.queryPage(u);
    22. }
    23. }
    1. @Repository
    2. public interface Iusermapper {
    3. /**
    4. * 分页
    5. * @param u 需要的实体
    6. * @return
    7. */
    8. List queryPage(User u);
    9. }

    3.1 自动代理

    aspectj-autoproxy/>:自动为spring容器中那些配置@aspectJ切面的bean创建代理。

     applicationContext-base.xml

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    4. xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    6. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
    8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    9. <context:component-scan base-package="com.zking"/>
    10. <context:property-placeholder location="classpath:jdbc.properties"/>
    11. <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
    12. destroy-method="close">
    13. <property name="driverClassName" value="${jdbc.driver}"/>
    14. <property name="url" value="${jdbc.url}"/>
    15. <property name="username" value="${jdbc.username}"/>
    16. <property name="password" value="${jdbc.password}"/>
    17. <property name="initialSize" value="10"/>
    18. <property name="maxTotal" value="100"/>
    19. <property name="maxIdle" value="50"/>
    20. <property name="minIdle" value="10"/>
    21. <property name="maxWaitMillis" value="-1"/>
    22. <property name="testWhileIdle" value="true">property>
    23. <property name="validationQuery" value="select 1">property>
    24. bean>
    25. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    26. <property name="dataSource" ref="dataSource"/>
    27. <property name="mapperLocations" value="classpath:/mapper/**/*.xml"/>
    28. <property name="plugins">
    29. <list>
    30. <bean class="com.github.pagehelper.PageInterceptor">
    31. <property name="properties">
    32. <value>
    33. helperDialect=mysql
    34. value>
    35. property>
    36. bean>
    37. list>
    38. property>
    39. bean>
    40. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    41. <property name="basePackage" value="com/zking/**/mapper"/>
    42. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    43. bean>
    44. <aop:aspectj-autoproxy/>
    45. <bean id="transactionManager"
    46. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    47. <property name="dataSource" ref="dataSource"/>
    48. bean>
    49. <tx:annotation-driven transaction-manager="transactionManager"/>
    50. beans>

    4.spring-test

    1)依赖包

    1. <dependency>
    2. <groupId>org.springframeworkgroupId>
    3. <artifactId>spring-testartifactId>
    4. <version>${spring.version}version>
    5. dependency>

    2)相关的注解

    1. @RunWith(SpringJUnit4ClassRunner.class)
    2. @ContextConfiguration(locations={"classpath*:ApplicationContext*.xml"})

    3)在执行单元测试时,为了避免产生脏数据,可将测试单元设置成事务回滚

    1. @Rollback(value = true|false)//true回滚事务,false提交事务
    2. @Transactional(transactionManager = "transactionManager")

    测试示例: 

    简化分页实现(环捞通知)

    1. @Component
    2. @Aspect
    3. public class PageintAOP {
    4. @Around("execution(* com.zking.mybatis..*.*Paging(..))")
    5. public Object around(ProceedingJoinPoint point) throws Throwable {
    6. //获取所有参数,包括PageBane
    7. Object[] args = point.getArgs();
    8. PageBean pageBean=null;
    9. for (Object arg:args){
    10. if (arg instanceof PageBean){//instanceof是否存在
    11. pageBean = (PageBean) arg;
    12. if(pageBean !=null && pageBean.isPagination()){
    13. PageHelper.startPage(pageBean.getPage(),pageBean.getRows());//放入线程本地方法
    14. }
    15. }
    16. }
    17. //获取目标方法的分页参数,封装到List集合里
    18. Object rv = point.proceed();
    19. if(pageBean !=null && pageBean.isPagination()){
    20. PageInfo info=new PageInfo((List)rv);//获取List集合
    21. pageBean.setTotal((int) info.getTotal());
    22. }
    23. return rv;
    24. }
    25. }
    @Around:环捞通知定义

    mapper层

    1. @Repository
    2. public interface Iusermapper {
    3. /**
    4. * 分页
    5. * @param u 需要的实体
    6. * @return
    7. */
    8. List queryPage(User u);
    9. }
    1. <select id="queryPage" resultType="com.zking.mybatis.model.User">
    2. select id,name,loginName,pwd,rid from t_oa_user
    3. <where>
    4. <if test="name != null and name != ''">
    5. and name like concat('%', #{name}, '%')
    6. if>
    7. where>
    8. select>

    service层

    1. @Service
    2. public class Userserviceimpl implements Iuserservice {
    3. @Autowired
    4. private Iusermapper iusermapper;
    5. /**
    6. * 分页
    7. * @param u
    8. * @param pagebean
    9. * @return
    10. */
    11. @Override
    12. public List queryPagePaging(User u, PageBean pagebean) {
    13. /*if (pagebean != null && pagebean.isPagination()) {
    14. PageHelper.startPage(pagebean.getPage(), pagebean.getRows());
    15. }
    16. List list = iusermapper.queryPage(u);
    17. if (pagebean != null && pagebean.isPagination()) {
    18. PageInfo info = new PageInfo(list);
    19. pagebean.setTotal((int) info.getTotal());
    20. }*/
    21. return iusermapper.queryPage(u);
    22. }
    23. }
    1. public interface Iuserservice {
    2. /**
    3. * 分页
    4. * @param u
    5. * @param pagebean
    6. * @return
    7. */
    8. List queryPagePaging(User u, PageBean pagebean);
    9. }

    Test测试 

    1. //指定文件进行配置,用于生成spring上下文对象
    2. @RunWith(SpringJUnit4ClassRunner.class)
    3. @ContextConfiguration("classpath*:applicationContext*.xml")
    4. public class UserserviceimplTest {
    5. //通过mybatis与spring集成后进行依赖注入
    6. @Autowired
    7. private Iuserservice userserviceimpl;
    8. @Test
    9. public void queryPage(){
    10. User u=new User();
    11. PageBean pageBean = new PageBean();
    12. pageBean.setRows(4);
    13. List list = userserviceimpl.queryPagePaging(u, pageBean);
    14. list.forEach(t-> System.out.println(t));
    15. }
    16. }

    5spring常用注解

    • @Controller:用于控制层的组件(spring的Action类似)
    • @Repository:用于数据访问层(DAO)的组件
    • @Service:用于业务逻辑层组件
    • @Component:泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
    • @Around:环捞通知定义
    • @Aspect:用于切面的组件
    
                    
  • 相关阅读:
    Claude3荣登榜首,亚马逊云科技为您提供先行体验!
    javaweb 之 Request 和 Response 常见案例 用户登录和注册
    C语言 字符数组
    使用promise创建一个同步事件驱动api
    软考高级系统架构设计师系列论文真题九:论软件多层架构的设计
    docke安装mysql以及主从搭建(并且指定数据生成路径)
    UDP 报文结构与注意事项全解析
    23. [Python GUI] PyQt5中的模型与视图框架-抽象视图基类QAbstractItemView
    【目标检测】39、一文看懂计算机视觉中的数据增强
    【 Linux 】密钥对登录Ubuntu 20.04
  • 原文地址:https://blog.csdn.net/Bugxiu_fu/article/details/126548616