【Spring学习笔记 八】Spring整合MyBatis实现方式(下)

简介: 【Spring学习笔记 八】Spring整合MyBatis实现方式(下)

Spring的实现回顾

当然我们还需要配置相关Spring的内容,例如我们通过Service来控制Dao层的实现,并且在调用方法前后增加切面控制增加访问日志:

1 创建Service层

创建PersonService及其实现类

PersonService

package com.example.spring_mybatis.service;
import com.example.spring_mybatis.model.Person;
import java.util.List;
public interface PersonService {
    List<Person> getPersonList();
}

PersonService

package com.example.spring_mybatis.serviceImpl;
import com.example.spring_mybatis.daoImpl.PersonDaoImpl;
import com.example.spring_mybatis.model.Person;
import com.example.spring_mybatis.service.PersonService;
import lombok.Data;
import java.util.List;
@Data
public class PersonServiceImpl implements PersonService {
    private PersonDaoImpl personDaoImpl;
    @Override
    public List<Person> getPersonList() {
        return personDaoImpl.getPersonList();
    }
}

2 创建Dao层

Dao层的接口我们已经在MyBatis部分实现了,接下来只要编写其实现类即可:

package com.example.spring_mybatis.daoImpl;
import com.example.spring_mybatis.dao.PersonDao;
import com.example.spring_mybatis.model.Person;
import java.util.List;
public class PersonDaoImpl implements PersonDao {
    @Override
    public List<Person> getPersonList() {
        return null;
    }
}

这里我们先不编写其实现,这里的实现其实就是我们今天的主题。

3 增加AOP类

增加Aop类关于日志的,扫描service包,给这个包里的方法前后都加日志:

package com.example.spring_mybatis.aop;
import java.time.LocalDateTime;
/**
 * * @Name LogProxy
 * * @Description
 * * @author tianmaolin
 * * @Data 2021/8/24
 */
public class LogAop  {
    public void beforeLog()  {
        System.out.println("日志记录开始"+ LocalDateTime.now());
    }
    public void afterLog()  {
        System.out.println("日志记录结束"+ LocalDateTime.now().plusMinutes(5));
    }
}

4 applicationContext.xml配置文件编写

我们先编写目前用到的一些配置,包括Service对Dao的调用:

<?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: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/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 使用aop注解 -->
    <aop:aspectj-autoproxy/>
    <!-- 定义目标对象:定义被代理者 -->
    <bean id="personServiceImpl" class="com.example.spring_mybatis.serviceImpl.PersonServiceImpl">
        <property name="personDaoImpl" ref="personDaoImpl"/>
    </bean>
    <bean id="personDaoImpl" class="com.example.spring_mybatis.daoImpl.PersonDaoImpl">
    </bean>
    <!-- 定义切面,切面内包含通知要执行的方法-->
    <bean id="logAop" class="com.example.spring_mybatis.aop.LogAop"></bean>
    <!--aop的配置-->
    <aop:config proxy-target-class="true">
        <!--切面配置-->
        <aop:aspect ref="logAop">
            <!--切点-->
            <aop:pointcut id="logPointCut" expression="execution(* com.example.spring_mybatis.service..*.*(..))"/>
            <!--切点-通知-->
            <aop:before pointcut-ref="logPointCut" method="beforeLog"/>
            <aop:after pointcut-ref="logPointCut" method="afterLog"/>
        </aop:aspect>
    </aop:config>
</beans>

Spring整合MyBatis

Spring整合用到一个依赖,也就是我们上述坐标中的,其官方文档介绍:mybatis-spring

<!-- Mybatis-Spring相关 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.6</version>
        </dependency>

什么是 MyBatis-Spring?MyBatis-Spring 会帮助我们将 MyBatis 代码无缝地整合到 Spring 中。Spring整合MyBatis, 主要目的就是把MyBatis核心配置文件及Mapper文件中的内容交给Spring来处理,通俗的说,就是Spring把SqlSession的创建以及Mapper对象的创建都代理了,事实上就是我们不用再编写相关的配置文件以及配置文件读取类了,也就是我们上述的8个步骤中4-6步都不用做了,Spring完全代工。

1 SqlSession注入实现

那么首先来看第一种方式,我们通过这种方式可以干掉mybatis-config.xml,完全通过Spring来实现.

1-1 调整配置文件applicationContext.xml

由Spring来构建SqlSession,同时MybatisUtil也不需要了,SqlSession的构建完全交给Spring来实现,调整后配置如下:

<?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: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/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 使用aop注解 -->
    <aop:aspectj-autoproxy/>
    <!-- 定义目标对象:定义被代理者 -->
    <bean id="personServiceImpl" class="com.example.spring_mybatis.serviceImpl.PersonServiceImpl">
        <property name="personDaoImpl" ref="personDaoImpl"/>
    </bean>
    <bean id="personDaoImpl" class="com.example.spring_mybatis.daoImpl.PersonDaoImpl">
        <property name="sqlSession" ref="sqlSession"/>
    </bean>
    <!--DataSource: 使用Spring的数据源替换Mybatis的配置, 可以使用c3p0,dbcp,druid,或者spring提供的jdbc-->
    <!-- 加载数据库配置信息 -->
    <context:property-placeholder location="properties/db.properties" system-properties-mode="NEVER"/>
    <!-- 连接池对象 -->
    <bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
    </bean>
    <!--配置日志输出-->
    <bean id="configuration" class="org.apache.ibatis.session.Configuration">
        <property name="logImpl" value="org.apache.ibatis.logging.stdout.StdOutImpl"/>
    </bean>
    <!--配置SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="myDataSource"/>
        <!--关联Mybatis-->
        <!--<property name="configLocation" value="mybatis-config.xml"/>-->
        <property name="mapperLocations" value="mappers/*.xml"/>
        <property name="configuration" ref="configuration"/>
    </bean>
    <!--注册sqlSessionTemplate , 关联sqlSessionFactory-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--只能通过构造器注入sqlSessionFactory,因为没有set方法-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
    <!-- 加载日志配置信息 -->
    <context:property-placeholder location="log4j.properties" system-properties-mode="NEVER"/>
    <!-- 定义切面,切面内包含通知要执行的方法-->
    <bean id="logAop" class="com.example.spring_mybatis.aop.LogAop"></bean>
    <!--aop的配置-->
    <aop:config proxy-target-class="true">
        <!--切面配置-->
        <aop:aspect ref="logAop">
            <!--切点-->
            <aop:pointcut id="logPointCut" expression="execution(* com.example.spring_mybatis.service..*.*(..))"/>
            <!--切点-通知-->
            <aop:before pointcut-ref="logPointCut" method="beforeLog"/>
            <aop:after pointcut-ref="logPointCut" method="afterLog"/>
        </aop:aspect>
    </aop:config>
</beans>

上述配置中我们用到了这几个概念:

  • 在基础的 MyBatis 用法中,是通过 SqlSessionFactoryBuilder 来创建 SqlSessionFactory ,而在 MyBatis-Spring 中,则使用 SqlSessionFactoryBean 来创建
  • SqlSessionTemplate 是 MyBatis-Spring 的核心。作为 SqlSession 的一个实现,这意味着可以使用它无缝代替代码中已经在使用的 SqlSession

也就是原生Mybatis的SqlSession获取过程被Spring给接管了。

1-2 调整PersonDaoImpl类实现

由上边的配置我们发现,SqlSession被构建好后注入到了PersonDaoImpl中,这样实现类就可以打开一个session并执行相关逻辑了,然后具体的实现使用SqlSession通过personMapper.xml配置文件去构建代理对象然后调用方法实现。

package com.example.spring_mybatis.daoImpl;
import com.example.spring_mybatis.dao.PersonDao;
import com.example.spring_mybatis.model.Person;
import lombok.Data;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
@Data
public class PersonDaoImpl implements PersonDao {
    private SqlSessionTemplate sqlSession;
    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }
    @Override
    public List<Person> getPersonList() {
        return  sqlSession.getMapper(PersonDao.class).getPersonList();
    }
}

1-3 测试实现方式

最后我们来编写个单元测试类来测试下实现过程:

import com.example.spring_mybatis.service.PersonService;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PersonServiceTest {
    @Test
    public void getPersonListTest(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        PersonService personService = (PersonService) applicationContext.getBean("personServiceImpl");
        personService.getPersonList();
    }
}

打印结果如下:

2 SqlSessionFactory注入实现

让Dao继承Support类 , 直接利用 getSqlSession()获得SqlSessionTemplate , 然后直接注入SqlSessionFactory . 比起方式1 不需要管理SqlSessionTemplate , 而且对事务的支持更加友好

再向下追溯,可以发现SqlSessionTemplate有属性sqlSessionFactory,所以按照依赖链正常注入使用:

2-1 调整配置文件applicationContext.xml

我们调整配置文件,让SqlSessionFactory直接注入到PersonDaoImpl,不再使用中间的SqlSessionTemplate作为媒介:

<?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: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/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 使用aop注解 -->
    <aop:aspectj-autoproxy/>
    <!-- 定义目标对象:定义被代理者 -->
    <bean id="personServiceImpl" class="com.example.spring_mybatis.serviceImpl.PersonServiceImpl">
        <property name="personDaoImpl" ref="personDaoImpl"/>
    </bean>
    <bean id="personDaoImpl" class="com.example.spring_mybatis.daoImpl.PersonDaoImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
    <!--DataSource: 使用Spring的数据源替换Mybatis的配置, 可以使用c3p0,dbcp,druid,或者spring提供的jdbc-->
    <!-- 加载数据库配置信息 -->
    <context:property-placeholder location="properties/db.properties" system-properties-mode="NEVER"/>
    <!-- 连接池对象 -->
    <bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
    </bean>
    <!--配置日志输出-->
    <bean id="configuration" class="org.apache.ibatis.session.Configuration">
        <property name="logImpl" value="org.apache.ibatis.logging.stdout.StdOutImpl"/>
    </bean>
    <!--配置SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="myDataSource"/>
        <!--关联Mybatis-->
        <!--<property name="configLocation" value="mybatis-config.xml"/>-->
        <property name="mapperLocations" value="mappers/*.xml"/>
        <property name="configuration" ref="configuration"/>
    </bean>
    <!-- 加载日志配置信息 -->
    <context:property-placeholder location="log4j.properties" system-properties-mode="NEVER"/>
    <!-- 定义切面,切面内包含通知要执行的方法-->
    <bean id="logAop" class="com.example.spring_mybatis.aop.LogAop"></bean>
    <!--aop的配置-->
    <aop:config proxy-target-class="true">
        <!--切面配置-->
        <aop:aspect ref="logAop">
            <!--切点-->
            <aop:pointcut id="logPointCut" expression="execution(* com.example.spring_mybatis.service..*.*(..))"/>
            <!--切点-通知-->
            <aop:before pointcut-ref="logPointCut" method="beforeLog"/>
            <aop:after pointcut-ref="logPointCut" method="afterLog"/>
        </aop:aspect>
    </aop:config>
</beans>

2-2 调整PersonDaoImpl类实现

当然相应的也要调整PersonDaoImpl类的实现方式:

package com.example.spring_mybatis.daoImpl;
import com.example.spring_mybatis.dao.PersonDao;
import com.example.spring_mybatis.model.Person;
import lombok.Data;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;
@Data
public class PersonDaoImpl extends SqlSessionDaoSupport implements PersonDao {
    @Override
    public List<Person> getPersonList() {
        return  getSqlSession().getMapper(PersonDao.class).getPersonList();
    }
}

2-3 测试实现方式

测试类还使用之前的:

import com.example.spring_mybatis.service.PersonService;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PersonServiceTest {
    @Test
    public void getPersonListTest(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        PersonService personService = (PersonService) applicationContext.getBean("personServiceImpl");
        personService.getPersonList();
    }
}

可以看到打印结果同上:

3 Mapper对象自动扫描注入实现

前两种的整合实现方式都创建了Dao接口的实现类,通过SqlSession来获取mapper对象。使用第三种方式我们可以告诉Spring让其帮我们自动创建mapper的代理对象,也就是我们不再需要PersonDaoImpl了,它也由Spring代为生成:

3-1 调整配置文件applicationContext.xml

我们调整配置文件,干掉personDaoImpl的相关配置,直接使用映射类生成器生成:

<?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: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/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 使用aop注解 -->
    <aop:aspectj-autoproxy/>
    <!-- 定义目标对象:定义被代理者 -->
    <bean id="personServiceImpl" class="com.example.spring_mybatis.serviceImpl.PersonServiceImpl">
        <property name="userMapper" ref="userMapper"/>
    </bean>
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        <!--给哪个接口创建代理对象-->
        <property name="mapperInterface" value="com.example.spring_mybatis.dao.PersonDao"/>
    </bean>
    <!--DataSource: 使用Spring的数据源替换Mybatis的配置, 可以使用c3p0,dbcp,druid,或者spring提供的jdbc-->
    <!-- 加载数据库配置信息 -->
    <context:property-placeholder location="properties/db.properties" system-properties-mode="NEVER"/>
    <!-- 连接池对象 -->
    <bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
    </bean>
    <!--配置日志输出-->
    <bean id="configuration" class="org.apache.ibatis.session.Configuration">
        <property name="logImpl" value="org.apache.ibatis.logging.stdout.StdOutImpl"/>
    </bean>
    <!--配置SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="myDataSource"/>
        <!--关联Mybatis-->
        <!--<property name="configLocation" value="mybatis-config.xml"/>-->
        <property name="mapperLocations" value="mappers/*.xml"/>
        <property name="configuration" ref="configuration"/>
    </bean>
    <!-- 加载日志配置信息 -->
    <context:property-placeholder location="log4j.properties" system-properties-mode="NEVER"/>
    <!-- 定义切面,切面内包含通知要执行的方法-->
    <bean id="logAop" class="com.example.spring_mybatis.aop.LogAop"></bean>
    <!--aop的配置-->
    <aop:config proxy-target-class="true">
        <!--切面配置-->
        <aop:aspect ref="logAop">
            <!--切点-->
            <aop:pointcut id="logPointCut" expression="execution(* com.example.spring_mybatis.service..*.*(..))"/>
            <!--切点-通知-->
            <aop:before pointcut-ref="logPointCut" method="beforeLog"/>
            <aop:after pointcut-ref="logPointCut" method="afterLog"/>
        </aop:aspect>
    </aop:config>
</beans>

3-2 调整PersonServiceImpl类实现

同时我们需要在PersonServiceImpl中替换方法的实现方式,使用我们自动生成的userMapper,而不是对应创建的:

package com.example.spring_mybatis.serviceImpl;
import com.example.spring_mybatis.dao.PersonDao;
import com.example.spring_mybatis.daoImpl.PersonDaoImpl;
import com.example.spring_mybatis.model.Person;
import com.example.spring_mybatis.service.PersonService;
import lombok.Data;
import java.util.List;
@Data
public class PersonServiceImpl implements PersonService {
    private PersonDao userMapper;
    @Override
    public List<Person> getPersonList() {
        List<Person> personList=userMapper.getPersonList();
        for (Person person : personList) {
            System.out.println(person);
        }
        return personList;
    }
}

3-3 测试实现方式

测试类还使用之前的:

import com.example.spring_mybatis.service.PersonService;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PersonServiceTest {
    @Test
    public void getPersonListTest(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        PersonService personService = (PersonService) applicationContext.getBean("personServiceImpl");
        personService.getPersonList();
    }
}

打印结果如下:

总结一下

完成了上述的实现方式之后,我们确定最终的项目整体结构如下:

本篇Blog讨论了三种Spring整合MyBatis的方式,随着实现方式的递进,我们越来越多的工作交给了Spring去做,第一种整合方式我们配置了sqlSessionFactory(myDataSource,configuration),sqlSession,然后把sqlSession注入personDaoImpl类,通过这个步骤,我们摆脱了mybatis-config.xml核心配置文件的编写、MybatisUtils辅助类的编写。第二种整合方式我们抛弃了sqlSession,直接用sqlSessionFactory注入personDaoImpl实现。第三种整合方式我们则干脆抛弃了实现类personDaoImpl的编写,实现类的生成由Spring控制,其实继续演化下去,我们连mapper配置文件都可以抛弃,那就是使用注解,这些内容在Spring Boot中体现吧,所以框架就是先给出配置规则,再持续简化,让程序员逐渐变成傻瓜,俗称傻瓜式编程,但是如果原理不知道,出了错误可就不好排查了,所以我们还是不要做傻瓜好了。

相关实践学习
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
相关文章
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于 xml 的整合
本教程介绍了基于XML的MyBatis整合方式。首先在`application.yml`中配置XML路径,如`classpath:mapper/*.xml`,然后创建`UserMapper.xml`文件定义SQL映射,包括`resultMap`和查询语句。通过设置`namespace`关联Mapper接口,实现如`getUserByName`的方法。Controller层调用Service完成测试,访问`/getUserByName/{name}`即可返回用户信息。为简化Mapper扫描,推荐在Spring Boot启动类用`@MapperScan`注解指定包路径避免逐个添加`@Mapper`
1409 0
|
Java 数据库连接 数据库
Spring boot 使用mybatis generator 自动生成代码插件
本文介绍了在Spring Boot项目中使用MyBatis Generator插件自动生成代码的详细步骤。首先创建一个新的Spring Boot项目,接着引入MyBatis Generator插件并配置`pom.xml`文件。然后删除默认的`application.properties`文件,创建`application.yml`进行相关配置,如设置Mapper路径和实体类包名。重点在于配置`generatorConfig.xml`文件,包括数据库驱动、连接信息、生成模型、映射文件及DAO的包名和位置。最后通过IDE配置运行插件生成代码,并在主类添加`@MapperScan`注解完成整合
1890 1
Spring boot 使用mybatis generator 自动生成代码插件
|
Java 数据库连接 API
Java 对象模型现代化实践 基于 Spring Boot 与 MyBatis Plus 的实现方案深度解析
本文介绍了基于Spring Boot与MyBatis-Plus的Java对象模型现代化实践方案。采用Spring Boot 3.1.2作为基础框架,结合MyBatis-Plus 3.5.3.1进行数据访问层实现,使用Lombok简化PO对象,MapStruct处理对象转换。文章详细讲解了数据库设计、PO对象实现、DAO层构建、业务逻辑封装以及DTO/VO转换等核心环节,提供了一个完整的现代化Java对象模型实现案例。通过分层设计和对象转换,实现了业务逻辑与数据访问的解耦,提高了代码的可维护性和扩展性。
693 1
|
SQL Java 数据库
解决Java Spring Boot应用中MyBatis-Plus查询问题的策略。
保持技能更新是侦探的重要素质。定期回顾最佳实践和新技术。比如,定期查看MyBatis-Plus的更新和社区的最佳做法,这样才能不断提升查询效率和性能。
1041 1
|
SQL Java 数据库连接
Spring、SpringMVC 与 MyBatis 核心知识点解析
我梳理的这些内容,涵盖了 Spring、SpringMVC 和 MyBatis 的核心知识点。 在 Spring 中,我了解到 IOC 是控制反转,把对象控制权交容器;DI 是依赖注入,有三种实现方式。Bean 有五种作用域,单例 bean 的线程安全问题及自动装配方式也清晰了。事务基于数据库和 AOP,有失效场景和七种传播行为。AOP 是面向切面编程,动态代理有 JDK 和 CGLIB 两种。 SpringMVC 的 11 步执行流程我烂熟于心,还有那些常用注解的用法。 MyBatis 里,#{} 和 ${} 的区别很关键,获取主键、处理字段与属性名不匹配的方法也掌握了。多表查询、动态
562 0
|
SQL Java 数据库连接
对Spring、SpringMVC、MyBatis框架的介绍与解释
Spring 框架提供了全面的基础设施支持,Spring MVC 专注于 Web 层的开发,而 MyBatis 则是一个高效的持久层框架。这三个框架结合使用,可以显著提升 Java 企业级应用的开发效率和质量。通过理解它们的核心特性和使用方法,开发者可以更好地构建和维护复杂的应用程序。
1166 29
|
SQL JavaScript Java
Spring Boot 3 整合 Mybatis-Plus 实现数据权限控制
本文介绍了如何在Spring Boot 3中整合MyBatis-Plus实现数据权限控制,通过使用MyBatis-Plus提供的`DataPermissionInterceptor`插件,在不破坏原有代码结构的基础上实现了细粒度的数据访问控制。文中详细描述了自定义注解`DataScope`的使用方法、`DataPermissionHandler`的具体实现逻辑,以及根据用户的不同角色和部门动态添加SQL片段来限制查询结果。此外,还展示了基于Spring Boot 3和Vue 3构建的前后端分离快速开发框架的实际应用案例,包括项目的核心功能模块如用户管理、角色管理等,并提供Gitee上的开源仓库
3872 11
|
前端开发 Java 数据库连接
Java后端开发-使用springboot进行Mybatis连接数据库步骤
本文介绍了使用Java和IDEA进行数据库操作的详细步骤,涵盖从数据库准备到测试类编写及运行的全过程。主要内容包括: 1. **数据库准备**:创建数据库和表。 2. **查询数据库**:验证数据库是否可用。 3. **IDEA代码配置**:构建实体类并配置数据库连接。 4. **测试类编写**:编写并运行测试类以确保一切正常。
1081 2
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于注解的整合
本文介绍了Spring Boot集成MyBatis的两种方式:基于XML和注解的形式。重点讲解了注解方式,包括@Select、@Insert、@Update、@Delete等常用注解的使用方法,以及多参数时@Param注解的应用。同时,针对字段映射不一致的问题,提供了@Results和@ResultMap的解决方案。文章还提到实际项目中常结合XML与注解的优点,灵活使用两者以提高开发效率,并附带课程源码供下载学习。
1349 0
|
Java 数据库连接 数据库
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——MyBatis 介绍和配置
本文介绍了Spring Boot集成MyBatis的方法,重点讲解基于注解的方式。首先简述MyBatis作为持久层框架的特点,接着说明集成时的依赖导入,包括`mybatis-spring-boot-starter`和MySQL连接器。随后详细展示了`properties.yml`配置文件的内容,涵盖数据库连接、驼峰命名规范及Mapper文件路径等关键设置,帮助开发者快速上手Spring Boot与MyBatis的整合开发。
2182 0