eclipse中搭建springboot学习(7)---JPA使用1

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Gr_lbxx/article/details/83181413

在pom.xml中引入jar包

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
    
          <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-thymeleaf</artifactId>
          </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <!-- JPA依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        
        <!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        
        <!-- oracle -->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.1.0</version>
        </dependency> 

         <!--热部署  -->
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
         </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                <fork>true</fork>
            </configuration>
            </plugin>
        </plugins>
    </build>

</project>
 

application.properties中添加配置

server.port=8089
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url= jdbc:oracle:thin:@192.168.200.61:1521:XXX
spring.datasource.username=XXX
spring.datasource.password=XXX
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
mybatis.mapper-locations=classpath:mapper/*.xml
#如果实体对应的表不存在,会自动创建
spring.jpa.properties.hibernate.hbm2ddl.auto=update

 PersonDTO

package com.example.demo1019.dto;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "person")
@Entity
public class PersonDTO {
    @Id
    @GeneratedValue
    // 这里的id必须为int类型,采用自增长的顺序往数据库添加,并且注意引用正确的包,否则会报错
    private int id;

    @Column(name = "name", nullable = true, length = 20)
    private String name;

    @Column(name = "age", nullable = true, length = 4)
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

PersonRepository

package com.example.demo1019.dao;

import org.springframework.data.jpa.repository.JpaRepository;

import com.example.demo1019.dto.PersonDTO;

public interface PersonRepository extends JpaRepository<PersonDTO, Long> {

}

新建PersonService

package com.example.demo1019.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo1019.dao.PersonRepository;
import com.example.demo1019.dto.PersonDTO;

@Service
public class PersonService {
    @Autowired
    private PersonRepository personRepository;

    public List<PersonDTO> findAll() {
        return (List<PersonDTO>) personRepository.findAll();
    }

    public void save(PersonDTO personDTO) {
        personDTO = new PersonDTO();
        personDTO.setAge(26);
        personDTO.setName("啾啾");
        personRepository.save(personDTO);
    }

    public void deletePerson(Long id) {
        personRepository.deleteById(id);
    }
}

PersonController 

package com.example.demo1019.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo1019.dto.PersonDTO;
import com.example.demo1019.service.PersonService;

@Controller
public class PersonController {
    @Autowired
    private PersonService personService;

    @RequestMapping("/getPersonList")
    @ResponseBody
    public List<PersonDTO> getPersonList() {
        return personService.findAll();
    }

    @RequestMapping("/addPerson")
    @ResponseBody
    public String addPerson(PersonDTO personDTO) {
        personService.save(personDTO);
        return "添加成功";
    }

    @RequestMapping("/deletePerson/{id}")
    @ResponseBody
    public String deletePerson(@PathVariable("id") Long id) {
        personService.deletePerson(id);
        return "删除成功";
    }
}

 整体路径如下:

猜你喜欢

转载自blog.csdn.net/Gr_lbxx/article/details/83181413