Skip to content

Commit 2fb4fab

Browse files
committed
新增spring-data-jpa实践
1 parent 32ac5b4 commit 2fb4fab

29 files changed

+562
-160
lines changed
Lines changed: 19 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,22 @@
1-
----
2-
## [spring-boot-3-logs-Logback 集成Logback日志框架](https://github.com/timebusker/spring-boot/tree/master/spring-boot-3-logs/spring-boot-3-logs-Logback/)
1+
## [spring boot 使用spring-data-jpa让数据访问更简单、更优雅](https://github.com/timebusker/spring-boot/tree/master/spring-boot-10-SpringData/)
32

4-
### 项目阐述
5-
![image](https://github.com/timebusker/spring-boot/raw/master/static/spring-boot-3-logs/spring-boot-3-logs-Logback/Logback.png?raw=true)
6-
7-
+ #### SLF4J+Logback配置说明
8-
* [logback日志分开纪录](http://www.cnblogs.com/DeepLearing/p/5664941.html)</br>
9-
* [logback节点配置详解](http://www.cnblogs.com/DeepLearing/p/5663178.html)
10-
* [logback 中文手册.pdf](https://github.com/timebusker/spring-boot/raw/master/static/spring-boot-3-logs/spring-boot-3-logs-Logback/logback_cn.pdf?raw=true)
11-
12-
+ #### 配置多环境不同日志级别
13-
***logback.xml*配置讲解**
14-
```xml
15-
<?xml version="1.0" encoding="UTF-8"?>
16-
<configuration>
17-
<!-- 文件输出格式 -->
18-
<property name="PATTERN" value="%-12(%d{yyyy-MM-dd HH:mm:ss.SSS}) |-%-5level [%thread] %c [%L] -| %msg%n" />
19-
<!-- test文件路径 -->
20-
<property name="TEST_FILE_PATH" value="c:/logs/test.log" />
21-
<!-- pro文件路径 -->
22-
<property name="PRO_FILE_PATH" value="c:/logs/prod.log" />
3+
![image](https://github.com/timebusker/spring-boot/raw/master/static/10/spring-data-jpa.png?raw=true)
234

24-
<!-- 开发环境 -->
25-
<springProfile name="dev">
26-
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
27-
<encoder>
28-
<pattern>%date [%thread] %-5level %logger{80} || %msg%n</pattern>
29-
</encoder>
30-
</appender>
31-
<logger name="cn.timebusker.util" level="debug" />
32-
<root level="info">
33-
<appender-ref ref="CONSOLE" />
34-
</root>
35-
</springProfile>
5+
- ### 关于spring-data-jpa
6+
+ 在实际开发过程中,对数据库的操作无非就“增删改查”。就最为普遍的单表操作而言,除了表和字段不同外,语句都是类似的,开发人员需要写大量类似而枯燥的语句来完成业务逻辑。
7+
+ 为了解决这些大量枯燥的数据操作语句,我们第一个想到的是使用ORM框架,比如:Hibernate。通过整合Hibernate之后,我们以操作Java实体的方式最终将数据改变映射到数据库表中。
8+
+ 为了解决抽象各个Java实体基本的“增删改查”操作,我们通常会以泛型的方式封装一个模板Dao来进行抽象简化,但是这样依然不是很方便,我们需要针对每个实体编写一个继承自泛型模板Dao的接口,再编写该接口的实现。虽然一些基础的数据访问已经可以得到很好的复用,但是在代码结构上针对每个实体都会有一堆Dao的接口和实现。
9+
+ 由于模板Dao的实现,使得这些具体实体的Dao层已经变的非常“薄”,有一些具体实体的Dao实现可能完全就是对模板Dao的简单代理,并且往往这样的实现类可能会出现在很多实体上。Spring-data-jpa的出现正可以让这样一个已经很“薄”的数据访问层变成只是一层接口的编写方式。
3610

37-
<!-- 测试环境 -->
38-
<springProfile name="test">
39-
<!-- 每天产生一个文件 -->
40-
<appender name="TEST-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
41-
<!-- 文件路径 -->
42-
<file>${TEST_FILE_PATH}</file>
43-
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
44-
<!-- 文件名称 -->
45-
<fileNamePattern>${TEST_FILE_PATH}/info.%d{yyyy-MM-dd}.log</fileNamePattern>
46-
<!-- 文件最大保存历史数量 -->
47-
<MaxHistory>100</MaxHistory>
48-
</rollingPolicy>
49-
<layout class="ch.qos.logback.classic.PatternLayout">
50-
<pattern>${PATTERN}</pattern>
51-
</layout>
52-
</appender>
53-
<root level="info">
54-
<appender-ref ref="TEST-FILE" />
55-
</root>
56-
</springProfile>
57-
58-
<!-- 生产环境 -->
59-
<springProfile name="prod">
60-
<appender name="PROD_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
61-
<file>${PRO_FILE_PATH}</file>
62-
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
63-
<fileNamePattern>${PRO_FILE_PATH}/warn.%d{yyyy-MM-dd}.log</fileNamePattern>
64-
<MaxHistory>100</MaxHistory>
65-
</rollingPolicy>
66-
<layout class="ch.qos.logback.classic.PatternLayout">
67-
<pattern>${PATTERN}</pattern>
68-
</layout>
69-
</appender>
70-
<root level="warn">
71-
<appender-ref ref="PROD_FILE" />
72-
</root>
73-
</springProfile>
74-
</configuration>
75-
```
76-
77-
----
11+
- ### spring-data-jpa常用注解
12+
+ [**@Entity**]() : 指明该类是一个实体Bean,可以通过jpa与数据库表建立映射关系,每个成员代表数据库字段名称,实体Bean的每个实例代表数据表中的一行数据
13+
+ [**@Table**]() : 为被@Entity注释的类所要映射带数据库表,其中@Table.name()用来指定映射表的表名。如果缺省@Table注释,系统默认采用类名作为映射表的表名
14+
+ [**@Id**]() : 指定表的主键属性
15+
+ [**@GeneratedValue**]() : 定义了标识字段生成规则,详细内容参考[ @GeneratedValue 用法](http://blog.csdn.net/fancylovejava/article/details/7438660)
16+
+ [**@Entity**]()
17+
+ [**@Entity**]()
18+
+ [**@Entity**]()
19+
+ [**@Entity**]()
20+
+ [**@Entity**]()
21+
+ [**@Entity**]()
22+
+ [**@Entity**]()

spring-boot-10-SpringData/pom.xml

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0"?>
2-
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
3+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
34
<modelVersion>4.0.0</modelVersion>
45
<parent>
56
<groupId>cn.timebusker</groupId>
@@ -13,7 +14,7 @@
1314
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1415
</properties>
1516
<dependencies>
16-
<!-- Spring Boot 核心三个模块 START -->
17+
<!-- Spring Boot 核心三个模块 START -->
1718
<!-- 核心模块,包括自动配置支持、日志和YAML -->
1819
<dependency>
1920
<groupId>org.springframework.boot</groupId>
@@ -55,9 +56,30 @@
5556
<artifactId>spring-boot-devtools</artifactId>
5657
<optional>true</optional>
5758
</dependency>
59+
<dependency>
60+
<groupId>org.springframework.boot</groupId>
61+
<artifactId>spring-boot-starter-test</artifactId>
62+
<scope>test</scope>
63+
</dependency>
64+
<!-- 添加Spring-Data-JPA的依赖 -->
65+
<dependency>
66+
<groupId>org.springframework.boot</groupId>
67+
<artifactId>spring-boot-starter-data-jpa</artifactId>
68+
</dependency>
69+
70+
<dependency>
71+
<groupId>mysql</groupId>
72+
<artifactId>mysql-connector-java</artifactId>
73+
</dependency>
74+
75+
<dependency>
76+
<groupId>com.alibaba</groupId>
77+
<artifactId>fastjson</artifactId>
78+
</dependency>
5879

5980
<!-- 日志配置开始 -->
60-
<!-- 加上几个重要的实现适配器log4j-over-slf4j、log4j-over-slf4j、jul-to-slf4j, 然后jboss-logging部分也进行了统一的版本控制。同时删除及exclude掉所有log4j、commons-logging的各个版本。 适配器和具体日志实现,不能共存,否则适配器不生效。这样的话,我们只有logback配置文件即可,因为log4j的输出已经委托给了slf4j(通过log4j-over-slf4j),而slf4j的默认实现是logbac -->
81+
<!-- 加上几个重要的实现适配器log4j-over-slf4j、log4j-over-slf4j、jul-to-slf4j, 然后jboss-logging部分也进行了统一的版本控制。同时删除及exclude掉所有log4j、commons-logging的各个版本。
82+
适配器和具体日志实现,不能共存,否则适配器不生效。这样的话,我们只有logback配置文件即可,因为log4j的输出已经委托给了slf4j(通过log4j-over-slf4j),而slf4j的默认实现是logbac -->
6183
<dependency>
6284
<groupId>org.slf4j</groupId>
6385
<artifactId>slf4j-api</artifactId>
Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,17 @@
11
package cn.timebusker;
22

3-
import java.util.Arrays;
4-
53
import org.springframework.boot.SpringApplication;
64
import org.springframework.boot.autoconfigure.SpringBootApplication;
7-
import org.springframework.context.ApplicationContext;
85

96
@SpringBootApplication
107
public class App {
118

12-
/**
13-
* @SpringBootApplication注解的类一定要放在自定义包下且属于自定义包的
14-
*
15-
* @SpringBootApplication is a convenience annotation that adds all of the following:
16-
* @SpringBootApplication是一个方便的注解,增加了所有的以下内容:
17-
*
18-
* @Configuration tags the class as a source of bean definitions for the application context.
19-
* @Configuration 标记一个类来作为bean定义的应用程序上下文的资源
20-
*
21-
* @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
22-
* @EnableAutoConfiguration告诉Spring Boot开始加载基于类路径下的配置信息、beans、各种属性配置。
23-
*
24-
* Normally you would add @EnableWebMvc for a Spring MVC app, but Spring Boot adds
25-
* it automatically when it sees spring-webmvc on the classpath. This flags the application as a web
26-
* application and activates key behaviors such as setting up a DispatcherServlet
27-
* 通常你会添加@EnableWebMvc为Spring MVC的应用程序,但是当Spring Boot在classpath下检索到spring-webmvc时,
28-
* spring boot会自动添加。这标志该应用程序作为Web应用程序,并激活关键行为,如设立的DispatcherServlet
29-
*
30-
* @ComponentScan tells Spring to look for other components, configurations, and services in the the hello package, allowing it to find the HelloController.
31-
* @ComponentScan 告诉Spring寻找其他组件,配置,以及业务层类,最前面才能加载到所有的类。
32-
*/
33-
349
public static void main(String[] args) {
3510
// devtools:是spring boot的一个热部署工具
3611
//设置 spring.devtools.restart.enabled 属性为false,可以关闭该特性.
3712
//System.setProperty("spring.devtools.restart.enabled","false");
3813

3914
// 启动Sprign Boot
40-
ApplicationContext ctx = SpringApplication.run(App.class, args);
41-
System.out.println("Let's inspect the beans provided by Spring Boot:");
42-
String[] beanNames = ctx.getBeanDefinitionNames();
43-
Arrays.sort(beanNames);
44-
for (String beanName : beanNames) {
45-
System.out.println(beanName);
46-
}
15+
SpringApplication.run(App.class, args);
4716
}
4817
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package cn.timebusker.model.many2many;
2+
3+
public class Commodity {
4+
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package cn.timebusker.model.many2many;
2+
3+
public class category {
4+
5+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cn.timebusker.model.one2many;
2+
3+
import java.util.List;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.GenerationType;
9+
import javax.persistence.Id;
10+
import javax.persistence.OneToMany;
11+
import javax.persistence.Table;
12+
13+
/**
14+
* 公司表
15+
*
16+
* @author Administrator
17+
*/
18+
@Entity
19+
@Table(name = "t_company")
20+
public class company {
21+
22+
@Id
23+
@GeneratedValue(strategy = GenerationType.IDENTITY)
24+
private long comId;
25+
26+
@Column(nullable = false, length = 32)
27+
private String comName;
28+
29+
@OneToMany(mappedBy="company")
30+
private List<department> department;
31+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cn.timebusker.model.one2many;
2+
3+
import java.util.List;
4+
5+
import javax.persistence.CascadeType;
6+
import javax.persistence.Column;
7+
import javax.persistence.Entity;
8+
import javax.persistence.GeneratedValue;
9+
import javax.persistence.GenerationType;
10+
import javax.persistence.Id;
11+
import javax.persistence.JoinColumn;
12+
import javax.persistence.ManyToOne;
13+
import javax.persistence.OneToMany;
14+
import javax.persistence.Table;
15+
16+
/**
17+
* 部门表
18+
*
19+
* @author Administrator
20+
*/
21+
@Entity
22+
@Table(name = "t_department")
23+
public class department {
24+
25+
@Id
26+
@GeneratedValue(strategy = GenerationType.IDENTITY)
27+
private long depId;
28+
29+
@Column(nullable = false, length = 32)
30+
private String depName;
31+
32+
@ManyToOne(cascade=CascadeType.ALL)
33+
@JoinColumn(name = "comId")
34+
private company company;
35+
36+
@OneToMany(mappedBy="department")
37+
private List<employee> employee;
38+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cn.timebusker.model.one2many;
2+
3+
import javax.persistence.CascadeType;
4+
import javax.persistence.Column;
5+
import javax.persistence.Entity;
6+
import javax.persistence.GeneratedValue;
7+
import javax.persistence.GenerationType;
8+
import javax.persistence.Id;
9+
import javax.persistence.JoinColumn;
10+
import javax.persistence.OneToOne;
11+
import javax.persistence.Table;
12+
13+
/**
14+
* 员工详细信息表(一个员工一条详细)
15+
*
16+
* @author Administrator
17+
*/
18+
@Entity
19+
@Table(name = "t_detailInfo")
20+
public class detailInfo {
21+
22+
@Id
23+
@GeneratedValue(strategy = GenerationType.IDENTITY)
24+
private long detId;
25+
26+
@Column(nullable = false, length = 32)
27+
private String detName;
28+
29+
@OneToOne(cascade = CascadeType.ALL, optional = false)
30+
@JoinColumn(name = "empId")
31+
private employee employee;
32+
33+
34+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package cn.timebusker.model.one2many;
2+
3+
import java.util.List;
4+
5+
import javax.persistence.CascadeType;
6+
import javax.persistence.Column;
7+
import javax.persistence.Entity;
8+
import javax.persistence.GeneratedValue;
9+
import javax.persistence.GenerationType;
10+
import javax.persistence.Id;
11+
import javax.persistence.JoinColumn;
12+
import javax.persistence.JoinTable;
13+
import javax.persistence.ManyToMany;
14+
import javax.persistence.ManyToOne;
15+
import javax.persistence.OneToOne;
16+
import javax.persistence.Table;
17+
18+
/**
19+
* 员工表
20+
*
21+
* @author Administrator
22+
*/
23+
@Entity
24+
@Table(name = "t_employee")
25+
public class employee {
26+
27+
@Id
28+
@GeneratedValue(strategy = GenerationType.IDENTITY)
29+
private long empId;
30+
31+
@Column(nullable = false, length = 32)
32+
private String empName;
33+
34+
//mappedBy = "employee" 指定了,这个实体是被关系维护端的那个属性所维护
35+
//当我们设置了,mappedBy后,使关系成为双向的时候
36+
//在本例中,detailInfo实体是被 employee实体中的外键“detailInfo”所维护。
37+
@OneToOne(cascade = CascadeType.ALL, optional = false, mappedBy = "employee")
38+
private detailInfo detailInfo;
39+
40+
@ManyToOne(cascade=CascadeType.ALL)
41+
@JoinColumn(name="depId")
42+
private department department;
43+
44+
@ManyToMany(cascade=CascadeType.ALL)
45+
@JoinTable(name="t_em_pos", inverseJoinColumns = @JoinColumn (name = "em_id" ),
46+
joinColumns = @JoinColumn (name = "pos_id" ))
47+
48+
// @JoinTable(name="em_pos",indexes={@Index(columnList = "em_id"),@Index(columnList = "pos_id")},
49+
// inverseJoinColumns = @JoinColumn (name = "em_id" ),
50+
// joinColumns = @JoinColumn (name = "pos_id" ))
51+
private List<position> position;
52+
53+
54+
}

0 commit comments

Comments
 (0)