有哪些做课件的网站,怎么制作公众号推送,深圳公司网站制作,上海亿网站建设精心整理了最新的面试资料和简历模板#xff0c;有需要的可以自行获取
点击前往百度网盘获取 点击前往夸克网盘获取 一、环境准备
JDK 17Maven 3.8Spring Boot 3.2ArangoDB 3.11#xff08;本地安装或Docker运行#xff09;
Docker启动ArangoDB
docker run -d --name ar…精心整理了最新的面试资料和简历模板有需要的可以自行获取
点击前往百度网盘获取 点击前往夸克网盘获取 一、环境准备
JDK 17Maven 3.8Spring Boot 3.2ArangoDB 3.11本地安装或Docker运行
Docker启动ArangoDB
docker run -d --name arangodb \-p 8529:8529 \-e ARANGO_ROOT_PASSWORDrootpassword \arangodb:latest二、创建Spring Boot项目
使用start.spring.io创建项目添加依赖 Spring WebLombok
三、添加ArangoDB依赖
!-- pom.xml --
dependencygroupIdcom.arangodb/groupIdartifactIdarangodb-spring-data/artifactIdversion3.8.0/version
/dependency四、配置ArangoDB连接
# application.yml
arangodb:host: 127.0.0.1port: 8529user: rootpassword: rootpassworddatabase: spring_db创建配置类
Configuration
EnableArangoRepositories(basePackages com.example.repository)
public class ArangoConfig {Value(${arangodb.host})private String host;Value(${arangodb.port})private int port;Value(${arangodb.user})private String user;Value(${arangodb.password})private String password;Value(${arangodb.database})private String database;Beanpublic ArangoDB.Builder arangoBuilder() {return new ArangoDB.Builder().host(host, port).user(user).password(password);}Beanpublic ArangoDatabase arangoDatabase() {return arangoBuilder().build().db(database);}
}五、创建实体类
Document(users)
Data
NoArgsConstructor
AllArgsConstructor
public class User {Idprivate String id;ArangoIdprivate String arangoId;private String name;private String email;private Integer age;
}六、创建Repository接口
public interface UserRepository extends ArangoRepositoryUser, String {// 自定义查询方法Query(FOR u IN users FILTER u.age 0 RETURN u)ListUser findByAgeGreaterThanEqual(int age);
}七、实现Service层
Service
RequiredArgsConstructor
public class UserService {private final UserRepository userRepository;public User createUser(User user) {return userRepository.save(user);}public ListUser getAllUsers() {return (ListUser) userRepository.findAll();}public ListUser getUsersByAge(int age) {return userRepository.findByAgeGreaterThanEqual(age);}
}八、创建REST控制器
RestController
RequestMapping(/api/users)
RequiredArgsConstructor
public class UserController {private final UserService userService;PostMappingpublic ResponseEntityUser createUser(RequestBody User user) {return ResponseEntity.ok(userService.createUser(user));}GetMappingpublic ResponseEntityListUser getAllUsers() {return ResponseEntity.ok(userService.getAllUsers());}GetMapping(/age/{age})public ResponseEntityListUser getUsersByAge(PathVariable int age) {return ResponseEntity.ok(userService.getUsersByAge(age));}
}九、测试验证
启动Spring Boot应用使用Postman测试 POST /api/users{name: John Doe,email: johnexample.com,age: 28
}GET /api/users 查看所有用户GET /api/users/age/25 查询年龄≥25的用户
十、高级查询示例
// 在Repository中添加
Query(FOR u IN users FILTER u.name name RETURN u)
ListUser findByName(Param(name) String name);// 复杂查询示例
Query(FOR u IN users FILTER u.age minAge AND u.age maxAge RETURN u)
ListUser findByAgeRange(Param(minAge) int minAge, Param(maxAge) int maxAge);十一、事务管理
Autowired
private ArangoDatabase arangoDatabase;public void transactionalOperation() {arangoDatabase.transaction(users, Collections.emptyMap(),trx - {User user1 new User(Alice, aliceexample.com, 30);User user2 new User(Bob, bobexample.com, 25);trx.collection(users).insertDocument(user1);trx.collection(users).insertDocument(user2);return null;},Void.class);
}十二、常见问题
连接失败检查防火墙设置和ArangoDB日志版本兼容性确保ArangoDB服务端与Java驱动版本匹配AQL语法错误使用ArangoDB Web界面http://localhost:8529调试查询