IDEA下用springboot搭建web项目

SpringBoot的具体介绍可以参看其他网上介绍,这里就不多说了,就这几天的学习,个人理解,简而言之: 
(1)它是spring的升级版,Spring容器能做到的事情,它都能做到,而且更简便,从配置形式上来说,SpringBoot完全抛弃了繁琐的XML文件配置方式,而是替代性地用注解方式来实现,虽然本质来说,是差不多的(类似包扫描,注解扫描,类加载之类)。 
(2)SpringBoot集成的插件更多,从而使用很多服务,都只是引入一个依赖,几个注解和Java类就可以用了,具体的参考相关手册。 
(3)在Web应用开发这一块,之前的应用一般来说是打包成war包,再发布到相关服务器容器下(例如Tomcat),虽然SpringBoot也可以这么做,但在SpringBoot下更常见的形式是将SpringBoot应用打包成可执行jar包文件。之所以这么做,源于你可以直接将SpringBoot应用看成是一个Java Application,其Web应用可以没有webapp目录(更不用说web.xml了),它推荐使用html页面,并将其作为静态资源使用。 
下面具体记录一下,如何在IDEA下从零开始,一步步搭建SpringBoot Web应用,这里采用的是maven作依赖管理,新手起步,有任何疑问,请参考SpringBoot官网。 
需要说明的是SpringBoot依赖的JDK版本为1.8及以上。 
(1)File->new,选择maven,创建一个空项目,直接next. 
这里写图片描述
(2)填写工程名 
这里写图片描述
(3)next到底,成果创建一个基于maven的空Java项目,其目录结构是这样的: 
这里写图片描述
(4)在pom文件中引入SpringBoot相关依赖


   
   
  1. <parent>
  2. <groupId>org.springframework.boot </groupId>
  3. <artifactId>spring-boot-starter-parent </artifactId>
  4. <version>1.5.1.RELEASE </version>
  5. </parent>
  6. <dependencies>
  7. <dependency>
  8. <groupId>org.springframework.boot </groupId>
  9. <artifactId>spring-boot-starter-web </artifactId>
  10. </dependency>
  11. </dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

(5)新建一个controller 包,用于存放所有的controller,这里跟官方的一样,使用SampleController为第一个测试用例。代码如下:


   
   
  1. /**
  2. * Created by Song on 2017/2/15.
  3. * 官方示例工程中的测试代码
  4. */
  5. @Controller
  6. @EnableAutoConfiguration
  7. public class SampleController {
  8. @RequestMapping( "/")
  9. @ResponseBody
  10. String home() {
  11. return "Hello World!";
  12. }
  13. public static void main (String[] args) throws Exception {
  14. SpringApplication.run(SampleController.class, args);
  15. }
  16. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

注意到,这里有一个main函数,再联想到前面说的,SpringBoot应用一般是打包成可执行jar包来发布的,这个main函数就是整个项目的入口。而之所以能这么做,是因为SpringBoot连Tomcat8作为一个插件都集成进去了,所以就不必跟之前的SSM架构下一样,还需要去在Tomcat下配置war包才能运行。直接点击运行该main函数,再浏览器链接栏,输入地址http://localhost:8080/,就可以看到打印的字符串”Hello World!”了。这就是官网提供的一个最基本的基于SpringBoot的Web应用,如此便捷。 
当然,一个基本的Web应用,结构肯定不会这么简单。下面要说的是,如何在上面的基础上,搭建一个具有MVC结构的完整的Web应用,其中数据库采用的是MySQL,ORM采用的是Spring Data JPA,前端页面采用js+HTML5。(当然还有其他的方式,例如ORM框架采用mybatis等,本文暂未涉及。) 
(6)在resource目录下新建一个application.properties文件(或yml文件),命名与位置为SpringBoot默认的配置文件。在该文件中,记录着所有的模块配置内容。例如Tomcat的端口(默认8080)以及编码方式等:


   
   
  1. server .port= 8080
  2. server .tomcat .uri-encoding=utf - 8
  • 1
  • 2
  • 1
  • 2
  • 1
  • 2

(7)引入本项目中所需要的相关依赖(mysql连接驱动 以及Spring Data JPA,thymeleaf模板引擎)


   
   
  1. <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  2. <dependency>
  3. <groupId>mysql </groupId>
  4. <artifactId>mysql-connector-java </artifactId>
  5. <version>5.1.39 </version>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
  8. <dependency>
  9. <groupId>org.springframework.boot </groupId>
  10. <artifactId>spring-boot-starter-thymeleaf </artifactId>
  11. <version>1.4.0.RELEASE </version>
  12. </dependency>
  13. <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
  14. <dependency>
  15. <groupId>org.springframework.boot </groupId>
  16. <artifactId>spring-boot-starter-data-jpa </artifactId>
  17. <version>1.5.1.RELEASE </version>
  18. </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

(8)在application.properties中配置MySQL数据库连接信息 
这里的数据库为本地数据库test,用户名和密码改成自己的


   
   
  1. #MySQL
  2. spring .datasource .driver- class- name= com .mysql .jdbc .Driver
  3. spring .datasource .url=jdbc:mysql: //localhost: 3306 /test?characterEncoding=utf8
  4. spring .datasource .username=****
  5. spring .datasource .password=****
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

(9)在application.properties中配置Spring Data JPA 
这一段的意思就是说,数据库类型为MYSQL,日志信息打印具体执行的sql语句,表更新策略以及Java类到数据库表字段的映射规则等,具体查看网络资料。


   
   
  1. #Spring Data JPA
  2. spring .jpa .database=MYSQL
  3. spring .jpa .show-sql= true
  4. spring .jpa .hibernate .ddl-auto=update
  5. # Naming strategy
  6. spring .jpa .hibernate .naming-strategy = org .hibernate .cfg .ImprovedNamingStrategy
  7. # stripped before adding them to the entity manager)
  8. spring .jpa .properties .hibernate .dialect = org .hibernate .dialect .MySQL5Dialect
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

(10)编写一个实体类User 
@Table标签,指定数据库中对应的表名,id配置为主键,生成策略为自动生成


   
   
  1. /**
  2. * Created by Song on 2017/2/15.
  3. * Model 用户
  4. */
  5. @Entity
  6. @Table(name = "tbl_user")
  7. public class User {
  8. @Id
  9. @GeneratedValue(strategy = GenerationType.IDENTITY)
  10. private long id;
  11. private String name;
  12. private String password;
  13. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

(11)基于JPA,实现DAO层(即数据库数据的增删改查操作) 
新建UserRepositoty.java接口文件,源代码如下:


   
   
  1. /**
  2. * Created by Song on 2017/2/15.
  3. * User表操作接口
  4. */
  5. @Repository
  6. public interface UserRepositoty extends JpaRepository<User,Long>{
  7. @Query( "select t from User t where t.name = :name")
  8. User findByUserName(@Param( "name") String name);
  9. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

需要解释的是,Spring Data JPA提供了很多持久层接口,例如Repository,CrudRepositoty,PagingAndSortingRepository 以及JpaRepository 接口。其中Repository为基类,JpaRepository继承自PagingAndSortingRepository接口,两个泛型参数分别代表java POJO类以及主键数据类型。我们创建自己的数据库操作接口时,只需继承上述JPA提供的某个接口,即可自动继承相关数据操作方法,而不需要再次实现。例如CrudRepositoty提供了对增删改查操作的实现,PagingAndSortingRepository提供了分页查询方法的实现。另外JPA提供了一套命名规则例如readBy**()等,这些方法也只需要用户申明而由JPA自动实现了。如果这仍不能满足业务需求,也可以自定义SQL查询语句,例如上述代码所示,采用@Query标签, 其中 :*语法为引用下面用@Param标识的变量,需要注意的是其中User不是表面而是Java POJO类名。具体使用参考JPA使用手册。 
(12)设计Service层业务代码 
新建UserService类,其源代码如下:


   
   
  1. /**
  2. * Created by Song on 2017/2/15.
  3. * User业务逻辑
  4. */
  5. @Service
  6. public class UserService {
  7. @Autowired
  8. private UserRepositoty userRepositoty;
  9. public User findUserByName (String name){
  10. User user = ;
  11. try{
  12. user = userRepositoty.findByUserName(name);
  13. } catch (Exception e){}
  14. return user;
  15. }
  16. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

(13)设计Controller层 
新建UserController.java,提供两个接口,/user/index 返回页面,/user/show返回数据。其源代码如下:


   
   
  1. /**
  2. * Created by Song on 2017/2/15.
  3. * User控制层
  4. */
  5. @Controller
  6. @RequestMapping(value = "/user")
  7. public class UserController {
  8. @Autowired
  9. private UserService userService;
  10. @RequestMapping(value = "/index")
  11. public String index (){
  12. return "user/index";
  13. }
  14. @RequestMapping(value = "/show")
  15. @ResponseBody
  16. public String show (@ RequestParam (value = "name" )String name){
  17. User user = userService.findUserByName(name);
  18. if( != user)
  19. return user.getId()+ "/"+user.getName()+ "/"+user.getPassword();
  20. else return "null";
  21. }
  22. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

(14)在application.properties文件中配置页面引擎。这里采用SpringMVC(SpringBoot还提供thymeleaf,freemaker等)。这里需要配置其静态资源(js、css文件、图片文件等)路径,以及html页面文件路径,参考SpringMVC在Spring下的配置。


   
   
  1. #视图层控制
  2. spring .mvc .view .prefix=classpath:/templates/
  3. spring .mvc .view .suffix= .html
  4. spring .mvc .static-path-pattern=/ static /**
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

(15)在resource目录下新建templates以及static目录,分别用于存放html文件以及(js、css文件、图片)文件。在(13)中返回了一个“user/index”页面,所以在templates下新建user目录,在user目录下新建index.html页面,这里就不写什么了,默认页面,通过相对路径引入js文件,js文件里只做示意,弹出一个alert()。 
user/index.html


   
   
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8"/>
  5. <script src="../static/scripts/jquery.min.js"> </script>
  6. <script src="../static/scripts/test.js"> </script>
  7. <title>Title </title>
  8. </head>
  9. <h1>TEST PAGE </h1>
  10. <body>
  11. </body>
  12. </html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

static/scripts/test.js


   
   
  1. $( document).ready( function (){
  2. alert( "OK TEST");
  3. });
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

(16)配置JPA 
新建一个configuration包,用于存放项目配置类。类似SSM架构下,spring需要配置Java POJO类包路径以及DAO层接口路径,以自动扫描相关注解,这里同样需要配置这两项,不同的是Spring采取的是xml配置方式,这里用Java代码+注解方式配置。新建一个JpaConfiguration.java类,其代码如下:


   
   
  1. /**
  2. * Created by Song on 2017/2/15.
  3. * JPA 配置类
  4. */
  5. @Order(Ordered.HIGHEST_PRECEDENCE)
  6. @Configuration
  7. @EnableTransactionManagement(proxyTargetClass = true)
  8. @EnableJpaRepositories(basePackages = "com.song.repository")
  9. @EntityScan(basePackages = "com.song.entity")
  10. public class JpaConfiguration {
  11. @Bean
  12. PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor(){
  13. return new PersistenceExceptionTranslationPostProcessor();
  14. }
  15. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

(17)配置项目启动入口 
到这一步就可以删掉(5)中官方示例给出的SampleController.java了,由于我们的工程结构已经发生了改变,我们需要告诉SpringBoot框架去扫描哪些包从而加载对应类,所以这里重新编写main函数。新建一个Entry.java类,其代码如下(其中@SpringBootApplication是一个复合注解,就理解为自动配置吧):


   
   
  1. /**
  2. * Created by Song on 2017/2/15.
  3. * 项目启动入口,配置包根路径
  4. */
  5. @SpringBootApplication
  6. @ComponentScan(basePackages = "com.song")
  7. public class Entry {
  8. public static void main (String[] args) throws Exception {
  9. SpringApplication.run(Entry.class, args);
  10. }
  11. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

(18)运行main函数,访问http://localhost:8080/user/index 会显示测试页面,并弹出alert(),访问http://localhost:8080/user/show?name=**(数据表里存在的数据)会显示user信息。最终的工程文件结构如下: 
这里写图片描述 
完整项目工程:https://github.com/Sonlan/springboot-demo

  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值