一 添加依赖
导入我们需要的依赖jar包,因为我这里用到的是mysql数据库,所以在pom.xml添加以下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
.二 application.properties文件配置
#mysql数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/springboot
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#jpa在数据库建表的方式
spring.jpa.properties.hibernate.hbm2ddl.auto=create
#d端口号设置
server.port=8060
#显示数据库执行语句
spring.jpa.show-sql=true
#启用shutdown
endpoints.shutdown.enabled=true
#禁用密码验证
endpoints.shutdown.sensitive=false
JPA支持XML和JDK5.0注解两种元数据的形式,元数据描述对象和表之间的映射关系,框架据此将实体对象持久化到数据库表中;所以我们第一次创建表的时候用create,之后就用update。不然像笔者之前犯的低级错误,明明数据库中自己插入数据,执行查询功能的时候,查询出来是空,当时懵逼了一下,最后检查了下配置文件才发现,所以这种低级错误最好避免出现
三 创建entity实体类
由于我只是单纯的模拟一下,所以简单的写了个用户名密码的实体类,具体代码如下:
@Entity
@Table(name="sysuser")//表名
public class SysUser {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)//主键生成策略--递增形式
private Long id;
@Column(nullable=false)//列不为空
private String userName;
@Column(nullable=false)
private String passWord;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
}
四 创建dao层
package com.example.springboot; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.model.SysUser;
@Repository
public interface UserRepository extends JpaRepository<SysUser,Long> {
SysUser findByUserName(String userName);
}
你也可以自定义写方法
五 创建controller层
由于我这边没有啥业务逻辑,所以不用书写service层,具体工作时候还是要写上
package com.example.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.example.model.SysUser;
import com.example.springboot.UserRepository;
@RestController
@RequestMapping("/test")
public class SysUserController {
@Autowired
private UserRepository userRepository;
@RequestMapping(value = "/getSysUser",method = RequestMethod.GET)
public String getSysUser() {
SysUser sysUser=new SysUser();
sysUser.setId((long) 1);
sysUser.setUserName("wang");
sysUser.setPassWord("123");
userRepository.save(sysUser);
SysUser user=userRepository.findByUserName("wang");
return user.toString();
}
}
六 application启动类配置
@SpringBootApplication
//@ComponentScan(basePackages= {"com."})
//@EntityScan("com.example")
public class MyprojectApplication {
public static void main(String[] args) {
SpringApplication.run(MyprojectApplication.class, args);
}
}
新手容易犯错的地方:
由于没有了解启动类配置的作用范围,所以如果你把包自己新建不与启动类包同级或者不在启动类包下,那么启动类配置就扫描不到它,所以报错,但你可以通过@ComponentScan来扫描controller层,service层,dao层,通过@EntityScan来扫描实体类
总结:
其实我们仔细看jpa数据操作层其实就是hibernate,他们之间的关系就是 ,可以简单的理解为JPA是标准接口,Hibernate是实现。通过这种整合,springboot确实比较快速方便。但是后续所用到的注解还是挺多的,所以对几个常用重要的注解还是加强记忆。
补充:上面在application.properties中配置了endpoints.shutdown.enabled=true 这句话,前提我们需要导入一个jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
在我刚开始使用springboot的时候,启动服务后,更改代码后想重启,然后老是报端口被占用,每次都通过dos命令的
netstat -ano|findstr 端口号,然后takkill /pid 端口号 /f,甚是麻烦,后来查找资料才发现pring Boot Actuator
的shutdown endpoin
t默认是关闭的,所以我们需要将其开启,这样子你就方便多了