spring boot入门例

来源:互联网 发布:数据分析专业 编辑:程序博客网 时间:2024/04/28 17:37

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>test.spring</groupId><artifactId>boot</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>boot Maven Webapp</name><url>http://maven.apache.org</url><properties><java.version>1.8</java.version></properties><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.3.5.RELEASE</version></parent><dependencies><dependency><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><version>2.4.3</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.16.4</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency></dependencies><build><finalName>boot</finalName></build></project>

package com.spring.boot.controller;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.spring.boot.entity.User;import com.spring.boot.service.HelloService;@Controllerpublic class HelloController {@AutowiredHelloService helloService;@RequestMapping("/")    @ResponseBody    List<User> home() {helloService.add();        return helloService.find();    }    }

package com.spring.boot.service;import java.util.Arrays;import java.util.List;import javax.transaction.Transactional;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.spring.boot.entity.User;import com.spring.boot.repository.UserRepository;@Servicepublic class HelloService {@AutowiredUserRepository userRepository;@Transactionalpublic void add(){User u1 =new User(1L, "qq", "ww");User u2 =new User(2L, "qqww", "wwss");List<User> ul = Arrays.asList(u1,u2);userRepository.save(ul);}public List<User> find(){return userRepository.findAll();}}

package com.spring.boot.repository;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.stereotype.Repository;import com.spring.boot.entity.User;@Repositorypublic interface UserRepository extends JpaRepository<User, Long>{}

package com.spring.boot.entity;import static javax.persistence.GenerationType.IDENTITY;import java.io.Serializable;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.Table;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Entity@Table(name="t_user")@Data@AllArgsConstructor@NoArgsConstructorpublic class User implements Serializable{/** *  */private static final long serialVersionUID = 1L;@Id@GeneratedValue(strategy = IDENTITY)@Column(name="id")private Long id;@Column(name="name")private String name;@Column(name="pass")private String pass;}

package com.spring.boot;import java.util.Arrays;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.ComponentScan;@SpringBootApplicationpublic class Application {public static void main(String[] args) { ApplicationContext ctx = SpringApplication.run(Application.class, args);       /* System.out.println("Let's inspect the beans provided by Spring Boot:");        String[] beanNames = ctx.getBeanDefinitionNames();        Arrays.sort(beanNames);        for (String beanName : beanNames) {            System.out.println(beanName);        }*/}}


0 0