用的是idea工具创建的spring boot项目,这里就不说了。
就是一个简单的demo
页面操作,通过Controller操作, 用spring-data-jpa来持久化数据库(h2)。所以需要用带的maven依赖有
1. 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
1. spring-boot-starter-web这个就是spring web需要的starter
2. spring-boot-starter-thymeleaf 这个是视图等需要的starter
3. spring-boot-starter-data-jpa 用jap来实现数据的持久化的
4. h2 内嵌的数据库
2. 目录结构说明
TzwBootApplication.java
package com.tzw.boot.tzwboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import javax.persistence.Entity;
/**
* 该注解实现了三个功能
* 1.@Configuration Spring基于Java的配置
* 2.@ComponentScan 启动扫描组件
* 3.@EnableAutoConfiguration 开启了Spring Boot自动配置
*
*/
@SpringBootApplication(scanBasePackages = "com.tzw")
@EnableJpaRepositories(basePackages = "com.tzw")
@EntityScan(basePackages = "com.tzw.entity")
public class TzwBootApplication {
public static void main(String[] args) {
SpringApplication.run(TzwBootApplication.class, args);
}
}
首先这里有几个注解咱们要注意一下。(明天再详细讲哦)
Controller
这里主要做的是调用japRepository持久化数据库的操作,当然主要是看日志,看操作数据库是否成功。至于跟页面的交互并没有做,咱就忽略这个。
package com.tzw.Controller;
import com.tzw.entity.Client;
import com.tzw.reposity.TzwRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Controller
public class ReadingController {
@Autowired
private JpaRepository jpaRepository;
@RequestMapping(value = "/")
@ResponseBody
public String getBookList(){
System.out.println("进入Controller");
return "Hello Word";
}
@RequestMapping(value = "/hello")
public String getHello(){
System.out.println("进入Hello");
return "hello.html";
}
@RequestMapping("/getClient")
public void getClient(){
Client client = new Client();
client.setClientId(1);
client.setClientName("张三");
client.setClientNameEn("zs");
List all = jpaRepository.findAll();
System.out.println(all.toString()+"----------------------");
}
@RequestMapping("/saveClient")
public void getSaveClient(){
Client client = new Client();
// client.setClient_id("2");
client.setClientName("张三");
client.setClientNameEn("zs");
Object save = jpaRepository.save(client);
System.out.println(save.toString()+"-----------------");
}
}
这个有个注解也需要明确一下
@RestController 和 Controller
@ResponseBody
实体类
package com.tzw.entity;
import lombok.Data;
import lombok.Generated;
import lombok.Value;
import javax.persistence.*;
@Data
@Entity
@Table(name = "Client")
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="client_id")
public int clientId;
@Column(name="client_name")
public String clientName;
@Column(name="client_name_en")
public String clientNameEn;
}
需要注意几个注解
Repository
package com.tzw.reposity;
import com.tzw.entity.Client;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Repository
public interface TzwRepository extends JpaRepository<Client, String> {
}
这里要详细讲一下jap的实现流程
application.yml
server:
port: 8380
spring:
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:~/test
username: sa
password:
schema: classpath:db/schema.sql
data: classpath:db/db.sql
jpa:
database: h2
hibernate:
ddl-auto: create-drop
show-sql: true
h2:
console:
path: /h2-console
enabled: true
schema.sql
drop table client;
create table client (
client_id number(10) primary key,
client_name varchar(60),
client_name_en varchar(200)
);
db.sql
insert into client values (1,'张三','zs');
查看h2数据库,当项目启动成功,输入http://localhost:8380/h2-console/,出现下面页面