使用IDEA创建spring boot项目与项目解析(炒鸡简单一学就会) |
您所在的位置:网站首页 › idea的springboot项目怎么启动 › 使用IDEA创建spring boot项目与项目解析(炒鸡简单一学就会) |
各位看官老婆大家好,这篇介绍使用IDEA创建spring boot项目的过程和项目的特点~ 一.新建项目 ①在New Project 中选择Spring Initializr(弹簧初始化程序) ②JDK版本建议选择1.8,待会儿要和下面的一步保持一致 ③初始化服务的URL,选择default那个就行 ④next ⑤group自己填咯 ⑥artifact项目名字自己起咯 ⑦Java Version选择8,承接上一步的1.8 ⑧next ⑨左侧选择web,在中间的web前打勾 ⑩next 这里就是项目的命名和指定项目路径,你问我为啥又命名一次,我也想知道 (~ ̄ ̄)~ 接着finish创建完成 我们来看看这香气逼人的新项目 SpringbootApplication: 一个带有 main() 方法的类,用于启动应用程序 SpringbootApplicationTests:一个空的 Junit 测试了,它加载了一个使用 Spring Boot 字典配置功能的 Spring 应用程序上下文 application.properties:一个空的 properties 文件,可以根据需要添加配置属性 pom.xml: Maven 构建说明文件 二.新建HelloController import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/hello") public String hello() { return "Hello Spring Boot!"; } } //@RestController 注解: 该注解是 @Controller 和 @ResponseBody 注解的合体版三.启动springboot 在DemoApplicatin中右键点击Run,由于springboot自带Tomcat,所以无需手动配置;也可以点击右上角的绿色播放按钮来启动。 控制台输出是这样的,花花绿绿的还有spring的logo。 浏览器地址栏输入localhost:8080/hello,显示出欢迎界面,访问成功。 *springboot其他启动方式(番外): 1.侧边栏中的run选项 2.侧边栏中的package选项 生成jar包后,cmd中运行jar -jar 项目名.jar,运行后便可浏览器访问localhost:8080/hello 四.Spring Boot项目解析 1.pom.xml 较以往不同的是,springboot的pom文件多出了标签,该标签配置了springboot的父级依赖。有了这个,当前的项目才是 Spring Boot 项目,spring-boot-starter-parent 是一个特殊的 starter ,它用来提供相关的 Maven 默认依赖,使用它之后,常用的包依赖就可以省去 version 标签。 org.springframework.boot spring-boot-starter-parent 2.0.4.RELEASE2.应用入口类 Spring Boot 项目通常有一个名为 *Application 的入口类,入口类里有一个 main 方法, 这个 main 方法其实就是一个标准的 Javay 应用的入口方法。 @SpringBootApplication 是 Spring Boot 的核心注解,它是一个组合注解,该注解组合了:@Configuration、@EnableAutoConfiguration、@ComponentScan; 若不是用 @SpringBootApplication 注解也可以使用这三个注解代替。 其中,@EnableAutoConfiguration 让 Spring Boot 根据类路径中的 jar 包依赖为当前项目进行自动配置,例如,添加了 spring-boot-starter-web 依赖,会自动添加 Tomcat 和 Spring MVC 的依赖,那么 Spring Boot 会对 Tomcat 和 Spring MVC 进行自动配置。 Spring Boot 还会自动扫描 @SpringBootApplication 所在类的同级包以及下级包里的 Bean ,所以入口类建议就配置在 grounpID + arctifactID 组合的包名下 3.Spring Boot 的配置文件(这个是重点,赶紧用本本记下来~) springboot使用的是全局配置文件,名为application.properties。Spring Boot 不仅支持常规的 properties 配置文件,还支持 yaml 语言的配置文件。yaml 是以数据为中心的语言,在配置数据的时候具有面向对象的特征。 这是application.properties的写法,类似于Java中的方法调用 server.port=8989 server.servlet.context-path=/hello这是application.yml的写法,树状结构,最后一个key的值之前要加一个空格 server: port: 8989 server: servlet: context-path: /hello两种配置文件只需更改后缀名即可使用,不过不要忘记更改写法。 4.Spring Boot 热部署 当发现任何类发生了改变,就会通过 JVM 类加载的方式,加载最新的类到虚拟机中,这样就不需要重新启动也能看到修改后的效果了。 往 pom.xml 中添加一个依赖 org.springframework.boot spring-boot-devtools true重新启动 Spring Boot ,然后修改任意代码,就能观察到控制台的自动重启现象。 5.Spring Boot 使用 1)集成 MyBatis 修改 pom.xml 增加对 MySql和 MyBatis 的支持 org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1 mysql mysql-connector-java 5.1.212)新增数据库链接参数 3)创建 Student 实体类和 StudentMapper 映射类 新建一个【pojo】包,然后在其下创建一个 Student 类 public class Student { private Integer id; private Integer student_id; private String name; private Integer age; private String sex; private Date birthday; /* getter and setter */ }新建一个【mapper】包,然后在其下创建一个 StudentMapper 映射类: @Mapper public interface StudentMapper { @Select("SELECT * FROM student") List findAll(); }4)编写 StudentController @Controller public class StudentController { @Autowired StudentMapper studentMapper; @RequestMapping("/listStudent") public String listStudent(Model model) { List students = studentMapper.findAll(); model.addAttribute("students", students); return "listStudent"; } }5)编写 listStudent.jsp 文件 id name ${s.id} ${s.name}6)重启服务器运行 完毕 感谢@不能游的鱼指出本文的错误(@Mapperpublic其实是public没有换行),我在此表示感谢~附上spring boot在实际项目中的结构。 以上就是本篇带给大家的springboot项目创建和解析的相关知识,感谢大家的阅读~! |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |