In-depth study of spring-boot series (2) -- using spring-data-jpa

This article uses my previous blog post project as a prototype, changed it to a spring-boot project, and used spring-data-jpa instead of mybatis as a dao implementation.
The original project reference, Spring+SpringMVC+MyBatis+Maven framework integration: http://jisonami.iteye.com/blog/2295345
Since the spring-boot jar project does not support jsp temporarily, this project is still a war project, the next This article will introduce the jar project after replacing the jsp page with the thymeleaf template.

This article mainly records the following points:
1. The directory structure agreed by spring-boot
2. The jars that Maven needs to introduce
3. The relevant configuration of application.properties
4. The simple use of spring-data-jpa

1. The directory structure agreed by spring-boot



For this directory structure, Application.java is the Java configuration class of spring, which contains the main() method of the spring-boot application, which will be executed in the spring-boot jar project.
Spring currently supports three metadata configurations. , namely xml, Java annotations, Java configuration classes, and spring-boot recommends using Java configuration classes to complete the configuration of metadata.
The current directory structure of Jisonami2 is as follows:



Jisonami2Application.java is the entry class of spring-boot, including configuration information and main() method
@SpringBootApplication
@Controller
public class Jisonami2Application {
	
	@RequestMapping("/")
	public String index(){
		return "../../index";
	}
	
	public static void main(String[] args) {
		SpringApplication.run(Jisonami2Application.class, args);
	}
}


@SpringBootApplication is actually a combination of the following three annotations, and @SpringBootApplication is used directly when taking its default value
@Configuration
@EnableAutoConfiguration
@ComponentScan


Currently this project is a war project, so Jisonami2Application needs to inherit the SpringBootServletInitializer class and implement the following methods
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
	return builder.sources(Jisonami2Application.class);
}


2. The jar that Maven needs to introduce
can be configured as a spring-boot project in maven in two ways. This article uses the simplest direct inheritance spring-boot-starter-parent to become a spring-boot project.
By introducing some spring-boot-starter dependencies in the pom.xml of the spring-boot project, the configuration of the project can be simplified.
maven's pom.xml is as follows:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>org.jisonami</groupId>
	<artifactId>Jisonami2</artifactId>
	<packaging>war</packaging>
	<version>0.1.1</version>
	<name>Jisonami Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<!-- Directly inherit spring-boot-starter-parent to become spring-boot project-->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.3.3.RELEASE</version>
	</parent>

	<properties>
		<!-- A lambda expression is used to override the default java version, which defaults to 1.6 -->
		<java.version>1.8</java.version>
		<!-- For the table generated by hibernate5.1.0.Final before, hibernate4.3.11 introduced by default using spring-boot-starter-data-jpa will report sql error-->
		<hibernate.version>5.1.0.Final</hibernate.version>
		<oracle14.version>10.2.0.4.0</oracle14.version>
		<taglibs.standard.version>1.1.2</taglibs.standard.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
		<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>
		<!-- maven needs war package, spring boot jsp project must be war project, jar project does not support jsp -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<scope>provided</scope>
		</dependency>

		<!-- Introduce oracle database jdbc driver package-->
		<dependency>
			<groupId>com.oracle</groupId>
			<artifactId>ojdbc14</artifactId>
			<version>${oracle14.version}</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>

		<!-- jstl dependencies -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
		</dependency>
		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>${taglibs.standard.version}</version>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<!-- spring boot project compilation plugin -->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>


3. Related configuration of
application.properties application.properties is the configuration file of the spring-boot project. Spring-boot has built-in a lot of default configuration information. In the application.properties file, we can override the default configuration information of spring-boot.
At present, this project only has Need to configure the prefix and suffix of the jsp jump of spring-mvc
#Specify the project context path
#server.context-path=/Jisonami2
#Configure the prefix and suffix of the jsp view
spring.mvc.view.prefix=/WEB-INF/content/
spring.mvc.view.suffix=.jsp


Fourth, the simple use of spring-data-jpa

1. First, you need to configure the data
source Add the following method to Jisonami2Application.java to configure the data source
@Bean
@ConfigurationProperties(locations="classpath:DBConfig.properties", prefix="datasource")
public DataSource dataSource(){
	return DataSourceBuilder.create().build();
}

The default configuration in the DataSourceBuilder source code is as follows, that is, there are 4 default data sources available: (The dbcp data source configured above in DBConfig.properties)
private static final String[] DATA_SOURCE_TYPE_NAMES = { "org.apache.tomcat.jdbc.pool.DataSource", "com.zaxxer.hikari.HikariDataSource", "org.apache.commons.dbcp.BasicDataSource", "org.apache.commons.dbcp2.BasicDataSource" };


Configuration of DBConfig.properties:
datasource.driverClassName=oracle.jdbc.driver.OracleDriver
datasource.url=jdbc:oracle:thin:@192.168.75.130:1521:orcl
datasource.username=jison
datasource.password = jison

2. Taking UserRepository as an example, a brief introduction to
the code of spring-data-jpa using UserRepository is as follows:
public interface UserRepository extends CrudRepository<User, String>{
	
	public User findByName(String name);
	
	public User findByNameAndPassword(String name, String password);
	
}

UserRepository inherits the CrudRepository interface and the CrudRepository interface defines some commonly used methods, remove the comments, and see the source code defines the following methods:
<S extends T> S save(S entity);

	<S extends T> Iterable<S> save(Iterable<S> entities);

	T findOne(ID id);

	boolean exists(ID id);

	Iterable<T> findAll();

	Iterable<T> findAll(Iterable<ID> ids);

	long count();

	void delete(ID id);

	void delete(T entity);

	void delete(Iterable<? extends T> entities);

	void deleteAll();


Well, spring-data-jpa automatically generates the implementation of these methods for us. We only need to call the corresponding method in the service to implement the corresponding logic. The specific usage of spring-data-jpa is the official sample project of

google spring-data-jpa: https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample- data-jpa At this point, the project is completed. For more detailed code, please refer to the 0.1.1 version of my github project Jisonami: https://github.com/jisonami/Jisonami2/tree/0.1.1




Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326895106&siteId=291194637
Recommended