Skip to content

Commit 17d8f56

Browse files
Jpa
1 parent ae205a2 commit 17d8f56

File tree

4 files changed

+96
-1
lines changed

4 files changed

+96
-1
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.compont;
2+
3+
import javax.persistence.*;
4+
5+
/**
6+
* Created by zhuzhengping on 2017/3/13.
7+
*/
8+
9+
@Entity
10+
@Table(name = "tp_author")
11+
public class Author_jpa {
12+
13+
@Id
14+
@GeneratedValue(strategy = GenerationType.AUTO)
15+
private Long id;
16+
17+
@Column(name = "real_name")
18+
private String realname;
19+
20+
@Column(name = "nick_name")
21+
private String nickName;
22+
23+
public Long getId() {
24+
return id;
25+
}
26+
27+
public void setId(Long id) {
28+
this.id = id;
29+
}
30+
31+
public String getRealname() {
32+
return realname;
33+
}
34+
35+
public void setRealname(String realname) {
36+
this.realname = realname;
37+
}
38+
39+
public String getNickName() {
40+
return nickName;
41+
}
42+
43+
public void setNickName(String nickName) {
44+
this.nickName = nickName;
45+
}
46+
}

src/main/java/com/controller/AuthorController_mybatis.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Created by zhuzhengping on 2017/3/10.
1616
* 使用mybatis时,要注明扫描的包
1717
*
18-
*
18+
*
1919
*/
2020
@RestController
2121
@RequestMapping(value = "/data/mybatis/author")

src/main/java/com/dao/UserRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
/**
1515
* Created by zhuzhengping on 2017/2/14.
16+
* jpa 的实现方式有良知,一种是继承jparepository的方法
17+
* 还有一种是调用entitymanage的方法
1618
*/
1719
public interface UserRepository extends JpaRepository<User,Long> {
1820

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.dao.impl;
2+
3+
import com.compont.Author;
4+
import com.dao.AuthorDao_jdbc;
5+
import org.springframework.stereotype.Repository;
6+
7+
import javax.persistence.EntityManager;
8+
import javax.persistence.PersistenceContext;
9+
import java.util.List;
10+
11+
/**
12+
* Created by zhuzhengping on 2017/3/13.
13+
* jpa 的实现方式有良知,一种是继承jparepository的方法
14+
* 还有一种是调用entitymanage的方法
15+
*
16+
* 值得注意的是,这个的from对象名,而不是具体的表名
17+
*/
18+
@Repository
19+
public class AuthorDaoImpl_jpa implements AuthorDao_jdbc {
20+
21+
@PersistenceContext
22+
private EntityManager entityManager;
23+
@Override
24+
public int add(Author author) {
25+
return 0;
26+
}
27+
28+
@Override
29+
public int update(Author author) {
30+
return 0;
31+
}
32+
33+
@Override
34+
public int delete(Long id) {
35+
return 0;
36+
}
37+
38+
@Override
39+
public Author findAuthor(Long id) {
40+
return this.entityManager.createQuery("select t from Author t where id = ?1",Author.class).setParameter(1,id).getSingleResult();
41+
}
42+
43+
@Override
44+
public List<Author> findAuthorList() {
45+
return this.entityManager.createQuery("select t from Author t",Author.class).getResultList();
46+
}
47+
}

0 commit comments

Comments
 (0)