1.POM依赖
<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.github.carter659</groupId>
<artifactId>Demo02-JPA</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<name>Demo02-JPA</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--############## JPA 相关包 ############## -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- MYSQL 连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.项目树及各层快速食用
- 表结构
- 项目树
- 配置文件
#服务端口
server.port=8080
# 地址
spring.datasource.url=jdbc:mysql://localhost:3306/supervisory-console?characterEncoding=utf8&useSSL=true
# 账户
spring.datasource.username=root
# 密码
spring.datasource.password=123456
# 显示执行SQL语句
spring.jpa.show-sql=true
#############更多的相关配置自行查阅#############
- Entity(User.java)
@Entity
@Table(name = "user")
public class User implements Serializable {
/** serialVersionUID. */
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private int id;
private String name;
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;
}
}
- Dao(ITestDao.java)
public interface ITestDao extends JpaRepository<User, Integer> {
List<User> findByName(String name);
}
- Service(TestService.java)
@Service
public class TestService {
@Autowired
ITestDao testDao;
public List<User> getUsers(String name){
return testDao.findByName(name);
}
}
- Controller(TestController)
@RestController
public class TestController {
@Autowired
TestService service;
@RequestMapping("/test")
public List<User> getUserList(@RequestParam String name){
return service.getUsers(name);
}
}
- App.java
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
3.运行
2条
1条