大学 英文网站建设,长沙优化网站推广,wordpress seo,免费版网站建设合同在Spring Boot中#xff0c;DAO是数据访问对象的缩写#xff0c;它是一种设计模式用于提供对数据库操作的抽象层。通过使用DAO模式#xff0c;我们可以将数据操作与业务逻辑分离#xff0c;并提供一个单独的接口来执行所有的数据库操作。 在Spring Boot中#xff0c;通常使… 在Spring Boot中DAO是数据访问对象的缩写它是一种设计模式用于提供对数据库操作的抽象层。通过使用DAO模式我们可以将数据操作与业务逻辑分离并提供一个单独的接口来执行所有的数据库操作。 在Spring Boot中通常使用Spring Data JPA来实现DAO。Spring Data JPA是Spring Framework的子项目之一用于简化JPAJava Persistence API的开发。Spring Data JPA提供了一些默认实现如自动生成常见的SQL查询等。 要使用DAO首先需要创建一个DAO接口并继承自Spring Data JPA提供的CrudRepository或JpaRepository接口。然后定义该接口所代表的实体类及其主键类型。例如 public interface UserRepository extends JpaRepositoryUser, Long {// 自定义查询方法ListUser findByLastName(String lastName);
} 在上面的示例中UserRepository是一个DAO接口其中User是实体类Long是主键类型。findByLastName是一个自定义的查询方法。 最后在Service或Controller中注入该DAO接口并调用其方法即可完成对数据库的操作。 确保com.example.app.dao.ILoginDao接口已经被定义为一个Bean并被Spring容器管理了。可以通过在该接口上加上Component注解或者在配置文件中使用bean标签来将它声明为一个bean。确保com.example.app.service.impl.LoginService类已经被Spring容器管理了。可以在该类上加上Service注解或者在配置文件中使用bean标签来将它声明为一个bean。确保在com.example.app.service.impl.LoginService类中已经正确地注入了com.example.app.dao.ILoginDao类型的bean。可以在该字段上加上Autowired注解或者在构造函数中进行注入。 在Spring Boot中DAOData Access Object是一种设计模式它提供了一个抽象层来访问数据库。使用DAO模式能够将数据操作与业务逻辑分离并提供统一的接口来执行所有的数据库操作。Spring Boot通过整合Spring Data JPA和MyBatis等ORM框架来实现DAO功能。 下面是一个使用Spring Data JPA实现DAO的示例 1.创建一个实体类User用来映射数据库表的字段 Entity
Table(name users)
public class User {IdGeneratedValue(strategy GenerationType.IDENTITY)private Long id;Column(nullable false, unique true)private String username;Column(nullable false)private String password;Column(nullable false)private String email;// getters and setters
} 2.创建一个继承自JpaRepository的接口UserRepository用于定义User实体的数据访问接口 Repository
public interface UserRepository extends JpaRepositoryUser, Long {OptionalUser findByUsername(String username);Boolean existsByUsername(String username);Boolean existsByEmail(String email);
} 3.在Service层中使用UserRepository Service
public class UserService {Autowiredprivate UserRepository userRepository;public User getUserById(Long id) {return userRepository.findById(id).orElseThrow(() - new RuntimeException(User not found));}public User saveUser(User user) {return userRepository.save(user);}public void deleteUser(Long userId) {userRepository.deleteById(userId);}
} 在这个示例中UserService类注入了一个UserRepository的实例并使用内置的方法来实现对用户数据的增删改查操作。 4.在Controller层中调用UserService RestController
RequestMapping(/api/users)
public class UserController {Autowiredprivate UserService userService;GetMapping(/{id})public ResponseEntityUser getUserById(PathVariable Long id) {User user userService.getUserById(id);return ResponseEntity.ok(user);}PostMappingpublic ResponseEntityUser createUser(RequestBody User user) {User savedUser userService.saveUser(user);return ResponseEntity.status(HttpStatus.CREATED).body(savedUser);}DeleteMapping(/{id})public ResponseEntityVoid deleteUser(PathVariable Long id) {userService.deleteUser(id);return ResponseEntity.noContent().build();}
} 在这个示例中UserController通过注入UserService实例来访问和操作用户数据。其中GetMapping、PostMapping和DeleteMapping注解分别对应HTTP GET、POST和DELETE请求Spring Boot会自动将JSON格式的请求体解析成Java对象。 综上所述使用DAO模式可以将数据操作与业务逻辑分离并提供统一的接口来执行所有的数据库操作。在Spring Boot中我们可以使用ORM框架如Spring Data JPA和MyBatis来实现DAO功能。 beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsdbean idloginDao classcom.example.app.dao.LoginDaoImpl//beans Autowired(requiredtrue)注解表示该属性必须在Spring容器中找到对应的bean进行注入如果找不到就会抛出异常。如果想要取消这个强制依赖关系可以将required属性设置为false。 DefaultResultSetHandler.instantiateCollectionPropertyIfAppropriate() 是 MyBatis 中用于实例化集合类型属性的方法。当查询语句返回多条记录时MyBatis 会调用该方法将结果集封装到一个集合对象中以便后续处理。 具体来说该方法首先会检查传入参数 resultContext 是否包含集合类型属性并且该属性为 null 或空集合。如果是则会通过反射创建一个新的集合对象并将其赋值给对应的属性。这个集合对象的类型由 resultMap 中定义的集合类型指定。如果 resultMap 中没有指定集合类型则默认使用 java.util.ArrayList。 需要注意的是该方法仅在 resultType 为集合类型例如 List、Set 等时才会被调用。如果 resultType 是其他类型如自定义 Java 对象则不会调用该方法。 假设我们有一个 User 类其中包含一个名为 orders 的属性表示用户的订单列表 public class User {private Integer id;private String name;private ListOrder orders;// getters and setters
}public class Order {private Integer id;private String product;private Integer quantity;// getters and setters
} 在 MyBatis 中我们可以编写如下的查询语句来查询用户及其订单 select idgetUserWithOrders resultMapuserResultMapSELECT u.*, o.id AS order_id, o.product, o.quantityFROM user uLEFT JOIN order o ON u.id o.user_idWHERE u.id #{id}
/select 这里使用了 LEFT JOIN 来将用户和订单关联起来查询结果中可能包含多条记录。在对这个查询结果进行映射时MyBatis 会通过以下步骤处理集合类型属性 orders 在 userResultMap 中我们需要使用 collection 元素来指定如何映射 orders 属性 resultMap iduserResultMap typeUserid propertyid columnid/result propertyname columnname/collection propertyorders ofTypeOrderid propertyid columnorder_id/result propertyproduct columnproduct/result propertyquantity columnquantity//collection
/resultMap Initializing Spring DispatcherServlet dispatcherServlet是Spring框架在启动时输出的日志信息之一。它表示正在初始化Spring MVC中的DispatcherServlet即Web请求的入口点。 DispatcherServlet是Spring MVC模式中的核心组件之一。它负责接收来自客户端的HTTP请求并将请求分派到合适的控制器(Controller)进行处理。在Spring框架中可以通过配置多个DispatcherServlet实例来处理不同URL模式的请求。 使用Component注解使用Component注解可以将一个类声明为组件并且告诉Spring容器需要将它实例化为一个bean。使用这个注解后可以在其他组件中通过Autowired来引用该bean。使用Configuration和Bean注解使用Configuration注解可以将一个类声明为配置类表示它会定义一组Bean。通过在方法上添加Bean注解可以将该方法返回的对象声明为一个BeanSpring容器就会使用该方法创建并管理该Bean。使用Import注解使用Import注解可以将其他类或配置类导入到当前配置类中。通过这种方式可以将其他Java类或配置类中定义的Bean添加到当前应用程序上下文中。使用Conditional注解使用Conditional注解可以根据条件仅仅创建某些bean。当特定条件满足时该bean才会被创建并添加到应用程序上下文中。使用FactoryBean接口实现FactoryBean接口可以创建一个工厂类用于创建其他Bean的实例。FactoryBean接口提供了标准的生命周期回调方法因此可以控制Bean的创建过程。 注解 SpringBootApplication 用于启动Spring Boot应用程序、MapperScan(com.example.app.dao) 用于扫描Mybatis Mapper接口并生成代理对象、ImportResource(classpath:applicationContext.xml) 用于加载XML配置文件。 另外该类还继承了 SpringBootServletInitializer 并重写了它的 configure() 方法这表明正在使用 WAR 部署方式来部署的应用程序。 在MyBatis框架中MapperScan(com.example.app.dao)和MapperScan(com.example.app.mapper)注解的作用是扫描指定包下的Java接口将其注册为MyBatis的Mapper接口。 通常情况下这两个注解没有本质上的区别只是命名不同而已。开发者可以根据自己的实际业务需求来选择更加合适的命名方式以方便阅读和维护代码。 需要注意的是使用时应该确保注解中的包路径与Mapper接口所在的包路径相匹配否则可能会导致Mapper接口无法被正确扫描和注册。 将 com.example.app.entity.Login 类型的对象强制转换为 com.alibaba.fastjson.JSONObject 类型但是这两个类不能直接互相转换导致了异常。 要修复这个问题需要检查代码中涉及到这两个类的部分看看是否错误地进行了类型转换或者使用了错误的数据类型。可能需要修改代码以确保正确的类型转换才能够顺利执行。 加群联系作者vxxiaoda0423 仓库地址https://github.com/webVueBlog/JavaGuideInterview