MyBatis-Flex 架构下使用 XML 方式实现数据库操作

概述

MyBatis-Flex 是一个对 MyBatis 进行增强的框架,它继承了 MyBatis 的所有特性,并在此基础上提供了更多的灵活性和便利性。在本文中,我们将探讨如何在 MyBatis-Flex 架构下使用 XML 方式来实现数据库中数据的增加、删除、修改和查询操作。

环境搭建

在开始之前,请确保你已经搭建好了以下环境:

  • JDK 1.8 或更高版本
  • Maven 或 Gradle 构建工具
  • MyBatis-Flex 依赖
  • 数据库(如 MySQL)

添加依赖

如果你使用 Maven,可以在 pom.xml 文件中添加以下依赖:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.mybatis-flex</groupId>
            <artifactId>mybatis-flex-spring-boot-starter</artifactId>
            <version>1.9.7</version>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

配置 MyBatis-Flex

数据源配置

application.propertiesapplication.yml 文件中配置数据库连接信息:

server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/xxx
    username: root
    password: 123456

MyBatis-Flex 配置

在 Spring Boot 配置类中添加 MyBatis-Flex 配置:

@Configuration
@MapperScan("com.example.mapper")
public class MyBatisFlexConfig {
    // 配置分页插件
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

XML 映射文件配置

增加数据

src/main/resources/mapper 目录下创建一个 XML 文件,例如 ScoreinfoMapper.xml

<insert id="insertByStudentinfoId" parameterType="java.lang.Integer" >
    insert into tb_scoreinfo (id,studentinfo_id,c_name,grade) values (#{id},#{studentinfoId},#{c_name},#{grade});
</insert>

删除数据

<delete id="deleteByStudentinfoId" parameterType="java.lang.Integer">
        delete from tb_scoreinfo where id=#{id}
</delete>

修改数据

<update id="updateByStudentinfoId" parameterType="java.lang.Integer">
    update tb_scoreinfo set c_name='J2EE' where id=#{studentinfoId}
</update>

查询数据

<select id="selectByStudentinfoId" resultType="com.example.myapp.class_04_2303840463lsh_03.po.Scoreinfo">
    SELECT * from tb_scoreinfo where studentinfo_id=#{studentinfoId}
</select>

使用 Mapper

在你的服务层或控制器中注入 ScoreinfoMapper 并使用定义的方法:

public interface ScoreinfoMapper extends BaseMapper<Scoreinfo> {
    List<Scoreinfo> selectByStudentinfoId(int studentinfoId);
    int updateByStudentinfoId(int studentinfoId);
    int insertByStudentinfoId(Scoreinfo scoreinfo);
    int deleteByStudentinfoId(int id);
}

测试

@SpringBootTest
class Class042303840463lsh03ApplicationTests {
    private StudentinfoMapper studentinfoMapper;
    @Autowired
    private ScoreinfoMapper scoreinfoMapper;

    @Test
    void studentinfoUpdateByStudentinfoId(){
        int scoreinfoss=scoreinfoMapper.updateByStudentinfoId(63);
        System.out.println(scoreinfoss);
    }
    @Test
    void studnetinfoInsertByStudentinfo(){
        Scoreinfo scoreinfo=new Scoreinfo(53,53,"C++",4);
        int scoreinfoss=scoreinfoMapper.insertByStudentinfoId(scoreinfo);
        System.out.println(scoreinfoss);
    }
    @Test
    void studentinfoSelectByStudentinfoId(){
        List<Scoreinfo> scoreinfos=scoreinfoMapper.selectByStudentinfoId(53);
        System.out.println(scoreinfos);
    }
    @Test
    void scoreinfoDeleteByStudentinfoId(){
        int scoreinfoss=scoreinfoMapper.deleteByStudentinfoId(53);
        System.out.println(scoreinfoss);
    }
}

结果

总结

通过上述步骤,你可以使用 MyBatis-Flex 和 XML 配置文件来实现数据库的增加、删除、修改和查询操作。这种方式提供了清晰和灵活的 SQL 管理,有助于维护大型项目中的数据库操作。

希望这篇博客能帮助你更好地理解和使用 MyBatis-Flex。如果你有任何问题或需要进一步的帮助,请随时联系我。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值