springboot加springdatajpa实现简单的增删改查

新建一个maven项目,指定其父项目为springboot,再添加spring data jpa的坐标(定义了一系列的crud接口【我们要做的就是在dao层写接口继承这些spring data jpa封装实现好的父接口,我们的dao层就可以直接获得简单的crud功能了】,其背后spring通过代理,默认使用hibernate来实现之-----因此可以不用引入hibernate的坐标),再添加spring-boot-starter-web坐标(引入springmvc,供我们浏览器测试使用。)。最后再添加mysql 的驱动连接即可:

pom.xml:



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>
  <parent>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-parentartifactId>
    <version>2.1.6.RELEASEversion>
    <relativePath/> 
  parent>
  <groupId>comgroupId>
  <artifactId>newSjpaartifactId>
  <version>1.0-SNAPSHOTversion>
  <packaging>warpackaging>

  <name>newSjpa Maven Webappname>
  
  <url>http://www.example.comurl>

  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>1.7maven.compiler.source>
    <maven.compiler.target>1.7maven.compiler.target>
  properties>

  <dependencies>
    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <scope>runtimescope>
    dependency>
    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-data-jpaartifactId>
    dependency>
    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-webartifactId>
    dependency>
  dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-maven-pluginartifactId>
      plugin>
    plugins>
  build>

project>

写个与数据库对应的pojo类

@Entity
@Table(name = "user")
public class UserEntity implements Serializable {
    @Id
    @GeneratedValue
    private Integer id;
    @Column(name = "username")
    private String username;
    @Column(name ="password")
    private String password;
    @Column(name ="sex")
    private String sex;
    @Column(name ="photo_url")
    private String photoUrl;
    @Column(name ="email")
    private String email;
    @Column(name = "type")
    private Integer type;
    @Column(name ="register_date")
    private String registerDate;

    @Column(name ="signature")
    private String signature;
    @Column(name ="level")
    private Integer level;
    @Column(name ="active_code")
    private String activeCode;
    @Column(name ="has_active")
    private Integer hasActive;
//省略getter和setter方法.............

UserDao层的代码如下:

public interface UserDao extends JpaRepository<UserEntity,Long> ,JpaSpecificationExecutor<UserEntity> , Serializable {
}

controller层代码如下:

@RestController
public class HiController {
    @Autowired
    UserDao userDao;
    @RequestMapping
    public List<UserEntity>hi(){
        return userDao.findAll();
    }
}

application.properties配置如下:

server.port=8000
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/bbs?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.database=mysql
spring.jpa.show-sql=true

加个springbootapplication的注解,写个sprongboot项目的启动类,调一下springApplication类的静态run方法,启动整个项目:

@SpringBootApplication
public class StartApp {
    public static void main(String[]args){
        SpringApplication.run(StartApp.class,args);
    }
}

你可能感兴趣的:(springboot)