乳神胸夹花,广州信用卡代还款,蒋定桂
表示当前pom文件从spring-boot-starter-parent继承下来,在spring-boot-starter-parent中提供了很多默认配置,可以简化我们的开发。
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.1.4.release</version> <relativepath/> <!-- lookup parent from repository --> </parent>
<properties> <project.reporting.outputencoding>utf-8</project.reporting.outputencoding> <java.version>1.8</java.version> <resource.delimiter>@</resource.delimiter> <maven.compiler.source>${java.version}</maven.compiler.source> <project.build.sourceencoding>utf-8</project.build.sourceencoding> <maven.compiler.target>${java.version}</maven.compiler.target> </properties>
<properties> <activemq.version>5.15.9</activemq.version> <antlr2.version>2.7.7</antlr2.version> <appengine-sdk.version>1.9.73</appengine-sdk.version> <artemis.version>2.6.4</artemis.version> ... </properties>
这样比如使用starter-web的时候就不需要指定版本号
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> <version>2.1.4.release</version> </dependency>
这时候将依赖管理的问题放到dependencymanagement中。
官网说明文档见:13.2.2 using spring boot without the parent pom
<dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-dependencies</artifactid> <version>2.1.4.release</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencymanagement>
使用mvn package打包的plugin。
<build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build>
官网见:13.5 starters
starters are a set of convenient dependency descriptors that you can include in your application. you get a one-stop shop for all the spring and related technologies that you need without having to hunt through sample code and copy-paste loads of dependency descriptors. for example, if you want to get started using spring and jpa for database access, include the spring-boot-starter-data-jpa dependency in your project.
spring-boot-starter-*
thirdpartyproject-spring-boot-starter
查看其diagram,可以排除某个依赖
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> <exclusions> <exclusion> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-tomcat</artifactid> </exclusion> </exclusions> </dependency>
官网见:18. using the @springbootapplication annotation
等同于@enableautoconfiguration,@componentscan和@configuration
官网见:23. springapplication
server.port=9090
application.yml
public class person { private string name; private int age; private date birthday; private string[] hobbies; private idcard idcard; ... }
public class idcard { private int id; private string number; }
person: name: jack age: 17 birthday: 1997/06/01 hobbies: [code,sing,share] idcard: id: 1 number: 111
@component @configurationproperties(prefix="person")
@autowired private person person;
如果person类上报错,在pom文件中加入如下依赖
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-configuration-processor</artifactid> </dependency>
官网见:90.2 reload templates without restarting the container
resources目录下有一个templates文件夹,可以将动态资源放到其中
<!--thymeleaf的jar包--> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-thymeleaf</artifactid> </dependency>
<html xmlns:th="http://www.thymeleaf.org"> <head> </head> <body> <span style="color:red; font-size:30pt" th:text="${str}"></span> </body>
@controller @requestmapping("/gupao") public class gupaocontroller { @requestmapping("/hello") public string hello(model model){ string str="hello spring boot"; //想要动态的显示在网页当中 model.addattribute("str",str); //接下来的页面是能够动态显示传过来的数据 return "test"; } }
在resources目录下有一个static文件夹,可以将静态资源放到其中,浏览器可以直接访问。
"classpath:/meta-inf/resources/" "classpath:/resources/" "classpath:/static/" "classpath:/public/"
webmvcautoconfiguration--->webmvcautoconfigurationadapter.addresourcehandlers(xxx)--->
this.resourceproperties.getstaticlocations()
return this.staticlocations;
private string[] staticlocations = classpath_resource_locations;
private static final string[] classpath_resource_locations = { "classpath:/meta-inf/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/" };
观察
@configurationproperties(prefix = "spring.resources", ignoreunknownfields = false) public class resourceproperties {
配置application.properties
spring.resources.static-locations=classpath:/gupao/
通过spring boot web项目api接口的方式,整合mybatis实现crud的操作。
重温一下web项目创建的过程。
<dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> </dependency> <dependency> <groupid>org.mybatis.spring.boot</groupid> <artifactid>mybatis-spring-boot-starter</artifactid> <version>1.3.1</version> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-thymeleaf</artifactid> </dependency>
db_gupao_springboot--->t_user
public class user { private int id; private string username; private string password; private string number; ... }
@repository @mapper public interface usermapper { user find(string username); list<user> list(); int insert(user user); int delete(int id); int update(user user); }
@service public class userservice { @autowired public usermapper usermapper; public user findbyusername(string username){ return usermapper.find(username); } public list<user> listuser(){ return usermapper.list(); } public int insertuser(user user){ return usermapper.insert(user); } public int updateuser(user user){ return usermapper.update(user); } public int delete(int id){ return usermapper.delete(id); } }
@restcontroller @requestmapping(value="/user",method = {requestmethod.get,requestmethod.post}) public class usercontroller { @autowired private userservice userservice; @requestmapping("/listone") @responsebody public user listone(string username){ return userservice.findbyusername(username); } @requestmapping("/listall") @responsebody public list<user> listall(){ return userservice.listuser(); } @requestmapping(value="/add",method= requestmethod.post) @responsebody public string add(user user){ int result=userservice.insertuser(user); if(result>=1) { return "添加成功"; }else{ return "添加失败"; } } @requestmapping(value="/update",method= requestmethod.post) @responsebody public string update(user user){ int result=userservice.updateuser(user); if(result>=1) { return "修改成功"; }else{ return "修改失败"; } } @requestmapping(value="/delete",method= requestmethod.get) @responsebody public string delete(int id){ int result=userservice.delete(id); if(result>=1) { return "删除成功"; }else{ return "删除失败"; } } }
<?xml version = "1.0" encoding = "utf-8"?> <!doctype mapper public "-//mybatis.org//dtd com.example.mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.csdn.springbootmybatis.dao.usermapper"> <resultmap id="result" type="com.gupao.springbootmybatis.domain.user"> <result property="username" column="username"/> <result property="password" column="password"/> <result property="number" column="number"/> </resultmap> <select id="find" resultmap="result"> select * from t_user where username=#{username} </select> <select id="list" resultmap="result"> select * from t_user </select> <insert id="insert" parametertype="com.gupao.springbootmybatis.domain.user" keyproperty="id" usegeneratedkeys="true"> insert into t_user ( id,username,password,number ) values ( #{id}, #{username, jdbctype=varchar}, #{password, jdbctype=varchar}, #{number} ) </insert> <delete id="delete" parametertype="int"> delete from t_user where id=#{id} </delete> <update id="update" parametertype="com.gupao.springbootmybatis.domain.user"> update t_user set user.username=#{username},user.password=#{password},user.number=#{number} where user.id=#{id} </update> </mapper>
#数据源 spring: datasource: url: jdbc:mysql://127.0.0.1:3306/boot?useunicode=true&characterencoding=utf-8&servertimezone=utc username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.driver #mybatis托管mapper文件 mybatis: mapper-locations: classpath:mapper/*.xml
mvn -dmaven.test.skip -u clean install
java -jar xxx.jar
<groupid>com.csdn</groupid> <artifactid>springboot-demo2</artifactid> <version>0.0.1-snapshot</version> <packaging>war</packaging>
https://www.youtube.com/watch?v=lhkwltdimhi&feature=youtu.be
build anything with spring boot spring boot is the starting point for building all spring-based applications. spring boot is designed to get you up and running as quickly as possible, with minimal upfront configuration of spring.
- | get started in seconds using spring initializr |
- | build anything: rest api, websocket, web, streaming, tasks, and more |
- | simplified security |
- | rich support for sql and nosql |
- | embedded runtime support: tomcat, jetty, and undertow |
- | developer productivity tools such as livereload and auto restart |
- | curated dependencies that just work |
- | production-ready features such as tracing, metrics, and health status |
- | works in your favorite ide: spring tool suite, intellij idea, and netbeans |
group:com.example
artifact:bootiful
dependencies:reactive web,reactive mongodb,lombok,actuator,security
spring data integrates seamlessly with sql and nosql persistence stores. spring data supports reactive data access,too!
@component class datawriter implements applicationrunner { private final customerrepository customerrepository; datawriter(customerrepository customerrepository) { this.customerrepository = customerrepository; } @override public void run(applicationarguments args) throws exception { flux.just("jack", "rechal", "richard", "jobs") .flatmap(name -> customerrepository.save(new customer(null, name))) .subscribe(system.out::println); } } interface customerrepository extends reactivemongorepository<customer, string> { } @document @noargsconstructor @data class customer { private string id,name; public customer(string id, string name) { this.id = id; this.name = name; } }
on the web,nobody knows you're a reactive microservice.
@springbootapplication public class bootifulapplication { @bean routerfunction<serverresponse> routes(customerrepository cr){ return routerfunctions.route(get("/customers"),serverrequest -> ok().body(cr.findall(),customer.class)); } public static void main(string[] args) { springapplication.run(bootifulapplication.class, args); } }
how's your app's health?who better to articulate that then the application itself?
spring boot featurese strong opinions,loosely held.
it's easy to change any of them with properties or pluggable implementations
management.endpoint.health.show-details=always management.endpoints.web.exposure.exclude=*
@bean healthindicator healthindicator(){ return () -> health.status("i <3 production").build(); }
访问:curl | jq
effortlessly plugin authentication and authorization in a traditional or reactive application with spring security
@bean mapreactiveuserdetailsservice users(){ return new mapreactiveuserdetailsservice(user.withdefaultpasswordencoder().username("user").password("pw").roles("user").build()); }
访问:curl -vu user:pw | jq
let's provision a mongodb instance,configure our application's route and mongodb binding,and then push our application to production with cloud foundry.
命令切换到bootiful根目录下
cf services
定位到my-mongodb文件夹
大家可以扫描下方二维码关注下我的微信公众号,公众号内没有福利,只会定期生产技术性文章!
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
如何在java 8 stream表达式实现if/else逻辑
Java中有界队列的饱和策略(reject policy)原理解析
网友评论