av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

MyBatis Plus 入門使用詳細教程

瀏覽:92日期:2023-10-23 07:33:54

一、MyBatis Plus 介紹

MyBatis Plus 是國內人員開發的 MyBatis 增強工具,在 MyBatis 的基礎上只做增強不做改變,為簡化開發、提高效率而生。

MyBatis Plus 的核心功能有:支持通用的 CRUD、代碼生成器與條件構造器。

通用 CRUD:定義好 Mapper 接口后,只需要繼承 BaseMapper<T> 接口即可獲得通用的增刪改查功能,無需編寫任何接口方法與配置文件條件構造器:通過 EntityWrapper<T> (實體包裝類),可以用于拼接 SQL 語句,并且支持排序、分組查詢等復雜的 SQL代碼生成器:支持一系列的策略配置與全局配置,比 MyBatis 的代碼生成更好用

BaseMapper<T> 接口中通用的 CRUD 方法:

MyBatis Plus 入門使用詳細教程

二、MyBatis Plus 集成 Spring

數據表結構

DROP TABLE IF EXISTS `tbl_employee`;CREATE TABLE `tbl_employee` ( `id` int(11) NOT NULL AUTO_INCREMENT, `last_name` varchar(50) DEFAULT NULL, `email` varchar(50) DEFAULT NULL, `gender` char(1) DEFAULT NULL, `age` int(11) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

pom 文件

<dependencies> <!-- MP --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>2.3</version> </dependency> <!-- 測試 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!-- 數據源 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <!-- 數據庫驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> <!-- Spring 相關 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.3.9.RELEASE</version> </dependency> </dependencies>

MyBatis 全局配置文件 mybatis-config.xml

<?xml version='1.0' encoding='UTF-8' ?><!DOCTYPE configuration PUBLIC '-//mybatis.org//DTD Config 3.0//EN' 'http://mybatis.org/dtd/mybatis-3-config.dtd'><!-- 不作任何配置 --><configuration />

數據源 db.properties

jdbc.url=jdbc:mysql://localhost:3306/mpjdbc.username=rootjdbc.password=1234

Spring 配置文件 applicationContext.xml

<!-- 數據源 --> <context:property-placeholder location='classpath:db.properties'/> <bean class='com.alibaba.druid.pool.DruidDataSource'> <property name='url' value='${jdbc.url}'></property> <property name='username' value='${jdbc.username}'></property> <property name='password' value='${jdbc.password}'></property> </bean> <!-- MP 提供的 MybatisSqlSessionFactoryBean --> <bean class='com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean'> <!-- 數據源 --> <property name='dataSource' ref='dataSource'></property> <!-- mybatis 全局配置文件 --> <property name='configLocation' value='classpath:mybatis-config.xml'></property> <!-- 別名處理 --> <property name='typeAliasesPackage' value='com.jas.bean'></property> <!-- 注入全局MP策略配置 --> <property name='globalConfig' ref='globalConfiguration'></property> <!-- 插件注冊 --> <property name='plugins'> <list> <!-- 注冊分頁插件 --> <bean /> <!-- 注入 SQL 性能分析插件,建議在開發環境中使用,可以在控制臺查看 SQL 執行日志 --> <bean class='com.baomidou.mybatisplus.plugins.PerformanceInterceptor'> <property name='maxTime' value='1000' /> <!--SQL 是否格式化 默認false--> <property name='format' value='true' /> </bean> </list> </property> </bean> <!-- 定義 MybatisPlus 的全局策略配置--> <bean class='com.baomidou.mybatisplus.entity.GlobalConfiguration'> <!-- 在 2.3 版本以后,dbColumnUnderline 默認值是 true --> <property name='dbColumnUnderline' value='true'></property> <!-- 全局的主鍵策略 --> <property name='idType' value='0'></property> <!-- 全局的表前綴策略配置 --> <property name='tablePrefix' value='tbl_'></property> </bean> <!-- 配置mybatis 掃描mapper接口的路徑 --> <bean class='org.mybatis.spring.mapper.MapperScannerConfigurer'> <property name='basePackage' value='com.jas.mapper'></property> </bean>

三、快速體驗 MyBatis Plus

實體類 Employee

@TableName(value = 'tbl_employee')public class Employee { @TableId(value = 'id', type = IdType.AUTO) private Integer id; @TableField(value = 'last_name') private String lastName; private String email; private Integer gender; private Integer age; public Employee() { super(); } public Employee(Integer id, String lastName, String email, Integer gender, Integer age) { this.id = id; this.lastName = lastName; this.email = email; this.gender = gender; this.age = age; } // 省略 set、get 與 toString() 方法

mapper 接口

/*** 不定義任何接口方法*/public interface EmployeeMapper extends BaseMapper<Employee> {}

在測試類中生成測試的 mapper 對象

private ApplicationContext context = new ClassPathXmlApplicationContext('classpath:applicationContext.xml'); private EmployeeMapper employeeMapper = context.getBean('employeeMapper', EmployeeMapper.class);

簡單查詢測試

@Test public void getEmpByIdTest() { Employee employee = employeeMapper.selectById(1); System.out.println(employee); }

分頁查詢測試

@Test public void getEmpByPage() { Page<?> page = new Page<>(1, 5); List<Employee> list = employeeMapper.selectPage(page, null); System.out.println('總記錄數:' + page.getTotal()); System.out.println('總頁數' + page.getPages()); System.out.println(list); }

條件構造器測試

@Test public void getEmpByName() { EntityWrapper<Employee> wrapper = new EntityWrapper<>(); // ’last_name’ 與 ’age’ 對應數據庫中的字段 wrapper.like('last_name', '張'); wrapper.eq('age', 20); List<Employee> list = employeeMapper.selectList(wrapper); System.out.println(list); }

控制臺輸出的 SQL 分析日志

MyBatis Plus 入門使用詳細教程

上面幾個例子中,并沒有在 EmployeeMapper 接口中定義任何方法,也沒有在配置文件中編寫 SQL 語句,而是通過繼承 BaseMapper<T> 接口獲得通用的的增刪改查方法,復雜的 SQL 也可以使用條件構造器拼接。

通過這兩種方式已經能夠滿足很多的開發需求了,不過復雜的業務需求還是要編寫 SQL 語句的,流程和 MyBatis 一樣。

PS:完整的代碼(mybatis-plus-demo 目錄)傳到了 GitHub,包括 SQL 表結構與數據文件,點我前往~

總結

標簽: Mybatis 數據庫
相關文章:
主站蜘蛛池模板: 日韩精品一区在线观看 | 中文字幕免费在线观看 | 欧美一级免费片 | 天天影视亚洲综合网 | 免费一二区| 精品福利在线 | 国产综合久久久 | 亚洲精品久久区二区三区蜜桃臀 | 91精品国产91久久久久久最新 | 日本午夜免费福利视频 | 国产精品国产成人国产三级 | 亚洲一区二区三区免费观看 | 美女艹b | 精品一区二区三区在线观看 | 一级欧美一级日韩片免费观看 | 男人天堂网址 | av日韩在线播放 | 精品国产乱码一区二区三 | 天天操天天拍 | 福利av在线 | 亚洲精品天堂 | 男女污污网站 | 天天干b | 97视频在线免费 | 91精品国产综合久久精品图片 | 日韩精品激情 | 亚洲免费精品一区 | 男女网站在线观看 | 精品欧美一区二区在线观看欧美熟 | 亚洲一区二区三区免费视频 | 亚洲国产精品日本 | 精品国产一区二区国模嫣然 | 国产精品一级在线观看 | 久久久久久亚洲精品 | 亚洲一区 中文字幕 | 国产91综合一区在线观看 | 一二三四在线视频观看社区 | 久久99国产精一区二区三区 | 99视频精品 | 97av视频在线 | 国产9999精品 |