1.编码方式
1.1pom.xml配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.2.0</version>
</dependency>
1.2application.properties配置
spring.datasource.driverClassName=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc\:oracle\:thin\:@localhost\:1521\:orcl
spring.datasource.username=admin
spring.datasource.password=admin
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jackson.serialization.indent_output=true
1.3项目目录
domain
pojo的存放位置
dao
dao的存放位置
web
控制器层
1.4User.java
package com.shuai.spring_boot_1.domain;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "t_user")
public class User implements Serializable{
private static final long serialVersionUID = -644511628313198178L;
@Id
@GeneratedValue
private Integer id;
private String name;
private int age;
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
}
1.5UserRepository.java
package com.shuai.spring_boot_1.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import com.shuai.spring_boot_1.domain.User;
public interface UserRepository extends JpaRepository<User, Integer>{
}
1.6OneController.java
package com.shuai.spring_boot_1.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.shuai.spring_boot_1.dao.UserRepository;
import com.shuai.spring_boot_1.domain.User;
@RestController
public class OneController {
@Autowired
private UserRepository userRepository;
@RequestMapping("/add")
public String add() {
User user = new User();
user.setId(1);
user.setName("123");
user.setAge(20);
userRepository.save(user);
System.out.println("保存成功");
return "ok";
}
}
1.7App.java
package com.shuai.spring_boot_1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
1.8运行项目
运行App.java中的main方法
1.9访问项目
http://localhost:8080/add