下面是一个使用Spring Boot、MyBatis Plus和Elasticsearch的简单示例:
- 首先,在pom.xml文件中添加以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>6.5.20</version>
</dependency>
- 创建一个Spring Boot应用程序:
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
- 在application.properties文件中配置Elasticsearch:
# Elasticsearch连接配置
spring.data.elasticsearch.cluster-name=elasticsearch-cluster
spring.data.elasticsearch.cluster-nodes=localhost:9200
spring.data.elasticsearch.repositories.enabled=true
spring.data.elasticsearch.repositories.bootstrap-mode=local
spring.data.elasticsearch.repositories.type=elasticsearch
spring.data.elasticsearch.repositories.uri=http://localhost:9200
spring.data.elasticsearch.repositories.settings.number_of_shards=3
spring.data.elasticsearch.repositories.settings.number_of_replicas=1
spring.data.elasticsearch.repositories.settings.index.refresh_interactions=10s
spring.data.elasticsearch.repositories.settings.index.refresh_interval=1s
spring.data.elasticsearch.repositories.settings.index.lifecycle.name=my-index
spring.data.elasticsearch.repositories.settings.index.lifecycle.type=delete
spring.data.elasticsearch.repositories.settings.index.lifecycle.rollover.alias=rollover
spring.data.elasticsearch.repositories.settings.index.lifecycle.rollover.max_age=1d
spring.data.elasticsearch.repositories.settings.index.lifecycle.rollover.actions=create,delete
spring.data.elasticsearch.repositories.settings.index.lifecycle.rollover.strategy.delete.type=delete
spring.data.elasticsearch.repositories.settings.index.lifecycle.rollover.strategy.delete.base=0
spring.data.elasticsearch.repositories.settings.index.lifecycle.rollover.strategy.delete.max_size=10g
spring.data.elasticsearch.repositories.settings.index.lifecycle.rollover.strategy.delete.max_age=1d
#MyBatis配置
mybatis-plus.mapper-locations=classpath:mapper/*.xml
mybatis-plus.type-aliases-package=com.example.entity
spring.data.elasticsearch.cluster-name:设置Elasticsearch集群的名称。
spring.data.elasticsearch.cluster-nodes:设置Elasticsearch集群中的节点地址。
spring.data.elasticsearch.repositories.enabled:设置是否启用Elasticsearch数据源。
spring.data.elasticsearch.repositories.bootstrap-mode:设置Elasticsearch数据源的启动方式。
spring.data.elasticsearch.repositories.type:设置Elasticsearch数据源的类型。
spring.data.elasticsearch.repositories.uri:设置Elasticsearch数据源的地址。
spring.data.elasticsearch.repositories.settings.number_of_shards:设置Elasticsearch数据源的分片数。
spring.data.elasticsearch.repositories.settings.number_of_replicas:设置Elasticsearch数据源的副本数。
spring.data.elasticsearch.repositories.settings.index.refresh_interactions:设置Elasticsearch数据源的实时更新时间间隔。
spring.data.elasticsearch.repositories.settings.index.refresh_interval:设置Elasticsearch数据源的更新时间间隔。
spring.data.elasticsearch.repositories.settings.index.lifecycle.name:设置Elasticsearch数据源的生命周期名称。
spring.data.elasticsearch.repositories.settings.index.lifecycle.type:设置Elasticsearch数据源的生命周期类型。
spring.data.elasticsearch.repositories.settings.index.lifecycle.rollover.alias:设置Elasticsearch数据源的自动轮换别名。
spring.data.elasticsearch.repositories.settings.index.lifecycle.rollover.max_age:设置Elasticsearch数据源的最大自动轮换年限。
spring.data.elasticsearch.repositories.settings.index.lifecycle.rollover.actions:设置Elasticsearch数据源的自动轮换动作类型。
spring.data.elasticsearch.repositories.settings.index.lifecycle.rollover.strategy.delete.type:设置Elasticsearch数据源的删除类型。
spring.data.elasticsearch.repositories.settings.index.lifecycle.rollover.strategy.delete.base:设置Elasticsearch数据源的删除基础地址。
spring.data.elasticsearch.repositories.settings.index.lifecycle.rollover.strategy.delete.max_size:设置Elasticsearch数据源的最大删除大小。
spring.data.elasticsearch.repositories.settings.index.lifecycle.rollover.strategy.delete.max_age:设置Elasticsearch数据源的最大删除年限。
- 创建一个实体类:
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String password;
private Integer age;
//getter and setter methods
}
- 创建一个Mapper接口:
public interface UserMapper {
User findById(Long id);
List<User> findAll();
int deleteById(Long id);
}
- 创建一个Mapper实现类:
@Mapper
public interface UserMapperImpl extends BaseMapper<User> {
User findById(Long id);
List<User> findAll();
int deleteById(Long id);
}
- 创建一个UserService实现类:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User findById(Long id) {
return userMapper.findById(id);
}
@Override
public List<User> findAll() {
return userMapper.findAll();
}
@Override
public int deleteById(Long id) {
return userMapper.deleteById(id);
}
}
- 创建一个UserController实现类:
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/user/{id}")
public ResponseEntity<User> getUserById(@PathVariable Long id) {
User user = userService.findById(id);
if (user == null) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(user);
}
@PostMapping("/user")
public ResponseEntity<User> createUser(@RequestBody User user) {
userService.save(user);
return ResponseEntity.ok(user);
}
@DeleteMapping("/user/{id}")
public ResponseEntity<Void> deleteUser(@PathVariable Long id) {
userService.deleteById(id);
return ResponseEntity.ok().build();
}
}
- 创建一个UserServiceImpl实现类:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User findById(Long id) {
return userMapper.findById(id);
}
@Override
public List<User> findAll() {
return userMapper.findAll();
}
@Override
public int deleteById(Long id) {
return userMapper.deleteById(id);
}
@Override
public User save(User user) {
return userMapper.insert(user);
}
@Override
public void update(User user) {
userMapper.updateByPrimaryKey(user);
}
@Override
public void delete(User user) {
userMapper.deleteByPrimaryKey(user);
}
}
以上就是一个简单的Spring Boot、MyBatis Plus和Elasticsearch的示例。在这个示例中,我们创建了一个User实体类,一个UserMapper接口和一个UserService实现类来处理用户的CRUD操作。我们还配置了MyBatis Plus和Elasticsearch,并创建了一个UserController来提供RESTful API来操作用户。