AOP获取Mybatis的SQL,入参,操作类型

5 篇文章 0 订阅
2 篇文章 0 订阅

AOP获取Mybatis的SQL,入参,操作类型


```java
@Aspect
@Component
public class DynamicDataSourceAspect {

  @Autowired
  private SqlSessionFactory sqlSessionFactory;
    
  //mapper切点
  @Pointcut("execution( * com.linkage.xxx.xxx.manager.mapper.manager.*.*(..))")
  public void daoAspect() {
  }

  @After("daoAspect()")
  public void showSql(JoinPoint joinPoint) {

      MethodSignature signature = (MethodSignature) joinPoint.getSignature();
      Method method = signature.getMethod();
      //类路径
      String namespace = method.getDeclaringClass().getName();
      //方法名
      String methodName = method.getName();
      Configuration configuration = sqlSessionFactory.getConfiguration();
      MappedStatement mappedStatement = configuration.getMappedStatement(
          namespace + "." + methodName);
      //操作类型
      SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
      System.out.println("type==>" + sqlCommandType);
      //sql
      String sql = mappedStatement.getBoundSql(null).getSql();
      System.out.println("sql==>" + sql);
	  //入参
      Map<String, Object> parameterMap = new HashMap<>();
      Object[] args = joinPoint.getArgs();
      for (Object o : args) {
        System.out.println("params==>" + o);
      }
    }
}

转载标明出处:https://blog.csdn.net/renhuan28/article/details/123128130
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以Spring Boot为例,可以通过下面的步骤来整合MyBatis:1)引依赖:在项目的pom.xml文件中加MyBatis和MySQL的依赖;2)配置数据源:在application.properties文件中配置MySQL数据源;3)配置MyBatis:在application.properties文件中配置MyBatis的相关数,包括MyBatis的XML配置文件所在路径;4)创建MyBatis的XML配置文件:在配置文件所在路径下创建MyBatis的XML配置文件,用于配置MyBatis的各个组件;5)创建Mapper接口:定义Mapper接口,并且使用@Mapper注解标注;6)编写SQL语句:编写MyBatisSQL语句,并在XML配置文件中进行配置;7)使用MyBatis:通过Mapper接口来调用MyBatisSQL语句,完成数据库操作。 ### 回答2: Spring整合MyBatis是一种常用的Java框架组合,下面将介绍具体的操作和实例。 1. 操作步骤: (1)创建一个Spring项目,并添加Spring和MyBatis的依赖。 (2)在Spring配置文件中配置数据源,可以使用Spring提供的内置数据源或自定义数据源。 (3)配置MyBatisSqlSessionFactory,通过注数据源和设置其他属性来获取SqlSession。 (4)创建映射文件(Mapper XML),其中定义了数据库操作SQL语句,并配置Mapper接口和映射文件的对应关系。 (5)创建Mapper接口,定义与映射文件中SQL语句对应的方法。 (6)在Spring配置文件中配置MapperScannerConfigurer,用于扫描Mapper接口,并将其注册为Spring的Bean。 (7)在业务层中通过@Autowired注解注Mapper接口,即可使用MyBatis进行数据库操作。 2. 实例: 首先,在Spring项目的配置文件中配置数据源和MyBatisSqlSessionFactory: ```xml <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username" value="root" /> <property name="password" value="password" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath:mapper/*.xml" /> </bean> ``` 然后,创建Mapper接口和映射文件,例如UserMapper和user.xml: ```java public interface UserMapper { User getUserById(int id); void addUser(User user); void updateUser(User user); void deleteUser(int id); } <mapper namespace="com.example.mapper.UserMapper"> <select id="getUserById" parameterType="int" resultType="com.example.entity.User"> SELECT * FROM user WHERE id = #{id} </select> <insert id="addUser" parameterType="com.example.entity.User"> INSERT INTO user (id, name) VALUES (#{id}, #{name}) </insert> <update id="updateUser" parameterType="com.example.entity.User"> UPDATE user SET name = #{name} WHERE id = #{id} </update> <delete id="deleteUser" parameterType="int"> DELETE FROM user WHERE id = #{id} </delete> </mapper> ``` 接下来,在业务层中注Mapper接口并使用MyBatis进行数据库操作: ```java @Service public class UserService { @Autowired private UserMapper userMapper; public User getUserById(int id) { return userMapper.getUserById(id); } public void addUser(User user) { userMapper.addUser(user); } public void updateUser(User user) { userMapper.updateUser(user); } public void deleteUser(int id) { userMapper.deleteUser(id); } } ``` 最后,在控制器中注UserService,并调用相应的方法进行数据库操作: ```java @Controller public class UserController { @Autowired private UserService userService; @RequestMapping("/user/{id}") public String getUserById(@PathVariable int id, Model model) { User user = userService.getUserById(id); model.addAttribute("user", user); return "user"; } //... } ``` 通过以上步骤,就可以实现Spring与MyBatis的整合,进行数据库操作。 ### 回答3: Spring整合MyBatis是一种常见的Java开发的架构,它可以将Spring框架与MyBatis框架无缝集成,提供对数据库的访问和操作。以下是整合Spring和MyBatis框架的具体操作和实例。 1. 首先,需要在项目的配置文件中引Spring和MyBatis的相关依赖。可以使用Maven或Gradle等构建工具进行依赖管理,确保项目中包含了Spring和MyBatis的相关依赖项。 2. 在Spring的配置文件中添加对MyBatis的相关配置,例如数据源、事务管理器等。可以通过使用Spring的内置数据源或者自定义数据源来配置数据库连接信息。 3. 创建MyBatis的Mapper接口,在接口中定义需要访问和操作的数据库操作方法。可以使用MyBatis的注解或XML文件进行SQL语句的映射。 4. 创建Mapper接口的实现类,并在类中注SqlSessionFactory和Mapper接口。可以使用Spring的注解或XML配置文件进行依赖注。 5. 在Spring的配置文件中添加对Mapper接口实现类的配置,将其作为bean进行管理。可以使用Spring的注解或XML配置文件进行配置。 6. 在业务逻辑层(Service)中使用Autowired注解将Mapper接口注到Service类中,从而可以在Service中调用Mapper中定义的数据库操作方法。 7. 在需要使用数据库的地方,通过调用Service中的方法来访问和操作数据库。在Service中可以进行事务管理,保证数据操作的一致性和可靠性。 8. 在项目中使用Spring的注解或XML配置文件进行整个应用的配置和管理。可以使用Spring的AOP机制进行事务管理、日志记录等。 以上是使用Spring整合MyBatis的基本操作和实例。通过这种方式,可以实现将Spring和MyBatis无缝集成,提供了对数据库的访问和操作的便利。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值