mysql +mybatis下全文搜索的使用

第一步在你的需要全文搜索的列上点击 全文搜索full text类型索引,注意我的例子是该列是个longText类型的列

第一步建立fulltext类型索引如下图

索引类型选择full text

第二步:

验证 全文搜索sql好不好使

 第三步:

我用的是mybatis框架,改自动生成的example条件设置和mapper.xml文件内容

 

 

 

Page<TLog> page = (Page<TLog>) logMapper.selectByExample(tLogExample);

最后mybatis框架生成的sql语句基本上就是这样 利用了全文搜索语法

注意mysql中利用全文搜索功能的前提就是在要全文搜索的列上   建立full text类型索引

最后mybatis框架生成的sql语句基本上就是这样 利用了全文搜索语法

select id, log_type, system_name, visit_time,message from t_log WHERE ( MATCH (message) AGAINST ('*FA9X0D00000001*' IN BOOLEAN MODE ) ) 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以为你解答。以下是一个简单的实现步骤: 1. 创建数据库表 首先,你需要在 MySQL 数据库中创建一个表来存储搜索数据。例如,可以创建一个名为 `product` 的表,包含 `id`、`name` 和 `description` 三个字段。 ```sql CREATE TABLE product ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255), description TEXT ); ``` 2. 创建后端服务 使用 Spring Boot 和 MyBatis 框架来创建一个后端服务,可以接收搜索请求并从数据库中获取数据。具体实现步骤如下: - 添加相关依赖 在 `pom.xml` 文件中添加 MyBatisMySQL 相关依赖: ```xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.25</version> </dependency> ``` - 配置数据库连接 在 `application.properties` 中添加数据库连接配置: ``` spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` - 创建数据模型 创建一个名为 `Product` 的 Java 类,用于映射数据库表中的数据: ```java public class Product { private Integer id; private String name; private String description; // 省略 getter 和 setter 方法 } ``` - 创建 MyBatis 映射文件 创建一个名为 `ProductMapper.xml` 的 MyBatis 映射文件,用于定义 SQL 语句和数据模型的映射关系: ```xml <mapper namespace="com.example.demo.mapper.ProductMapper"> <select id="search" resultType="com.example.demo.model.Product"> SELECT * FROM product WHERE name LIKE CONCAT('%', #{keyword}, '%') </select> </mapper> ``` - 创建数据访问层 创建一个名为 `ProductMapper` 的 Java 接口,用于定义数据访问方法: ```java @Mapper public interface ProductMapper { List<Product> search(String keyword); } ``` - 创建控制器 创建一个名为 `ProductController` 的 Spring MVC 控制器,用于接收搜索请求和返回搜索结果: ```java @RestController public class ProductController { @Autowired private ProductMapper productMapper; @GetMapping("/search") public List<Product> search(@RequestParam("keyword") String keyword) { return productMapper.search(keyword); } } ``` 至此,后端服务的实现就完成了。 3. 创建前端界面 在前端界面中,可以使用 Vue 和 ElementUI 的 `el-input` 和 `el-table` 组件来实现搜索和显示数据的功能。例如,可以实现如下代码: ```html <template> <div> <el-input v-model="keyword" placeholder="请输入搜索关键字" @keyup.enter.native="search" /> <el-table :data="data"> <el-table-column prop="name" label="名称" /> <el-table-column prop="description" label="描述" /> </el-table> </div> </template> <script> export default { data() { return { keyword: '', data: [] }; }, methods: { async search() { const { data } = await this.$http.get('/search', { params: { keyword: this.keyword } }); this.data = data; } } }; </script> ``` 这段代码会创建一个包含搜索输入框和显示搜索结果的界面,当用户输入关键字并按下回车键时,会向后端发送搜索请求,并将结果显示在表格中。 以上就是一个简单的使用 Spring Boot、MyBatis、Vue 和 ElementUI 来创建搜索功能的实现步骤。当然,具体实现方式还需要根据你的具体需求来进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值