Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
データソース設定
# datasource
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:tcp://localhost/~/test
spring.datasource.username=sa
spring.datasource.password=
# connection pool use tomcat
spring.datasource.tomcat.maxActive=10
spring.datasource.tomcat.maxIdle=10
spring.datasource.tomcat.minIdle=10
spring.datasource.tomcat.initialSize=10
spring.datasource.tomcat.defaultAutoCommit=false
# h2 for debug tool
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.h2.console.settings.web-allow-others=true
親子テーブル例
DROP TABLE depart
CREATE TABLE depart (
deid int NOT NULL AUTO_INCREMENT primary key,
dename varchar(20)
)
DROP TABLE employ
CREATE TABLE employ (
eid int NOT NULL AUTO_INCREMENT primary key,
deid int,
name varchar(20),
age int,
CONSTRAINT fk_deid
FOREIGN KEY (deid)
REFERENCES depart (deid)
ON DELETE RESTRICT ON UPDATE RESTRICT
)
データ
INSERT INTO PUBLIC.depart ( deid, dename ) VALUES (1,'テスト');
INSERT INTO PUBLIC.depart ( deid, dename ) VALUES (2,'システム');
INSERT INTO PUBLIC.depart ( deid, dename ) VALUES (3,'事務');
INSERT INTO PUBLIC.employ ( eid, deid, NAME, AGE ) VALUES (10,1,'田中',21);
INSERT INTO PUBLIC.employ ( eid, deid, NAME, AGE ) VALUES (11,1,'佐藤',23);
INSERT INTO PUBLIC.employ ( eid, deid, NAME, AGE ) VALUES (20,2,'鈴木',25);
INSERT INTO PUBLIC.employ ( eid, deid, NAME, AGE ) VALUES (21,2,'安田',30);
INSERT INTO PUBLIC.employ ( eid, deid, NAME, AGE ) VALUES (30,3,'清水',45);
package jx.entity;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="depart")
public class Depart implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@Column(name="deid", unique = true, nullable = false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long deid;
private String dename;
@OneToMany // 多対1
List<Employ> employ;
public Long getDeid() {
return deid;
}
public void setId(Long deid) {
this.deid = deid;
}
public String getDename() {
return dename;
}
public void setDename(String dename) {
this.dename = dename;
}
public List<Employ> getEmploy() {
return employ;
}
public void setEmploy(List<Employ> employ) {
this.employ = employ;
}
}
package jx.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="employ")
public class Employ implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@Column(name="eid", unique = true, nullable = false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long eid;
//private Long deid;
private String name;
private int age;
@ManyToOne //
Depart depart;
public Long getEid() {
return eid;
}
public void setEid(Long eid) {
this.eid = eid;
}
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 Long getDeid() {
// return deid;
// }
// public void setDeid(Long deid) {
// this.deid = deid;
// }
public Depart getDepart() {
return depart;
}
public void setDepart(Depart depart) {
this.depart = depart;
}
}
package jx.repository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import jx.entity.Depart;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
@Repository
public interface DepartRepository extends JpaRepository<Depart, Long> {
public List<Depart> findAll();
}
package jx.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import jx.entity.Depart;
import jx.entity.Employ;
@Repository
public interface EmployRepository extends JpaRepository<Employ, Long> {
}
package com.example.demo;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.test.context.junit4.SpringRunner;
import jx.entity.Depart;
import jx.entity.Employ;
import jx.repository.DepartRepository;
import jx.repository.EmployRepository;
@RunWith(SpringRunner.class)
@SpringBootTest
@ComponentScan( "jx.*" )
@EntityScan("jx.entity" )
@Configuration
@EnableJpaRepositories("jx.*")
public class TestPj3ApplicationTests {
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private DepartRepository departRepository;
@Autowired
private EmployRepository employRepository;
@Test
public void contextLoads() {
System.out.println(222);
try {
//List<Depart> departments = departRepository.findAll();
// Employ employ2 = new Employ();
// employ2.setAge(23);
// employ2.setName("テスト5");
// employ2.setDepart(depart);
insertAll2();
//readUpdate();
//List<Map<String, Object>> ret = jdbcTemplate.queryForList("SELECT * FROM DEPART");
System.out.println(111);
System.out.println(222);
}catch(Exception e) {
e.printStackTrace();
System.out.println(222);
}
}
private void insertAll() {
Depart depart = new Depart();
depart.setDename("自動3");
departRepository.save(depart);
Employ employ1 = new Employ();
employ1.setAge(19);
employ1.setName("テスト");
employ1.setDepart(depart);
employRepository.save(employ1);
}
private void insertAll2() {
Depart depart = new Depart();
depart.setDename("自動5");
Employ employ1 = new Employ();
employ1.setAge(19);
employ1.setName("テスト");
employ1.setDepart(depart);
Employ employ2 = new Employ();
employ2.setAge(19);
employ2.setName("テスト");
employ2.setDepart(depart);
Employ employ3 = new Employ();
employ3.setAge(19);
employ3.setName("テスト");
employ3.setDepart(depart);
List<Employ> employList = new ArrayList<>();
employList.add(employ1);
employList.add(employ2);
employList.add(employ3);
depart.setEmploy(employList);
departRepository.save(depart);
}
private void readUpdate() {
List<Depart> departments = departRepository.findAll();
Depart depart1 = departments.get(0);
Employ employ1 = new Employ();
employ1.setAge(21);
employ1.setName("テスト4");
employ1.setDepart(depart1);
employRepository.save(employ1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.