Jap技术总结

文章目录

  • JPA技术
  • 1. Springboot整合JPA
    • 1.1 引入JPA
    • 1.2配置
    • 1.3 启动类
    • 1.4 实体类
    • 1.5 定义接口和数据库交互(dao)
    • 1.6 建立service(增删改查)接口,并实现
    • 1.7 测试
    • 1.8Test(⊙o⊙)结果

JPA技术

Java Persistence Api(ORM 全自动化框架)

1. Springboot整合JPA

1.1 引入JPA

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-data-jpaartifactId>
dependency>

1.2配置

server:
  port: 8099

#数据源配置
spring:
  datasource:
    druid:
      url: jdbc:mysql://localhost:3306/mybatis_db?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
      username: root
      password: 1234
      driver-class-name: com.mysql.cj.jdbc.Driver
  #json时间处理
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
  #jpa全自动框架
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

logging:
  level:
    com.dyit.springboot: debug

1.3 启动类

@SpringBootApplication
public class Main {

    public static void main(String[] args) {
        SpringApplication.run(Main.class);
    }
}

1.4 实体类

映射数据库的表

@Entity
@Table(name = "Person_tab")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)//根据数据库的主键策略(id自增)
    @Column(name="person_id")
    private Integer id;
    @Column(name="person_name")
    private String name;
    @Column(name="person_gender")
    private String gender;
    @Column(name="person_age")
    private Integer age;
    @Column(name="person_phone")
    private String phone;
}

1.5 定义接口和数据库交互(dao)

@Repository
public interface IPersonDao extends CrudRepository<Person,Integer> {
    Person findByPhone(String phone);
}

1.6 建立service(增删改查)接口,并实现

接口

public interface IPersonService {
    void add(Person person);
    void update(Person person);
    List<Person> findAll();
    Person findByphone(String phone);
}

实现

@Service
@Transactional
public class PersonServiceImpl implements IPersonService {
    @Autowired
    private IPersonDao dao;
    @Override
    public void add(Person person) {

        dao.save(person);
    }

    @Override
    public void update(Person person) {
        dao.save(person);
    }


    @Override
    public List<Person> findAll() {

        return (List<Person>) dao.findAll();
    }

    @Override
    public Person findByphone(String phone) {
        return dao.findByPhone(phone);
    }
}

1.7 测试

鼠标在service实现类后(PersonServiceImpl implements IPersonService),用快捷键Alt+回车,创建Test类

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@Slf4j
public class PersonServiceImplTest {

    @Autowired
    private IPersonService ips;
    @Test
    public void add() {

        ips.add(new Person(null,"齐斯","男",22,"1234567"));
    }

    @Test
    public void update() {
        ips.update(new Person(1,"齐齐","男",22,"12345677"));

    }

    @Test
    public void findAll() {
        log.debug("findAll:"+ips.findAll());
    }

    @Test
    public void findByphone() {
        System.out.println("1234567");
    }
}

1.8Test(⊙o⊙)结果

add()
Jap技术总结_第1张图片

update()
Jap技术总结_第2张图片

findAll()
Jap技术总结_第3张图片

findByphone()通过电话号找人

在这里插入图片描述

你可能感兴趣的:(笔记,java,spring,boot,intellij-idea)