IDEA+gradle+Springboot+mysql
来源:互联网 发布:淘宝开店 保证金 编辑:程序博客网 时间:2024/05/05 04:11
最近在帮忙做一WEB Service。用到了IDEA和SpringBoot。对于新的IDE,不想多评价,毕竟跟eclipse类似,只是界面风格不同,配置方式有小的调整。SpringBoot则是非常方便,大大缩短了service的开发时间。
IDEA提供了两种版本控制方式,maven和gradle。由于跟android打交道比较多,因此选用了gradle。
本文将用个例子,简述工程的搭建和运行过程。
(1)IDEA的下载
首先,IDEA不是免费的!重要的是“但是”!
但是,官方给与30天的免费试用期。还有更重要的“当然”!
当然,并非没有破解的办法。破解方法网上很多,这里不再啰嗦。
官网为https://www.jetbrains.com/idea/download/#section=windows
官网有两个版本,Ultimate和Community。其不同在于,前者穿着衣服(功能多),后者光着屁股(支持功能少,需要自己添加plugin)。若不涉及web开发,后者就够用了。官网上有两者详细的区别,这里不再啰嗦。
我们选用Ultimate。都能破解了,当然选个功能多的。当前最新版本为2017.2.5
安装后,启动界面如下图
(2)SpringBoot项目的搭建
(a)点击“create new project”,选择“Spring Initializr”
这里需要注意的是,需要配置java SDK。
(b)Next,配置project。
选择maven or gradle,定义service package,定义发布格式jar or war
(c)Next,选择Dependence
这里我们先只选择web,之后再在project中根据需求添加其他dependence.
(d)Next,选择存储路径。
(e)Next,Gradle配置。
设置里默认没有提供”Use auto-import”,建议选上。
可以选择本地的gradle,这样编译速度更快,不受网络状况限制。
(f)Next,finish。
看一下,工程结构。
工程创建时,IDE给创建了两个文件一个是SpringBootApplication,一个是ServletInitializer。这两个文件暂时无需修改,他们是service的入口。
(f)处理不能创建java文件的问题。(optional)
有时会出现,在package下无法创建java文件的情况,这种问题通常出现在gradle版本控制,且不使用本地gradle的情况下。即点击右键新建时,没有java文件选项。
解决方案是:
ctrl+alt+shift+s进入工程结构设置界面。选择相应modle,并把src目录设置为source目录即可。可参照下图步骤。
至此,工程创建完成。
(3)创建一个简单的 hello world
SpringBoot帮助我们通过注释的方式,在尽可能少的步骤中,完成service的开发。
这里,至需要两步,即可完成hello world。
(a)创建一个controllor。当然,也可以不创建,直接在IDE提供的XXXApplication完成sub url mapping。
(b)定义方法,绑定sub url,并处理请求。
全部代码,如下
package com.breakloop.xxx.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class chargeBegin { @RequestMapping("/1234") public String sayHello(){ return "hello world!!!!!!"; }}
其中,
@RestController,用来标注该类为Request的处理类。应用将自动识别。
@RequestMapping(“/”),用来绑定Servlet的Sub url。
至此,所有编码结束。
当然,Springboot所提供的注解还有很多,不是简单一篇博文能够总结的。这里我们只记录步骤。
(4)Service的运行
第一次运行service时,需要配置TOMCAT
若已配置好TOMCAT,至需要快捷键运行即可。
Shift+F9,build project
Shift+F10,run application
Tomcat配置步骤如下:
(a)IDE右上角,EditConfiguration
(b)配置Tomcat Location
(c)创建Tomcat Location配置对象,并绑定war。
注:对于war的选择,war(exploded)支持热部署,通常用于开发阶段。war则用于发布阶段。
(d)查看运行效果
Shift+F9,重新编译
Shift+F10,运行。
访问localhost:8080/1234,查看效果。
至此,一个简单的SpringBoot Hello world完成。
在此基础上,我们添加数据库操作。
(5)mysql数据库的访问
(a)添加JDBC引用
在build.gradle中添加依赖库
dependencies { compile('org.springframework.boot:spring-boot-starter-data-jpa') compile('org.springframework.boot:spring-boot-starter-web') providedRuntime('org.springframework.boot:spring-boot-starter-tomcat') testCompile('org.springframework.boot:spring-boot-starter-test') runtime('mysql:mysql-connector-java')//JDBC依赖库}
(b)添加properties
在main/resources下的application.properties中添加数据库配置。如下图:
spring.jpa.hibernate.ddl-auto=create
指出,若数据库不存在,则自动创建。还有其他几个取值,如:update,create-drop等。
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
指出,数据库名称(db_example),以及ip和端口。
spring.datasource.username=springuserspring.datasource.password=ThePassword
指出,login user name和login password
服务启动后,将根据properties连接数据库。
(c)创建ORM Object对象
即创建对应的表。
package com.breakloop.XXX.entity;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;@Entitypublic class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long uid; private String Name; private int age;}
如果@Entity等注释无法识别,则需要在build.gradle文件的dependens中添加spring-boot-starter-data-jpa引用。
dependencies { compile('org.springframework.boot:spring-boot-starter-data-jpa')//JPA引用。 compile('org.springframework.boot:spring-boot-starter-web') providedRuntime('org.springframework.boot:spring-boot-starter-tomcat') testCompile('org.springframework.boot:spring-boot-starter-test')}
服务启动后,将会变量标注为@Entity的类,并根据类的定义,创建相应的表。因为我们properties中的设置为create,则若表已存在,则不做任何操作。
@Id,标注表的唯一标识。
@GeneratedValue(strategy = GenerationType.AUTO),标注表的该列,为自动生成数据。
(d)创建CurReposity接口
该接口,是一个操作集合。若不对方法进行个性化,无需任何修改,只需要创建一个空内容的接口文件即可。
每个表都需要有一个自己的接口文件。
package com.breakloop.xxxx.repository;import com.breakloop.xxxx.entity.User;import org.springframework.data.repository.CrudRepository;public interface UserRepository extends CrudRepository<User,Long> {}
(e)简单的增删改查
我们新创建一个controller类,名为UserController
在里面添加mapping,实现简单的增删改查。
package com.breakloop.XXXX.controller;import com.breakloop.XXXX.entity.User;import com.breakloop.XXXX.repository.UserRepository;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;import java.util.List;@RestControllerpublic class UserController { @Autowired UserRepository userRepository; @RequestMapping("/Add") public User addUser(){ User user=new User(); user.setAge(10); user.setName("A"); userRepository.save(user); return user; } @RequestMapping("/SearchAll") public List<User> findAll(){ Iterable<User> all= userRepository.findAll(); if (all!=null){ List<User> result=new ArrayList<User>(); for (User item : all) { result.add(item); } return result; } return null; } @RequestMapping("/Del") public String del(){ userRepository.delete(1L); return "Delete finished"; } @RequestMapping("/Update") public User update(){ User A=new User(); A.setAge(11); A.setUid(1L); A.setName("New Name A"); userRepository.save(A); return userRepository.findOne(1L); }}
依次执行:添加,查看,更新,删除,查看。
效果图如下:
注:代码中最为关键的一句是@Autowired,该注释告诉springboot,去帮助实现Repository接口。否则,抛空指针异常。
注:Springboot将返回的类,以JSON字符串形式输出。
至此,一个简单的基于SpringBoot+gradle的数据库服务完成。
- IDEA+gradle+Springboot+mysql
- Idea+SpringBoot+Mybtis+Mysql+Gradle+Swagger2
- Idea+SpringBoot+Mybtis+Mysql+Gradle+Swagger2
- Idea+SpringBoot+Mybtis+Mysql+Gradle+Swagger2
- idea使用gradle搭建SpringBoot
- idea+springboot+gradle创建项目
- 使用IDEA+GRADLE创建springboot项目
- IDEA SpringBoot gradle打项目jar包
- idea 下gradle创建springboot 项目
- 使用idea+gradle创建springboot项目
- springboot开启热部署之Idea&Gradle
- IDEA下gradle创建springboot项目
- IDEA下创建Springboot,thymeleaf,Mybatis,Postgresql,Gradle项目
- IDEA+gradle +SpringBoot 项目,bootrun 无法启动项目
- 使用 IntelliJ IDEA + Gradle + SpringBoot 搭建Hello World工程
- springboot+mybatis+gradle在idea和oracle使用
- IntelJ IDEA 启动 springBoot ,而不使用 Gradle 启动
- IDEA+Gradle+SpringBoot+SpringDataJPA+lambok(lombok有点问题)(一)
- springmvc的常用接口
- Docker常用命令
- kafka2:性能优化
- rsyslog 入门 第一篇
- 求几位数;打印每一位数字;逆序打印
- IDEA+gradle+Springboot+mysql
- LeetCode||55. Jump Game
- LeetCode.648 Replace Words
- Android项目如何取消与SVN的关联
- sublime中python中文输出
- php环境搭建步骤
- python的函数使用
- Android文本测量
- pandas的问题总结