Skip to content

Commit bab995a

Browse files
committed
springboot-jpa JPA
1 parent 0867d95 commit bab995a

File tree

5 files changed

+21
-13
lines changed

5 files changed

+21
-13
lines changed

springboot-data-jpa/pom.xml

+2-9
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<artifactId>spring-boot-starter-thymeleaf</artifactId>
4444
</dependency>
4545

46+
<!-- apo 切面支持 -->
4647
<dependency>
4748
<groupId>org.springframework.boot</groupId>
4849
<artifactId>spring-boot-starter-aop</artifactId>
@@ -55,7 +56,6 @@
5556
<version>1.2.47</version>
5657
</dependency>
5758

58-
5959
<!-- Lombok 工具 -->
6060
<dependency>
6161
<groupId>org.projectlombok</groupId>
@@ -97,19 +97,12 @@
9797
<artifactId>mysql-connector-java</artifactId>
9898
</dependency>
9999

100-
<!-- 引入druid数据源这里应该使用和springboot整合的jar。不然sql监控无效 -->
101-
<!--<dependency>-->
102-
<!--<groupId>com.alibaba</groupId>-->
103-
<!--<artifactId>druid</artifactId>-->
104-
<!--<version>1.1.12</version>-->
105-
<!--</dependency>-->
100+
<!-- 阿里 druid 数据源 -->
106101
<dependency>
107102
<groupId>com.alibaba</groupId>
108103
<artifactId>druid-spring-boot-starter</artifactId>
109104
<version>1.1.10</version>
110105
</dependency>
111-
112-
113106
</dependencies>
114107

115108
<build>

springboot-data-jpa/src/main/java/net/codingme/boot/domain/User.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package net.codingme.boot.domain;
22

3+
34
import lombok.AllArgsConstructor;
4-
import lombok.Builder;
55
import lombok.Data;
66
import lombok.NoArgsConstructor;
77
import org.springframework.format.annotation.DateTimeFormat;
88

99
import javax.persistence.*;
1010
import javax.validation.constraints.NotNull;
1111
import java.util.Date;
12-
import java.util.List;
1312

1413
/**
1514
* <p>
@@ -64,5 +63,4 @@ public class User {
6463
* 技能
6564
*/
6665
private String skills;
67-
6866
}

springboot-data-jpa/src/main/java/net/codingme/boot/domain/repository/UserRepository.java

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
@Repository
1414
public interface UserRepository extends JpaRepository<User, Integer> {
1515

16+
/**
17+
* 一个自定义方法,根据 username 和 password 查询 User 信息
18+
*/
1619
User findByUsernameAndPassword(String username, String password);
1720

1821
}

springboot-data-jpa/src/main/resources/application.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ spring.jpa.database-platform=org.hibernate.dialect.MySQL55Dialect
4343
#person.dog.name=旺财
4444
#person.dog.age=1
4545

46-
############################################################
46+
############################################################x
4747
# 生成随机值
4848
bootapp.secret=$ {random.value}
4949
bootapp.number=$ {random.int}

springboot-data-jpa/src/test/java/net/codingme/boot/SpringbootDataJpaApplicationTests.java

+14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package net.codingme.boot;
22

3+
import com.alibaba.fastjson.JSON;
4+
import net.codingme.boot.domain.User;
5+
import net.codingme.boot.domain.repository.UserRepository;
36
import org.junit.Test;
47
import org.junit.runner.RunWith;
58
import org.springframework.beans.factory.annotation.Autowired;
@@ -8,6 +11,7 @@
811

912
import javax.sql.DataSource;
1013
import java.sql.SQLException;
14+
import java.util.List;
1115

1216
@RunWith(SpringRunner.class)
1317
@SpringBootTest
@@ -16,6 +20,16 @@ public class SpringbootDataJpaApplicationTests {
1620
@Autowired
1721
private DataSource dataSource;
1822

23+
@Autowired
24+
private UserRepository userRepository;
25+
26+
@Test
27+
public void testUser(){
28+
List<User> one = userRepository.findAll();
29+
System.out.println(JSON.toJSONString(one));
30+
}
31+
32+
1933
@Test
2034
public void contextLoads() throws SQLException {
2135
System.out.println(dataSource.getClass());

0 commit comments

Comments
 (0)