springBootは火の非常に新しい技術今のjava円で、また別のオープンソースプロジェクトの開発チームの春である、今では、このプロジェクトは非常に人気があるようですので、著者はまた、このドアについて学ぶために、独自の体系的な方法を使用しようとしますすぐにウェブバックエンドエンジニアリングを構築springBootを使用する方法について何かを言うための技術、今日。
我々はspringBootを使用し始めたときSSMを使用した場合、我々は何をすべきか仕事のプロジェクトを開くために振り返りますか?まず、我々は春に依存関係の束をインポートする必要があり、MavenのベースのWebプロジェクトでは、IDEで作成する必要があり、我々は春のXML設定ファイルを探す必要はなく、これをweb.xmlファイルを変更し、そうで....ワード数で省略されます。全体的に、SSMは、構成の多くは非常に複雑です、あなた以前に開発されたいくつかのプロジェクトは、SSMで使用されている場合、あなたはおそらくすでに成熟したシステム構成を持っているし、その後の元で新しいプロジェクトを開きます行にコピーされたコンフィギュレーションのセット。しかし、コンフィギュレーション・ファイルの完全なセットのための時間がなかったか、フレームワークのバージョンをアップグレードする必要がない、頭痛はまだ非常に迷惑です。
だから、今日、私たちは、あなたが何をする必要があるかのWebプロジェクトを構成springBoot使用することを学びます。
1.プロジェクトはすぐにspringBootを生成します
春は、設計springBoot時間がより人道的考慮するので、ゼロから様々な痛みの春のプロジェクトをビルドするために、チームを認識することがあります。開発者は、のみ開く必要がstart.spring.io適切な構成を選択し、良いspringBootプロジェクトのダウンロードを生成するために彼らの必要性に応じて、その上に。
構成を選択する必要がある人のためとして、あなただけの必要がある最初の5時、次の販売をクリックして、すべてがすべてがspringBoot統合サポートを頼る見ることができます。
著者は、単純なスタートアッププロジェクトを構成するためにそれらを使用するには、これらのオプションは、JPA、MySQLをウェブを選びました。
2. [スタート]プロジェクト
将下载好到本地的压缩包打开,然后让IDE去导入依赖的jar包,完成之后我们启动一个这个项目。
启动的方式不再是先启动tomcat容器了,springBoot内部已经集成了一个tomcat,因此我们无需再做配置,只需要找到src/main/java下面的根目录中的Application.java文件,然后选择运行这个文件即可。
下面我们看一下启动后的样子:
但是我们会发现运行失败了!
错误提示是说我们没有配置数据源的url,所以当我们在springBoot项目中加入JPA依赖后就必须要配置数据源的信息。
好吧,我们把数据源的信息写上
下面再来试试:
启动成功!
3.测试MVC功能
前面写到我们在项目中加入了MVC的web依赖包,下面我们来测试一下MVC的功能。
在application类中编写一个接口:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class MySpringBootApplication {
@RequestMapping("/")
public String home(){
return "SpringBoot Start!欢迎使用";
}
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class,args);
}
@RequestMapping("use")
public String used(String key){
return "used:"+key;
}
}
然后我们启动后访问一下http:localhost:8080/ 看下运行结果:
这里有一点需要注意,在springBoot的项目中,它已经默认为我们配置了编码方式为UTF-8,因此我们的中文没有出现乱码。
从这里可以看出springBoot的优势了,我们若要新建web工程只需要在配置中加入一个web依赖,一切都不需要再配置了,在SSM中我们还要配置请求转发器、过滤器和跨域等等...你只需要照着已经配置好的springMVC的功能使用就行了。
跨域配置,在通常的项目开发中我们都选择的是前后端分离模式,因此跨域是必不可少的,下面我们就来配置一下springBoot的跨域支持。
新建一个配置类或者在一个已有的配置类中添加一个方法:
package com.viking.MySpringBoot.config.mvc;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* Created by Viking on 2019/4/26
* 匿名内部类实现跨域配置
*/
@Configuration
public class MyConfiguration {
@Bean
public WebMvcConfigurer crossConfigurer(){
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**");
}
};
}
}
これは、クロスドメインを設定します。この構成は、理由WebMvcConfigurerに、メソッド内匿名の内部クラス、我々はまた、他の方法で構成することができる同じ、継承WebMvcConfigurerインターフェイスを書き込み、ボイドaddCorsMappingsを上書きする(CorsRegistryレジストリ)メソッドができていますすべてのインターフェイスメソッドは、彼らはすでに、独自のメソッド本体を持っているインタフェースのデフォルトのキーワードの変更に追加されているので、我々はインターフェイスを継承するときに我々は、すべてのメソッドを実装する必要はありません。
WebMvcConfigurerインターフェイスコンフィギュレーションモードを実装します。
package com.viking.MySpringBoot.config.mvc;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* Created by Viking on 2019/5/6
* 重写接口方法实现配置跨域
*/
@Configuration
@EnableWebMvc
public class CORSConfiguration implements WebMvcConfigurer {
public void addCorsMappings(CorsRegistry registry) {
//设置允许跨域的路径
registry.addMapping("/**")
//设置允许跨域请求的域名
.allowedOrigins("*")
//这里:是否允许证书 不再默认开启
.allowCredentials(true)
//设置允许的方法
.allowedMethods("*")
//跨域允许时间
.maxAge(3600);
}
}
彼らは、二つ以上のいずれかを選択することができ、クロスドメイン要求を達成することができます。
4.テストJPA
springBoot MyBatisのをサポートすることができますが、著者はそれがspringBoot偉大な自尊心の関数であるので、我々は何も別の場所を試してみていることを信じていたとき。
以前のコンフィギュレーション・ファイルでは、私はあなたが気づいていない場合、私は、なぜな構成駆動型のデータベース接続が使用することができないコメントするように構成されたデータソースのドライバをのmysqlうかしら?springBootが文書で説明しているので、それは自動的にデータソースのURLで駆動する必要があるものに決定することができます。しかし、あなたは、ドライブがポンポンファイル内のジョブに依存して追加する必要があります。
JPAデータ・ソースを使用した場合も、それがあることに注意して、それ以外の場合は失敗します、URLのタイムゾーンデータベースを使用して開発する必要があります。
次の段落JPAのテストコードを書きます:
エンティティWeather.java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Date;
/**
* Created by Viking on 2019/4/27
* 测试springBoot中jpa的url参数
*/
@Entity(name = "weather")
public class Weather {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long rid;
private String weather;
private float temperature;
private String tip;
private Date date;
public long getRid() {
return rid;
}
public void setRid(long rid) {
this.rid = rid;
}
public String getWeather() {
return weather;
}
public void setWeather(String weather) {
this.weather = weather;
}
public float getTemperature() {
return temperature;
}
public void setTemperature(float temperature) {
this.temperature = temperature;
}
public String getTip() {
return tip;
}
public void setTip(String tip) {
this.tip = tip;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
リポジトリインタフェースWeatherRepository.java:
import com.viking.springboot.MySpringBoot.pojo.Weather;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* Created by Viking on 2019/4/27
*/
public interface WeatherRepository extends JpaRepository<Weather,Long> {
}
テストカテゴリ:
import com.viking.springboot.MySpringBoot.dao.WeatherRepository;
import com.viking.springboot.MySpringBoot.pojo.Weather;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Date;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MySpringBootApplicationTests {
@Autowired
private WeatherRepository weatherRepository;
@Test
public void contextLoads() {
Weather weather = new Weather();
weather.setWeather("冰雹");
weather.setTemperature(10.0f);
weather.setTip("冰雹天气,请注意预防自然灾害");
weather.setDate(new Date());
weatherRepository.save(weather);
}
}
観測データベース結果:
テストが成功した挿入し、クエリを再度試してみてください。
package com.viking.springboot.MySpringBoot;
import com.viking.springboot.MySpringBoot.dao.WeatherRepository;
import com.viking.springboot.MySpringBoot.pojo.Weather;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Date;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MySpringBootApplicationTests {
@Autowired
private WeatherRepository weatherRepository;
@Test
public void contextLoads() {
Weather weather = new Weather();
weather.setWeather("冰雹");
weather.setTemperature(10.0f);
weather.setTip("冰雹天气,请注意预防自然灾害");
weather.setDate(new Date());
weatherRepository.save(weather);
}
@Test
public void testQuery(){
Page<Weather> page = weatherRepository.findAll(PageRequest.of(0, 2));
System.out.println("list:"+page.getContent());
System.out.println("total:"+page.getTotalElements());
}
}
私たちはもはや、ページネーションプラグインのようにMyBatisの中で使用する必要があるとして、JPAのクエリは、ページングのためのサポートが付属していません。
結果を見てください:
テストは成功です!
要約すると:一般的に、springBootの使用が急速にWebプロジェクトをビルドすることは問題ではありませんが、SpringBoot非常にユーザーフレンドリーな設計、springBootの使用は、将来の傾向である、および設定の多くは、著者らはまた、完全に導入していませんでしたがありますspringBoot、今後の記事で、再び一つの研究を学ぶために真剣に。