eclipse中搭建springboot學習(7)---JPA使用1
在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); } }
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.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 "新增成功"; } }
整體路徑如下: