Springboot使用java注入方式第一种

日期:2019-10-25

早上9:36开始写

第一步:数据引用依赖

  <!--引入Druid连接池依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.6</version>
        </dependency>

第二步:创建配置文件

在解决端口冲突的文件里面添加上这么一句话解决问题

server.port=8090

第三步:读取配置文件

 
package com.liheng.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;

@Configuration
@PropertySource("classpath:jdbc.properties")
public class jdbcConfig {
    @Value("${jdbc.driverClassName}")
    String driverClassName;
    @Value("${jdbc.url}")
    String url;
    @Value("${jdbc.username}")
    String username;
    @Value("${jdbc.password}")
    String password;
    @Bean
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return  dataSource;
    }
}

第四步:添加java文件

 @Autowired
    public DataSource dataSource;

第五步:DUG运行断点

此篇完结

对了 早上报了一个错误

Disconnected from the target VM, address: '127.0.0.1:63165', transport: 'socket'。

第一:要注意创建java类的文件区分大小写

第二:要注意注解是否没有引入:(我就是没有引入注解@RestControoler)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,这是一个简单的分页查询的例子: 首先,你需要引入以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 然后,在你的实体类中添加分页相关的注解: ``` import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository<User, Long> { Page<User> findByName(String name, Pageable pageable); } ``` 在你的控制器中,你可以这样写: ``` import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @Autowired private UserRepository userRepository; @GetMapping("/users") public Page<User> getUsers(@RequestParam(value = "page", defaultValue = "0") int page, @RequestParam(value = "size", defaultValue = "10") int size) { Sort sort = Sort.by(Sort.Direction.ASC, "id"); Pageable pageable = PageRequest.of(page, size, sort); return userRepository.findAll(pageable); } } ``` 这样,你就可以使用 `/users` 接口来进行分页查询了。你可以通过调整 `page` 和 `size` 参数来控制分页的相关信息。 希望这对你有帮助。 ### 回答2: 使用Java Spring Boot编写分页查询可以通过以下步骤实现: 1. 导入所需的依赖:在pom.xml文件中添加Spring Boot和Spring Data JPA的依赖项,例如: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> </dependencies> ``` 2. 创建实体类:创建对应数据库表的实体类,并使用JPA注解进行映射,如: ```java @Entity @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; //其他属性和getter/setter方法 } ``` 3. 创建JPA Repository接口:创建继承于JpaRepository的接口,并添加@Repository注解,如: ```java @Repository public interface UserRepository extends JpaRepository<User, Long> { } ``` 4. 编写Controller:创建一个控制器类,注入UserRepository,并编写分页查询的API,如: ```java @RestController @RequestMapping("/users") public class UserController { @Autowired private UserRepository userRepository; @GetMapping public Page<User> getAllUsers(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) { Pageable pageable = PageRequest.of(page, size); return userRepository.findAll(pageable); } } ``` 5. 启动应用程序:在Spring Boot的入口类中添加@SpringBootApplication注解,并启动应用程序。 通过访问`http://localhost:8080/users?page=0&size=10`,即可获取第一页每页10条记录的用户列表。 以上就是使用Java Spring Boot编写分页查询的基本步骤。当然,根据具体的需求可以对代码进行进一步优化和定制。 ### 回答3: 使用Java Spring Boot编写分页查询可以通过以下步骤实现。 1.引入依赖:在项目的pom.xml文件中添加Spring Boot和Spring Data JPA的依赖。 ``` <dependencies> <!-- Spring Boot Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- Spring Data JPA --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> </dependencies> ``` 2.配置数据源:在application.properties文件中配置数据库连接信息。 ``` spring.datasource.url=jdbc:mysql://localhost:3306/your_database spring.datasource.username=your_username spring.datasource.password=your_password spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 3.创建实体类:创建一个与数据库表对应的实体类,并使用JPA注解定义属性和表与字段的映射关系。 ``` @Entity @Table(name = "your_table") public class YourEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "name") private String name; // getters and setters } ``` 4.创建JpaRepository接口:创建一个继承自JpaRepository的接口,用于对数据库进行操作。 ``` public interface YourRepository extends JpaRepository<YourEntity, Long> { Page<YourEntity> findAll(Pageable pageable); } ``` 5.编写查询方法:创建一个Service类并注入YourRepository接口,在该类中编写分页查询方法。 ``` @Service public class YourService { @Autowired private YourRepository yourRepository; public Page<YourEntity> findAll(int page, int size) { Pageable pageable = PageRequest.of(page, size); return yourRepository.findAll(pageable); } } ``` 6.创建控制器:创建一个RESTful控制器类,用于处理HTTP请求和响应。 ``` @RestController @RequestMapping("/yourEntity") public class YourController { @Autowired private YourService yourService; @GetMapping("/list") public ResponseEntity<Page<YourEntity>> getList(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) { Page<YourEntity> list = yourService.findAll(page, size); return new ResponseEntity<>(list, HttpStatus.OK); } } ``` 现在,你可以启动你的Spring Boot应用程序并使用浏览器或其他HTTP客户端发送GET请求到`http://localhost:8080/yourEntity/list?page=0&size=10`,即可获取分页查询的结果。 以上就是使用Java Spring Boot编写分页查询的简要步骤,你可以根据需要进行进一步的修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值