Spring Boot集成Spring Data

1.pom.xml配置

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.3.1.RELEASE</version>
	</parent>

	<dependencies>
		<!-- MYSQL -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<!-- Spring Boot JDBC -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>

		<!-- JDBC连接池组件 -->
		<dependency>
			<groupId>com.zaxxer</groupId>
			<artifactId>HikariCP</artifactId>

		</dependency>
		<!-- Spring Boot Data JPA -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-commons</artifactId>
		</dependency>
		<!-- AOP -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-aop</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- 应用容器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jetty</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
		</dependency>
		<!-- 日志管理log -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-log4j</artifactId>
		</dependency>
		<!-- Servlet -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
		</dependency>
	</dependencies>
2.application.properties 配置数据源

#JDBC
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
spring.datasource.username=root
spring.datasource.password=wbw123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#spring.datasource.max-idle=10
#spring.datasource.max-wait=10000
#spring.datasource.min-idle=5
#spring.datasource.initial-size=5
#spring.datasource.validation-query=SELECT 1
#spring.datasource.test-on-borrow=false
#spring.datasource.test-while-idle=true
#spring.datasource.time-between-eviction-runs-millis=18800
#spring.datasource.jdbc-interceptors=ConnectionState;SlowQueryReport(threshold=0)
#spring.datasource.type=com.zaxxer.hikari.HikariDataSource
#JPA
#spring.jpa.database=jdbc:mysql://localhost:3306/mydatabase
#spring.jpa.show-sql=true
#spring.jpa.properties.xxxxxxx=
#spring.jpa.generate-ddl=
#spring.jpa.open-in-view=
#spring.jpa.database-platform=
#spring.jpa.hibernate.ddl-auto=
#spring.data.jpa.repositories.enabled=
#spring.jpa.hibernate.naming-strategy.xxxxxxx=
server.context-path=/xw
server.port=8080


3.定义Repository接口

/**
 * 用户DAO 不需要添加@Repository注解
 * Spring会自动为我们继承CrudRepository接口的接口创建实现类 org.springframework.data.jpa.repository.support.SimpleJpaRepository
 * @author WANBGBOWE
 *
 */
public interface UserRepository extends CrudRepository<User, Integer> {

	//使用命名规范查询:fin 、findBy、read等
	public User findByName(String name);
	public List<User> findByNameAndAge(String name,String age);

4.编写程序启动类


/**
 * 
 * 启动器
 *
 */
@SpringBootApplication
public class Application1  implements CommandLineRunner{
	
	@Autowired
    private <span style="font-family: Arial, Helvetica, sans-serif;">UserRepository   </span>userRepository;
	@Override
	public void run(String... args) throws Exception {
		<pre name="code" class="java">            User user = userRepository.<span style="font-family: Arial, Helvetica, sans-serif;">findByName('xx');</span>
System.out.println(user);}public static void main(String[] args) { SpringApplication.run(Application1.class,args);}}

 



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Spring Boot集成Elasticsearch,你可以按照以下步骤进行操作: 1. 在你的项目的pom.xml文件中添加spring-boot-starter-data-elasticsearch依赖。根据你的引用内容,可以使用以下依赖配置: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> ``` 这个依赖将会引入Spring Boot对Elasticsearch的支持。[1] 2. 确保你的Elasticsearch版本和IK分词器插件的版本一致。你可以从GitHub上下载elasticsearch-analysis-ik插件,并将其集成到你的Elasticsearch中。[3] 3. 在你的Spring Boot应用程序中配置Elasticsearch连接信息。你可以在application.properties或application.yml文件中添加以下配置: ```yaml spring.data.elasticsearch.cluster-nodes=localhost:9200 spring.data.elasticsearch.cluster-name=your-cluster-name ``` 这里的localhost:9200是Elasticsearch的主机和端口,your-cluster-name是你的Elasticsearch集群的名称。你可以根据你的实际情况进行修改。 4. 在你的Spring Boot应用程序中使用Elasticsearch。你可以创建一个Elasticsearch的Repository接口,并在你的服务类中使用它来进行数据操作。例如: ```java @Repository public interface UserRepository extends ElasticsearchRepository<User, String> { // 自定义查询方法 List<User> findByFirstName(String firstName); } ``` 这里的User是你的实体类,String是实体类的ID类型。你可以根据你的实际情况进行修改。 通过以上步骤,你就可以在Spring Boot中成功集成Elasticsearch,并使用它进行数据操作了。希望对你有帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值