天天看点

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);}}