spring boot (一)

今天开始玩spring boot,做了个小例子,记录一下先。

最快的启动一个spring boot,只要下面几步:

1,新建一个maven项目;

2,配置简单的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>

上面红色的就是全部配置了;

3,写一个启动的类;

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

 运行一个main方法,spring boot便会帮你部署到他自带的tomcat里面去了,然后你就可以访问链接: http://127.0.0.1:8080/    便可以得到一个 : Hello World!了, so easy ! 妈妈再也不用担心我的配置了!

到此,没出现任何其他的配置文件!!!

上面就当有个认识就可以了。

接着我们来说说访问数据库的问题:

1,数据库JDBC链接:

这时候我们先加载驱动,我用的是oracle,就以他为例:

<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>

驱动jar包加好了,接下来是配置数据库链接,是配置在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

 这样jdbc链接也就结束了;

2,采用的orm框架:

  继续在application.properties里面配置hibernate

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

3,实体类映射:

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

 省去了表的字段名到实体字段的映射

4,dao操作:

dao操作比较好玩,很能体现“约定由于配置”的原则,你会发现你写出来的就是一个借口就好了,写属于自己的方法是要注意参数不是随便取的,要跟实体字段相对应才行。不过写着写着刚想到一个问题,就是传入费实体字段要如何处理?有空要试试!

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,调用:

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

猜你喜欢

转载自panshaobinsb.iteye.com/blog/2269856