实训随笔:从零开始Spring项目

Spring Boot是什么

开始一个Spring Boot项目

1、到intellij idea官网下载Ultimate版本的IDE,因为Community版本默认是没有Spring Initializr来初始化并配置运行Spring Boot的。

2、"New Project"->"Spring Initializr",Java jdk最好选择1.8版本,因为1.7的版本缺少一些关键内容,运行时可能会出错。

初始化的服务网址选择默认就可以。

点击"Next",配置项目基本信息,"Type"选择"Maven Project","Language"选择"Java",此处的"Java Version"应该与上一部的配置保持一致,选择"8"。

点击"Next",选择相关依赖库,勾选上"Web" "JPA" "MySQL" "JDBC"。

点击"Next",设置项目名称完成设置,一个Spring项目就建好了。

随后Idea会从Spring官网下载模板,并应用刚才的设置,初始目录结构如图所示。

我们还需要根据自己的需求建立一些类和文件夹,建议设置如下(其实刚开始根本用不了这么多类别):

代码层的结构

根目录:com.springboot

1.工程启动类(ApplicationServer.java)置于com.springboot.build包下

2.实体类(domain)置于com.springboot.domain

3.数据访问层(Dao)置于com.springboot.repository

4.数据服务层(Service)置于com,springboot.service,数据服务的实现接口(serviceImpl)至于com.springboot.service.impl

5.前端控制器(Controller)置于com.springboot.controller

6.工具类(utils)置于com.springboot.utils

7.常量接口类(constant)置于com.springboot.constant

8.配置信息类(config)置于com.springboot.config

9.数据传输类(vo)置于com.springboot.vo

资源文件的结构

根目录:src/main/resources

1.配置文件(.properties/.json等)置于config文件夹下

2.国际化(i18n))置于i18n文件夹下

3.spring.xml置于META-INF/spring文件夹下

4.页面以及js/css/image等置于static文件夹下的各自文件下

 我是在"src/main/java/根包"目录下新建了"Entity"(实体)"Service"(数据服务层与接口,偷个懒..)"Controller"(前端控制器)"Repository"(数据访问层)

"src/main/resources"目录下的"static"存放js/css/image等文件,"templates"存放html页面。

下一次再详细记录如何编写Spring Boot项目。

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentServiceBean': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract boxfish.bean.Student boxfish.service.StudentServiceBean.find(java.lang.String)!

原因:bean类中的扩展查询方法的query语句中的sql语句存在错误。

查询语句应该为select    s   from  Student s where s.id=?1这种样式。其中from后紧跟的内容必须和Entity中定义的实体类名完全相同,否则会报错。

参考资料:

https://blog.csdn.net/lzx925060109/article/details/40323741

https://blog.csdn.net/u012675150/article/details/79351990

猜你喜欢

转载自www.cnblogs.com/BoqianLiu/p/9216808.html
今日推荐