我正在浏览一份文件而且我遇到了一个叫做的术语DAO
.我发现它是一个数据访问对象.有人可以解释一下这究竟是什么吗?
我知道它是某种用于访问来自不同类型数据源的数据的接口,在我的这个小小的研究中,我碰到了一个名为数据源或数据源对象的概念,事情在我的脑海中搞砸了.
我真的想知道DAO
在使用它的位置方面是什么.如何使用?任何从非常基本的东西解释这个概念的页面的链接也是值得赞赏的.
Ram*_*ami 414
数据访问对象基本上是一个对象或接口,提供对底层数据库或任何其他持久性存储的访问.
该定义来自:http: //en.wikipedia.org/wiki/Data_access_object
另请参阅此处的序列图:http: //www.oracle.com/technetwork/java/dataaccessobject-138824.html
也许一个简单的例子可以帮助你理解这个概念:
假设我们有一个代表员工的实体:
public class Employee {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
员工实体将持久保存到Employee
数据库中的相应表中.一个简单的DAO接口来处理操作员工实体所需的数据库操作,如下所示:
interface EmployeeDAO {
List<Employee> findAll();
List<Employee> findById();
List<Employee> findByName();
boolean insertEmployee(Employee employee);
boolean updateEmployee(Employee employee);
boolean deleteEmployee(Employee employee);
}
Run Code Online (Sandbox Code Playgroud)
接下来,我们必须为该接口提供一个具体的实现来处理SQL服务器,另一个用于处理平面文件等.
Vde*_*deX 77
什么是数据访问对象(DAO) -
它是一个对象/接口,用于从数据存储数据库访问数据.
为什么我们使用DAO:
它抽象从数据资源(如数据库)中检索数据.这个概念是"将数据资源的客户端接口与其数据访问机制分开".
直接访问数据的问题是数据源可能会发生变化.例如,考虑您的应用程序部署在访问Oracle数据库的环境中.然后将其部署到使用Microsoft SQL Server的环境中.如果您的应用程序使用存储过程和特定于数据库的代码(例如生成数字序列),那么如何在应用程序中处理它?您有两种选择:
它全部称为DAO模式,它包括以下内容:
请查看此示例,这将更清楚地清楚.
示例
我假设这些事情必须在一定程度上清除您对DAO的理解.
我将是通用的,而不是特定于 Java 的,因为 DAO 和 ORM 用于所有语言。
要了解 DAO,您首先需要了解 ORM(对象关系映射)。这意味着,如果您有一个名为“person”的表,其中包含“name”和“age”列,那么您将为该表创建对象模板:
type Person {
name
age
}
Run Code Online (Sandbox Code Playgroud)
现在在 DAO 的帮助下,而不是编写一些特定的查询,来获取所有人,对于您正在使用的任何类型的数据库(可能容易出错),您可以这样做:
list persons = DAO.getPersons();
...
person = DAO.getPersonWithName("John");
age = person.age;
Run Code Online (Sandbox Code Playgroud)
您不会自己编写 DAO 抽象,它通常是某个开源项目的一部分,具体取决于您使用的语言和框架。
现在到这里的主要问题。“ .. 在哪里使用它... ”。通常,如果您正在编写复杂的业务和特定领域的代码,如果没有 DAO,您的生活将非常困难。当然,您不需要使用提供的 ORM 和 DAO,而是可以编写自己的抽象和本机查询。我过去曾这样做过,后来几乎总是后悔。
例如我们有一些实体组。
对于这个实体,我们创建存储库 GroupRepository。
public interface GroupRepository extends JpaRepository<Group, Long> {
}
Run Code Online (Sandbox Code Playgroud)
然后我们需要创建一个服务层来使用这个存储库。
public interface Service<T, ID> {
T save(T entity);
void deleteById(ID id);
List<T> findAll();
T getOne(ID id);
T editEntity(T entity);
Optional<T> findById(ID id);
}
public abstract class AbstractService<T, ID, R extends JpaRepository<T, ID>> implements Service<T, ID> {
private final R repository;
protected AbstractService(R repository) {
this.repository = repository;
}
@Override
public T save(T entity) {
return repository.save(entity);
}
@Override
public void deleteById(ID id) {
repository.deleteById(id);
}
@Override
public List<T> findAll() {
return repository.findAll();
}
@Override
public T getOne(ID id) {
return repository.getOne(id);
}
@Override
public Optional<T> findById(ID id) {
return repository.findById(id);
}
@Override
public T editEntity(T entity) {
return repository.saveAndFlush(entity);
}
}
@org.springframework.stereotype.Service
public class GroupServiceImpl extends AbstractService<Group, Long, GroupRepository> {
private final GroupRepository groupRepository;
@Autowired
protected GroupServiceImpl(GroupRepository repository) {
super(repository);
this.groupRepository = repository;
}
}
Run Code Online (Sandbox Code Playgroud)
在控制器中我们使用这个服务。
@RestController
@RequestMapping("/api")
class GroupController {
private final Logger log = LoggerFactory.getLogger(GroupController.class);
private final GroupServiceImpl groupService;
@Autowired
public GroupController(GroupServiceImpl groupService) {
this.groupService = groupService;
}
@GetMapping("/groups")
Collection<Group> groups() {
return groupService.findAll();
}
@GetMapping("/group/{id}")
ResponseEntity<?> getGroup(@PathVariable Long id) {
Optional<Group> group = groupService.findById(id);
return group.map(response -> ResponseEntity.ok().body(response))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
@PostMapping("/group")
ResponseEntity<Group> createGroup(@Valid @RequestBody Group group) throws URISyntaxException {
log.info("Request to create group: {}", group);
Group result = groupService.save(group);
return ResponseEntity.created(new URI("/api/group/" + result.getId()))
.body(result);
}
@PutMapping("/group")
ResponseEntity<Group> updateGroup(@Valid @RequestBody Group group) {
log.info("Request to update group: {}", group);
Group result = groupService.save(group);
return ResponseEntity.ok().body(result);
}
@DeleteMapping("/group/{id}")
public ResponseEntity<?> deleteGroup(@PathVariable Long id) {
log.info("Request to delete group: {}", id);
groupService.deleteById(id);
return ResponseEntity.ok().build();
}
}
Run Code Online (Sandbox Code Playgroud)