티스토리 뷰
gradle로 jpa hibernate 빌드하기 with mysql
project 구조
c:\practice\hibernate_gradle_01 에 만들었다.
project명은 hibernate_gradle_01 로 정했다.
build.gradle
group 'hibernate_gradle_01'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.5
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
// https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager
compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.2.2.Final'
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.6'
}
org.hibernate와 mysql jdbc connector를 import했다.
persistence.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="jpabook"> <class>Member</class> <properties> <property name="javax.persistence.jdbc.Driver" value="com.mysql.jdbc.Driver" /> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/krkim" /> <property name="javax.persistence.jdbc.user" value="root" /> <property name="javax.persistence.jdbc.password" value="password" /> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> <property name="hibernate.show_sql" value="true" /> </properties> </persistence-unit> </persistence> | cs |
db구조
db create문
1 2 3 4 5 6 7 8 9 10 | CREATE TABLE `member` ( `id` VARCHAR(50) NOT NULL COLLATE 'utf16_bin', `name` VARCHAR(50) NULL DEFAULT NULL COLLATE 'utf16_bin', `age` VARCHAR(50) NULL DEFAULT NULL COLLATE 'utf16_bin', PRIMARY KEY (`id`) ) COLLATE='utf16_bin' ENGINE=InnoDB ; | cs |
Member.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; /** * Created by grkim on 2016-08-22. */ @Entity @Table(name="member") public class Member { @Id @Column(name="id") private String id; @Column(name = "name") private String username; private Integer age; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } | cs |
Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; import java.util.List; /** * Created by grkim on 2016-08-22. */ public class Main { public static void main(String[] args) { EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpabook"); EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); try { tx.begin(); logic(em); tx.commit(); } catch (Exception e) { System.out.println(e); tx.rollback(); } } public static void logic(EntityManager em){ String id = "id3"; Member member = new Member(); member.setId(id); member.setUsername("kyeongrok"); member.setAge(2); em.persist(member); Member findMember = em.find(Member.class, id); System.out.println(findMember.getUsername() + findMember.getAge()); member.setAge(20); findMember = em.find(Member.class, id); System.out.println(findMember.getUsername() + findMember.getAge()); em.remove(member); } } | cs |
'Language > JAVA' 카테고리의 다른 글
Java DB연동 되었는지 확인하는 코드 (0) | 2016.12.16 |
---|---|
java timestamp 생성 (0) | 2016.12.02 |
java8 Interface를 lambda로 생성하기 (0) | 2016.06.19 |
java 1초에 한번씩 특정 method실행하는 TimerTask (2) | 2016.06.18 |
java 람다로 무한루프 스레드 돌리기 (0) | 2016.06.18 |
- Total
- Today
- Yesterday
- 도커각티슈박스
- docker container whale
- docker container case
- 도커컨테이너
- Sh
- 도커티슈박스
- vim
- 2017 티스토리 결산
- docker container tissue
- shellscript
- docker container
- 개발자
- Linux
- 싱가폴
- 이직
- 도커각티슈케이스
- docker container tissue box
- 도커티슈케이스
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |