1.Maven的pom.xml中的依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
application.properties中的一些MySQL配置
pring.datasource.url=jdbc:mysql://127.0.0.1/book?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
spring.datasource.username=root
spring.datasource.password=rootroot
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql= true
spring.thymeleaf.cache=false
server.port=8080
实现类的文件结构和一些接口实现:
详细见demo地址
https://gitee.com/xgkp/Spring8_2_5.git
测试文件中的必要代码:
@RunWith(SpringRunner.class)
@SpringBootTest
//@Transactional
public class ArticleServiceImplTest {
@Autowired
private ArticleRepository articleRepository;
@Test
public void testQuery(){
List<Article> list = articleRepository.findAll();
for (Article article : list){
System.out.println(article);
}
}
@Test
public void testRollBank(){
Article article = new Article();
article.setKeyword("keyword");
article.setTitle("title");
article.setAvaiable(true);
article.setType("图");
article.setDescription("描述");
article.setBody("body");
articleRepository.save(article);
}
}
执行测试进行自动化建表和添加数据
Article实体类中的代码:
@Entity
@Table(name = "article")
public class Article {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
public Article() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(nullable = false,unique = true)
@NotEmpty(message = "标题不能为空")
private String title;
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Boolean getAvaiable() {
return avaiable;
}
public void setAvaiable(Boolean avaiable) {
this.avaiable = avaiable;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
@Column(columnDefinition = "enum('图','文','图文')")
private String type;
private Boolean avaiable = Boolean.FALSE;
@Size(min = 0,max = 20)
private String keyword;
public void setKeyword(String keyword) {
this.keyword = keyword;
}
public String getKeyword() {
return keyword;
}
@Size(max = 255)
private String description;
@Column(nullable = false)
private String body;
@Transient
private List keywordsList;
public List getKeywordsList(){
return Arrays.asList(this.keyword.trim().split("|"));
}
public void setKeywordsList(List keywordsLists){
this.keywordsList = keywordsLists;
}
}
Respository中的核心代码
public interface ArticleRepository extends JpaRepository<Article, Long> {
Article findArticleById(Long id);
}
父类ArticleService声明
public interface ArticleService {
public List<Article> getArticleList();
public Article findArticleById(Long id);
}
public class ArticleServiceImpl implements ArticleService
注意这里的类和接口的声明和实现
public class ArticleServiceImpl implements ArticleService {
@Autowired
private ArticleRepository articleRepository;
@Override
public List<Article> getArticleList() {
return articleRepository.findAll();
}
@Override
public Article findArticleById(Long id) {
return articleRepository.getById(id);
}
}