环境创建
-
Create New Project
-
Spring Initializer
-
Project Metadata
-
Dependencies
-
name and location
-
目录结构
-
maven配置
web示例
我这里有一个mysql数据库,里面有一个user表,以查询此表为例
首先在项目里依次创建controller、service、dao、entity
包
在controller
包里创建HelloController
类
package com.example.test2.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/index")
public String sayHello() {
return "hello you!";
}
}
修改application.properties
为application.yml
编辑application.yml
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://10.180.222.50:3306/dbtest
username: root
password: db123
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
show-sql: true
运行项目
启动后,浏览器上测试HelloController类是否运行。
进一步的完善
创建UserEntity
package com.example.test2.entity;
import lombok.Data;
import javax.persistence.*;
@Entity
@Table(name = "user")
@Data
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
@Column(name = "password")
private String password;
}
创建UserDao
package com.example.test2.dao;
import com.example.test2.entity.UserEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface UserDao extends JpaRepository<UserEntity,String>,JpaSpecificationExecutor<UserEntity>{
UserEntity getByName(String name);
}
创建UserService
package com.example.test2.service;
import com.example.test2.dao.UserDao;
import com.example.test2.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserDao userDao;
public UserEntity getUserEntityByName(String name) {
return userDao.getByName(name);
}
public List<UserEntity> getAllUser() {
return userDao.findAll();
}
}
转向对UserService
类单项测试
选择Create New Tests
,勾选待测试方法
补充测试方法
package com.example.test2.service;
import com.example.test2.entity.UserEntity;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
class UserServiceTest {
@Autowired
private UserService userService;
@Test
void getUserEntityByName() {
UserEntity entity = userService.getUserEntityByName("user01");
System.out.println("name:"+entity.getName()+"***password:"+entity.getPassword());
}
@Test
void getAllUser() {
List<UserEntity> userEntityList = userService.getAllUser();
for (UserEntity entity : userEntityList) {
System.out.println("name:"+entity.getName()+"***password:"+entity.getPassword());
}
}
}
之后对方法进行Junit
测试的时候会出现空指针异常
java.lang.NullPointerException
at com.example.test2.service.UserServiceTest.getUserEntityByName(UserServiceTest.java:17)
......
1、先将测试主类Test2ApplicationTests
的访问权限设置为public
@SpringBootTest
public class Test2ApplicationTests {
@Test
void contextLoads() {
}
}
2、UserServiceTest
类继承测试主类
class UserServiceTest extends Test2ApplicationTests
再次单元测试即可通过
Hibernate: select userentity0_.id as id1_0_, userentity0_.name as name2_0_, userentity0_.password as password3_0_ from user userentity0_ where userentity0_.name=?
name:user01***password:YWRtaW4xMjM=
进一步的完善
创建UserController
package com.example.test2.controller;
import com.example.test2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/getusers")
public Object getUserInfo() {
return userService.getAllUser();
}
}
再次运行项目,浏览器访问IP:8080/getusers
打包部署
先执行maven clean
,再执行maven package
在target
目录下会生成jar
包
将jar包上传至某个服务器上,这里以Linux虚拟为例
[root@worker opt]# ll
total 38748
-rw-r--r--. 1 root root 39677552 May 18 15:46 test2-0.0.1-SNAPSHOT.jar
[root@worker opt]#
启动服务
[root@worker opt]# java -jar test2-0.0.1-SNAPSHOT.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.0.RELEASE)
......
2020-05-18 15:41:50.411 INFO 29966 --- [ main] com.example.test2.Test2Application : Started Test2Application in 6.108 seconds (JVM running for 6.6)
浏览器访问虚拟机ip:8080/getusers
至此开发环境搭建完成,经测试一切都OK。
遇到的问题
如果在写代码的实体类时,使用了lombok
插件,但是却找不到实体对应的Get/Set
方法时,需要在File>settings>plugins>搜索lombok>
找到对应的插件,选择安装,然后重启IDEA
即可。