数据库沿用上次的数据库
1使用jpa链接数据库
修改pom.xml文件,添加两个依赖库:
org.springframework.boot
spring-boot-starter-data-jpa
com.h2database
h2
runtime
com.alibaba
fastjson
1.2.62
更新。
2 新建实体Author
在包com.example.demo.models新建类Author,Author.java代码如下:
package com.example.demo.models;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import com.alibaba.fastjson.JSON;
@Entity
@Table(name = "authors")
public class Author {
/**
* id
*/
@Id
@GeneratedValue
private Long id;
/**
* 作者名字
*/
@Column(nullable = false)
private String name;
/**
* 作者年龄
*/
@Column(nullable = false)
private int age;
/**
* 简介
*/
@Column(nullable = false)
private String introduction;
/**
* 空参构造
*/
public Author() {
}
/**
* 有参构造
*/
public Author(String name, int age, String introduction) {
this.name = name;
this.age = age;
this.introduction = introduction;
}
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;
}
public String getIntroduction() {
return introduction;
}
public void setIntroduction(String introduction) {
this.introduction = introduction;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return JSON.toJSONString(this);
}
}
3 创建dao
在包com.example.demo.dao创建AuthorRepository,AuthorRepository.java代码如下:
package com.example.demo.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.demo.models.Author;
@Repository
public interface AuthorRepository extends JpaRepository {
}
4 业务处
在包com.example.demo.service创建接口IAuthorService,IAuthorService.java代码如下:
package com.example.demo.service;
import java.util.List;
import com.example.demo.models.Author;
public interface IAuthorService {
/**
* 获取所有 Author
*/
List findAll();
/**
* 新增 Author
*
* @param Author {@link Author}
*/
Author insertByAuthor(Author author);
/**
* 更新 Author
*
* @param Author {@link Author}
*/
Author update(Author author);
/**
* 删除 Author
*
* @param id 编号
*/
void delete(Long id);
/**
* 获取 Author
*
* @param id 编号
*/
Author findById(Long id);
}
5 实现业务
在包com.example.demo.service创建AuthorServiceImpl,实现业务接口IAuthorService,AuthorServiceImpl.java代码如下:
package com.example.demo.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.dao.AuthorRepository;
import com.example.demo.models.Author;
@Service
public class AuthorServiceImpl implements IAuthorService {
@Autowired
private AuthorRepository authorRepository;
@Override
public List findAll() {
return authorRepository.findAll();
}
@Override
public Author insertByAuthor(Author author) {
return authorRepository.save(author);
}
@Override
public Author update(Author author) {
return authorRepository.save(author);
}
@Override
public void delete(Long id) {
authorRepository.deleteById(id);
}
@Override
public Author findById(Long id) {
return authorRepository.getOne(id);
}
}
6 jpa配置
修改application.properties文件,代码如下:
#mysql\u914D\u7F6E
spring.datasource.url=jdbc:mysql://localhost:3306/itcaststore?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# config hibernate
#spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
7 测试
在src/test/java目录下的包com.example.demo.service中,创建测试文件AuthorServiceTest,AuthorServiceTest.java代码如下:
package com.example.demo.service;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.example.demo.models.Author;
@RunWith(SpringRunner.class)
@SpringBootTest
public class AuthorServiceTest {
@Autowired
private IAuthorService service;
@Test
public void findAll() {
List list = service.findAll();
for (Author author : list) {
System.out.println(author.toString());
}
System.out.println("打印结束");
}
@Test
public void insert() {
Author a = new Author("张三", 42, "浙江工业职业技术学院");
service.insertByAuthor(a);
}
}
执行insert,成功后,再执行findAll,可以看到新增加的数据。
打开MySQL数据库,可以看到数据库中增加了一张authors表。
8 完毕
关于找一找教程网
本站文章仅代表作者观点,不代表本站立场,所有文章非营利性免费分享。
本站提供了软件编程、网站开发技术、服务器运维、人工智能等等IT技术文章,希望广大程序员努力学习,让我们用科技改变世界。
[第07讲 连接到MySQL数据库03之jpa]http://www.zyiz.net/tech/detail-93449.html