spring boot (1)

I started playing spring boot today, made a small example, and recorded it first.

The fastest way to start a spring boot, just take the following steps:

1. Create a new maven project;

2. Configure simple pom.xml;

<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>com.panshao</groupId>
  <artifactId>springboot</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>springboot</name>
  <url>http://maven.apache.org</url>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
  <build>
    <finalName>springboot</finalName>
	   <sourceDirectory>src/main/java</sourceDirectory>
	    <testSourceDirectory>src/test/java</testSourceDirectory>
	    <outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>
	    <testOutputDirectory>src/main/webapp/WEB-INF/classes</testOutputDirectory>
	    <defaultGoal>package</defaultGoal>
	    <resources>
	      <resource>
	        <directory>src/main/resources</directory>
	      </resource>
	    </resources>
	    <testResources>
	      <testResource>
	        <directory>src/test/resources</directory>
	      </testResource>
	    </testResources>
  </build>
</project>

 

The red above is all the configuration;

 

3. Write a startup class;

package com.panshao.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@EnableAutoConfiguration
public class SampleController {
	 @RequestMapping("/")
	    @ResponseBody
	    String home() {
	        return "Hello World!";
	    }

	    public static void main(String[] args) throws Exception {
	        SpringApplication.run(SampleController.class, args);
	    }
}

 

 Run a main method, spring boot will help you deploy it to its own tomcat, and then you can access the link: http://127.0.0.1:8080/ and you can get one: Hello World!, so easy! Mom doesn't have to worry about my configuration anymore!

So far, no other configuration files have appeared! ! !

 

It's enough to know the above.

 

Then let's talk about the problem of accessing the database:

1. Database JDBC link:

At this time, we first load the driver, I use oracle, take him as an example:

 

<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>com.panshao</groupId>
  <artifactId>springboot</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>springboot</name>
  <url>http://maven.apache.org</url>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
</parent>
<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>
   <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-jdbc</artifactId>
	</dependency>
    <dependency>
		<groupId>com.oracle</groupId>
		<artifactId>ojdbc14</artifactId>
		<version>10.2.0.1.0</version>
	</dependency>
</dependencies>
  <build>
    <finalName>springboot</finalName>
	   <sourceDirectory>src/main/java</sourceDirectory>
	    <testSourceDirectory>src/test/java</testSourceDirectory>
	    <outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>
	    <testOutputDirectory>src/main/webapp/WEB-INF/classes</testOutputDirectory>
	    <defaultGoal>package</defaultGoal>
	    <resources>
	      <resource>
	        <directory>src/main/resources</directory>
	      </resource>
	    </resources>
	    <testResources>
	      <testResource>
	        <directory>src/test/resources</directory>
	      </testResource>
	    </testResources>
  </build>
</project>

The driver jar package is added, and the next step is to configure the database link, which is configured in application.properties:

spring.datasource.url=jdbc\:oracle\:thin\:@10.18.96.50\:1521\:ismp
spring.datasource.username=xxxx
spring.datasource.password=xxxx
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

 In this way, the jdbc link is over;

 

2. The ORM framework used:

  Continue to configure hibernate in application.properties

spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

 

3. Entity class mapping:

package com.panshao.springboot.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@SuppressWarnings("serial")
@Table(name="T_ACCEPT")
public class Accept implements Serializable{
	private String acceptNumber;
	private String bnetId;
	
	@Id
	public String getAcceptNumber() {
		return acceptNumber;
	}
	public void setAcceptNumber(String acceptNumber) {
		this.acceptNumber = acceptNumber;
	}
	public String getBnetId() {
		return bnetId;
	}
	public void setBnetId(String bnetId) {
		this.bnetId = bnetId;
	}
}

 The mapping of table field names to entity fields is omitted ;

 

4. Dao operation:

The dao operation is more fun, and it can reflect the principle of "convention is due to configuration". You will find that what you write is just an excuse. When writing your own method, you should pay attention that the parameters are not arbitrarily selected, but correspond to the entity fields. Just do it. However, I just thought of a question as I wrote it, that is, how to deal with the incoming fee entity field? Time to try!

package com.panshao.springboot.dao;

import java.util.List;

import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;

import com.panshao.springboot.entity.Accept;

@Transactional
public interface AcceptDao  extends CrudRepository<Accept, Long> {
	  public List<Accept> findByBnetId(String bnetId);
}

 

 

5. Call:

 

package com.panshao.springboot;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.panshao.springboot.dao.AcceptDao;
import com.panshao.springboot.entity.Accept;

@Controller
@EnableAutoConfiguration
public class AcceptController {
	
	@Autowired
	AcceptDao acceptDao;
	
    @RequestMapping("/get-by-bnetId")
    @ResponseBody
    public String getByBnetId(String bnetId) {
      List<Accept> acceptList = acceptDao.findByBnetId(bnetId);
      StringBuffer buffer = new StringBuffer();
      if (acceptList != null) {
    	 for (Accept accept : acceptList) {
    		 String acceptNumber = accept.getAcceptNumber();
    		 buffer.append("acceptNumber = "+acceptNumber + "\r\n");
		}
        return "The accept number is: \r\n" + buffer;
      }
      return "accept with bnetId=" + bnetId + " is not exist.";
    }
    
    public static void main(String[] args) throws Exception {
        SpringApplication.run(AcceptController.class, args);
    }
  }

 

http://127.0.0.1:8080/get-by-bnetId?bnetId=xxxx

 

 

Guess you like

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