springBoot【01】

/*
    使用spring官网的 http://start.spring.io/ 来建立项目包
    生成入口文件,入口文件中对类注释@SpringBootApplication,这个注释是唯一的,标明这个类是入口类
    可以直接启动该类,发现项目是通过内置的Tomcat启动的,端口号8080
    
    因为还没有配置数据库,所以先用注释标明先不启动DataSource

 */
@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class XcxloginApplication {

    public static void main(String[] args) {
        SpringApplication.run(XcxloginApplication.class, args);
    }
}

 

/**
 * 访问控制器需要用@RestController来声明,表示告诉Spring直接渲染返回string给调用者
 * 路由通过@RequestMapping来声明
 */

@RestController
public class HelloWorldController {
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(){
        return "HelloWorld";
    }
}

 

实体类、JPA、控制类

@Entity
@Table(name = "t_user")
public class UserEntity implements Serializable {
    @Id
    @GeneratedValue
    @Column(name = "t_id")
    private Long id;

    @Column(name = "t_name")
    private String name;

    @Column(name = "t_age")
    private int age;

    @Column(name = "t_address")
    private String address;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }


}


public interface UserJPA extends JpaRepository<UserEntity, Long>,
        JpaSpecificationExecutor<UserEntity>, Serializable {
}


@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserJPA userJPA;

    /**
     * 查询用户列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<UserEntity> list(){
        return userJPA.findAll();
    }

}

 

posted @ 2018-12-09 00:14  ohmydenzi  阅读(165)  评论(0编辑  收藏  举报