springboot

152 阅读3分钟

Spring Boot 主要特征是:

创建独立的spring应用程序

直接内嵌tomcat,jetty,underow(不需要打成war包部署)

提供了固定化的"starter"配置,以简化构建配置

尽可能的配置spring和第三方库

提供产品级的功能,如安全指标,运行状况检测,和外部配置等

绝不会生成代码,并且不需要xml配置

Spring Boot为所有 Spring 的开发者提供一个开箱即用的、非常快速的、广泛接受的入门体验


pom配置


<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.tea</groupId>
    <artifactId>tea</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>tea</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Druid连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.14</version>
        </dependency>
        <!--不要忘记数据库驱动,因为springboot不知道我们使用的什么数据库,这里选择mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!--mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>
        <!-- 通用mapper -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.2</version>
        </dependency>

        <!--thymeleaf模板-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

@SpringBootApplication

为一个组合注解,包含了

  • @SpringBootConfiguration

  • @EnableAutoConfiguration //开启自动配置

  • @ComponentScan //开启注解扫描

  • @Configuration:声明JdbcConfiguration是一个配置类。

  • @PropertySource:指定属性文件的路径是:classpath:jdbc.properties

  • 通过@Value为属性注入值。

  • 通过@Bean将 dataSource()方法声明为一个注册Bean的方法,Spring会自动调用该方法,将方法的返回值加入Spring容器中。相当于以前的bean标签



@Table(name = "tb_user")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String userName;

    private String password;

    private String name;

    private Integer age;

    private Integer sex;

    private Date birthday;

    private Date created;

    private Date updated;
    getter setter method
}

//通用mapper
@Mapper
public interface UserMapper extends tk.mybatis.mapper.common.Mapper<User>{

}

@Service
public class UserService {

  @Autowired
  private UserMapper userMapper;

  public User queryById(Long id){
    return this.userMapper.selectByPrimaryKey(id);
  }


  @Transactional
  public void deleteById(Long id){
    this.userMapper.selectByPrimaryKey(id);
  }

  public List<User> all(){
    return this.userMapper.selectAll();
  }
}


@Controller
@RequestMapping("/user")
public class UserController {

  @Autowired
  private UserService userService;

  @GetMapping("{id}")
  @ResponseBody
  public User queryUserId(@PathVariable("id") Long id){
    return this.userService.queryById(id);
  }



  @GetMapping("/all")
  public String all(Model model){
    List<User> users = this.userService.all();
    model.addAttribute("users",users);
    return "user";
  }
}


----gitee.com/queryQiu/yu…