Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
项目快速开始
可以通过官网的向导快速开始,也可以通过maven开始
pom.xml
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
启动类
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//这个注解表示这是个启动类
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Controller:
package com.example.demo.controller;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
//time:2020年3月28日下午5:14:32
@RestController
public class hello {
@RequestMapping("hello1")
public String hello1(Model model) {
return "index";
}
直接运行项目就可以访问了:localhost:8080/hello1
具体语法自查
添加下面的可以实现提示
xmlns:th="http://www.thymeleaf.org"
需要引入的依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
在application.yml中或者application.properties中配置数据库,二选其一
application.yml
spring:
datasource:
username: root
password: 781458665
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=***********
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
查询方式
package com.example.demo.controller;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
//time:2020年3月28日下午5:14:32
@Controller
public class hello {
// 自动配置注解
@Autowired
DataSource datasource;
//JDBC模板
@Autowired
JdbcTemplate JdbcTemplate;
@RequestMapping("hello3")
public String hello3(Model model) throws Exception {
Connection connection = datasource.getConnection();
System.out.println(connection);
Statement stmt = connection.createStatement();
String sql = "select * from user where id=3";
ResultSet res = stmt.executeQuery(sql);
while (res.next()) {
model.addAttribute("name", res.getString("name"));
}
return "index";
}
@RequestMapping("hello4")
@ResponseBody
public List hello4(Model model) {
List<Map<String, Object>> list = JdbcTemplate.queryForList("select * from user");
return list;
}
}
依赖
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.1version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
在bean包下创建一个bean:User
package com.example.demo.bean;
public class User {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
UserMapper
@Mapper注解可以选择替换为在启动类上加@MapperScan(“com.example.demo.mapper”)
这样在com.example.demo.mapper包下的mapper都会被扫描,而不用每个接口都加@Mapper
package com.example.demo.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.example.demo.bean.User;
//指定这是一个操作数据库的mapper
@Mapper
public interface UserMapper {
@Select("select * from user where id=#{id}")
public User getbyId(int id);
@Select("select name from user where id=#{id}")
public String getnameByid(int id);
@Insert("insert into user(name) values(#{name})")
public void insertName(User user);
@Select("select * from user")
public List<User> getlist();
}
编写Controller类
package com.example.demo.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.bean.User;
import com.example.demo.mapper.UserMapper;
//time:2020年3月28日下午11:33:33
@RestController
public class datamapper {
@Autowired
UserMapper usermapper;
@RequestMapping("get1")
public User get1() {
return usermapper.getbyId(2);
}
@RequestMapping("get2")
public String get2() {
return usermapper.getnameByid(2);
}
@RequestMapping("get3")
public List<User> get3() {
User user = new User();
user.setName("李磊");
usermapper.insertName(user);
return usermapper.getlist();
}
}
依赖
<dependency>
<groupId>org.springframework.datagroupId>
<artifactId>spring-data-elasticsearchartifactId>
dependency>
具体操作方式
首先编写一个Bean
Book
package com.example.demo.bean;
import org.springframework.data.elasticsearch.annotations.Document;
//time:2020年3月29日下午4:41:28
//这个注解表明这个数据插入到哪个index中,哪个type中
//ES6.x中一个index只允许创建一个type
@Document(indexName = "test_index", type = "test1")
public class Book {
private int id;
private String bookName;
private String author;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return id + "--" + bookName + "--" + author;
}
}
然后写一个操作其的类
BookResp
CrudRepository接口本来就有很多方法供使用,当然也可以自定义方法
package com.example.demo.mapper;
import java.util.List;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.data.repository.CrudRepository;
import com.example.demo.bean.Book;
//https://github.com/spring-projects/spring-data-elasticsearch
//time:2020年3月29日下午4:40:40
public interface BookResp extends CrudRepository<Book, Integer> {
// 可以自定义方法
// List findBy#{bean里查询的属性名}(查询的值);
List<Book> findBybookName(String bookName);
}
Controller
package com.example.demo.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.bean.Book;
import com.example.demo.mapper.BookResp;
//time:2020年3月29日上午11:02:53
@RestController
public class ESController {
@Autowired
BookResp bookResp;
@RequestMapping("es1")
public String get1() {
Book book = new Book();
book.setId(12);
book.setBookName("中华有为");
book.setAuthor("lrg");
// bookResp.index(book);
bookResp.save(book);
return "";
}
@RequestMapping("es2")
public Iterable<Book> get2() {
// List list = bookResp.findBybookName("asdasd");
return bookResp.findAll();
}
}