AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

你還在用Mybatis?試試這款神器,縱享絲滑

來源: 責編: 時間:2024-03-26 17:40:05 188觀看
導讀在 Java 項目中,我們經常需要與數據庫進行交互,而 Mybatis 是一個流行的持久層框架,它可以將 Java 對象映射到 SQL 語句,從而簡化數據庫操作。不過在使用 Mybatis 做開發時,最頭痛的事情就是處理復雜業務查詢,如果將業務轉

在 Java 項目中,我們經常需要與數據庫進行交互,而 Mybatis 是一個流行的持久層框架,它可以將 Java 對象映射到 SQL 語句,從而簡化數據庫操作。vVh28資訊網——每日最新資訊28at.com

不過在使用  Mybatis 做開發時,最頭痛的事情就是處理復雜業務查詢,如果將業務轉移到service層,就會增加訪問數據庫的次數。

如果放到dao層,就要手動在xml中寫復雜的sql,用插件自動生成mapper時,會覆蓋xml中的修改,非常讓人頭疼vVh28資訊網——每日最新資訊28at.com

Fluent Mybatis是什么

Fluent Mybatis 是一個基于 Mybatis 的擴展庫,它提供了更加簡潔、易讀的 API,使得我們能夠更加高效地進行數據庫操作。vVh28資訊網——每日最新資訊28at.com

本文將介紹如何使用 Fluent Mybatis 進行基本的增刪改查操作。vVh28資訊網——每日最新資訊28at.com

使用 Fluent Mybatis 很簡單,分為以下5步:vVh28資訊網——每日最新資訊28at.com

1. 環境準備

在開始使用 Fluent Mybatis 之前,我們需要確保已經安裝了 Java 開發環境、Maven 以及 MySQL 數據庫。接下來,我們需要在項目的 pom.xml 文件中添加 Fluent Mybatis 的依賴:vVh28資訊網——每日最新資訊28at.com

<dependencies><!--引入fluent-mybatis運行依賴包,scope為compile-->        <dependency>            <groupId>com.github.atool</groupId>            <artifactId>fluent-mybatis</artifactId>            <version>1.9.3</version>        </dependency>        <!--引入fluent-mybatis-processor,scope設置為provider編譯需要,運行時不需要-->        <dependency>            <groupId>com.github.atool</groupId>            <artifactId>fluent-mybatis-processor</artifactId>            <version>1.9.3</version>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-jdbc</artifactId>            <version>2.6.2</version>        </dependency>        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>8.0.30</version>        </dependency></dependencies>

2. 定義表結構

CREATE TABLE `user_info` (  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '自增id',  `user_name` varchar(64) DEFAULT NULL COMMENT '用戶名稱',  `user_age` int NOT NULL DEFAULT '0' COMMENT '用戶年齡',  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=101 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='用戶信息表';

3. 定義PO類

@FluentMybatis(table = "user_info") // 定義表名稱@Datapublic class UserInfoEntity extends RichEntity {    private Long id;    private String userName;    private Integer userAge;    @Override    public Class<? extends IEntity> entityClass() {        return UserInfoEntity.class;    }}

4. 編譯生成對應的mapper類

mvn clean compile

自動生成如下類:vVh28資訊網——每日最新資訊28at.com

圖片圖片vVh28資訊網——每日最新資訊28at.com

5. 配置數據源

首先,我們需要配置 Mybatis 的相關信息,如數據源、SQL 會話工廠等。這里,我們使用 Spring Boot 進行配置:vVh28資訊網——每日最新資訊28at.com

@ComponentScan(basePackages = "com.test")@MapperScan("com.test.mapper")@Configurationpublic class DataSourceConfig {    @Bean    public DataSource dataSource() {        final HikariDataSource dataSource = new HikariDataSource();        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8");        dataSource.setUsername("root");        dataSource.setPassword("1234");        return dataSource;    }    /**     * 定義mybatis的SqlSessionFactoryBean     *     * @param dataSource     * @return     */    @Bean    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();        bean.setDataSource(dataSource);        return bean;    }    @Bean    public MapperFactory mapperFactory() {        return new MapperFactory();    }}

如何使用 Fluent Mybatis?

下面我們使用 Fluent Mybatis 進行增刪改查的基本操作演示:vVh28資訊網——每日最新資訊28at.com

4.1 插入(Insert)

public class UserServiceTest {    @Autowired    private UserInfoMapper mapper;        @Test    public void insert() {        UserInfoEntity entity = new UserInfoEntity();        entity.setUserAge(10);        entity.setUserName("zhangsan");        mapper.insert(entity);    }}

在這個示例中,我們首先創建了一個新的 UserInfoEntity 對象,并設置了其 userName 和 userAge 屬性。然后,我們調用 UserInfoMapper.insert(entity) 方法將新用戶插入到數據庫中。vVh28資訊網——每日最新資訊28at.com

4.2 查詢(Select)

public class UserService {    @Autowired    private UserInfoMapper mapper;        @Test    public void select() {        UserInfoEntity entity = mapper.findOne(mapper.query()                .where.userName().like("zhangsan").end()                .limit(1));        System.out.println(entity);    }

在這個示例中,我們展示了如何使用 Fluent Mybatis 進行不同類型的查詢操作。我們可以根據用戶名模糊查詢用戶。vVh28資訊網——每日最新資訊28at.com

4.3 更新(Update)

public class UserService {    @Autowired    private UserInfoMapper mapper;    @Test    public void update() {        mapper.updateBy(mapper.updater()                .set.userName().is("lisi")                .set.userAge().is(20).end()                .where.id().eq(1L).end()        );    }}

在這個示例中,我們首先根據 ID 查詢到需要更新的用戶。然后,我們修改用戶的 name 和 age 屬性,并調用 UserMapper.updateBy() 方法將更新后的用戶信息保存到數據庫中。vVh28資訊網——每日最新資訊28at.com

4.4 刪除(Delete)

public class UserService {    @Autowired    private UserInfoMapper mapper;    @Test    public void delete() {        mapper.delete(mapper.query()                .where.id().eq(1L).end());    }}

在這個示例中,我們展示了如何使用 Fluent Mybatis 進行刪除操作。我們可以根據 ID 刪除用戶。vVh28資訊網——每日最新資訊28at.com

4.5 join查詢

@Test    public void joinQuery() {        JoinBuilder<FreeQuery> joinBuilder = null;        UserInfoQuery leftQuery = UserInfoQuery.query().selectAll();        UserInfoQuery rightQuery = UserInfoQuery.query().select.id("right_id").end(); // 添加別名        joinBuilder = JoinBuilder.from(leftQuery).join(rightQuery).onEq("id", "id").endJoin().build(); // join條件        FreeQuery iQuery = new FreeQuery(joinBuilder.build(), "sub").select("id", "user_name", "user_age"); // 輸出查詢字段        List<UserInfoEntity> userInfoEntities = mapper.listPoJos(UserInfoEntity.class, iQuery);        System.out.println(userInfoEntities);    }

在這個示例中,我們展示了如何使用 Fluent Mybatis 進行join查詢操作。我們將表user_info自身進行join,以id作為連接條件。vVh28資訊網——每日最新資訊28at.com

總結

通過以上示例,我們可以看到 Fluent Mybatis 提供了一套簡潔、易讀的 API,使得我們能夠更加高效地進行數據庫操作。同時,它還支持鏈式編程,讓代碼更加簡潔。如果你正在使用 Mybatis 進行數據庫操作,那么不妨嘗試一下 Fluent Mybatis,它或許會給你帶來更好的開發體驗。vVh28資訊網——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-79451-0.html你還在用Mybatis?試試這款神器,縱享絲滑

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 一個提出五年的 Node.js 模塊問題,終被解決!

下一篇: 20k級別前端是怎么使用LocalStorage的,想知道嗎?

標簽:
  • 熱門焦點
Top