在pom.xml中引入jar包
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.example
demo
0.0.1-SNAPSHOT
jar
demo
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.6.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-data-jpa
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1
com.oracle
ojdbc6
11.2.0.1.0
org.springframework.boot
spring-boot-devtools
true
org.springframework.boot
spring-boot-maven-plugin
true
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必须为Long类型,采用自增长的顺序往数据库添加,并且注意引用正确的包,否则会报错
private Long id;@Column(name = "name", nullable = true, length = 20)
private String name;@Column(name = "age", nullable = true, length = 4)
private int age;public Long getId() {
return id;
}public void setId(Long 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
{ }
新建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
findAll() {
return (List) 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 ListgetPersonList() {
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 "删除成功";
}
}
整体路径如下: