码迷,mamicode.com
首页 > 编程语言 > 详细

springdatajpa多表对多表有额外字段的情况处理

时间:2019-11-14 18:21:53      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:jackson   ali   char   structure   foreign   text   incr   字段   -o   

准备

  • jdk1.8或以上版本
  • maven 3.6或以上版本

技术

  • spring data jpa
  • springmvc

pom文件

  

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 
  3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5     <modelVersion>4.0.0</modelVersion>
  6 
  7     <groupId>com.lzy</groupId>
  8     <artifactId>demo</artifactId>
  9     <version>1.0</version>
 10     <packaging>war</packaging>
 11 
 12     <name>demo Maven Webapp</name>
 13     <!-- FIXME change it to the project‘s website -->
 14     <url>http://www.example.com</url>
 15 
 16 
 17     <properties>
 18         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 19         <maven.compiler.source>1.8</maven.compiler.source>
 20         <maven.compiler.target>1.8</maven.compiler.target>
 21 
 22 
 23         <org.springframework.version>5.0.7.RELEASE</org.springframework.version>
 24 
 25 
 26     </properties>
 27 
 28     <dependencies>
 29         <!-- https://mvnrepository.com/artifact/junit/junit -->
 30 
 31         <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
 32         <dependency>
 33             <groupId>commons-codec</groupId>
 34             <artifactId>commons-codec</artifactId>
 35             <version>1.13</version>
 36         </dependency>
 37 
 38 
 39         <dependency>
 40             <groupId>javax.transaction</groupId>
 41             <artifactId>javax.transaction-api</artifactId>
 42             <version>1.2</version>
 43         </dependency>
 44 
 45         <dependency>
 46             <groupId>junit</groupId>
 47             <artifactId>junit</artifactId>
 48             <version>4.12</version>
 49             <scope>test</scope>
 50         </dependency>
 51 
 52 
 53         <dependency>
 54             <groupId>org.hibernate</groupId>
 55             <artifactId>hibernate-entitymanager</artifactId>
 56             <version>5.1.7.Final</version>
 57         </dependency>
 58 
 59         <dependency>
 60             <groupId>org.springframework</groupId>
 61             <artifactId>spring-orm</artifactId>
 62             <version>${org.springframework.version}</version>
 63         </dependency>
 64 
 65         <dependency>
 66             <groupId>org.springframework.data</groupId>
 67             <artifactId>spring-data-jpa</artifactId>
 68             <version>2.1.8.RELEASE</version>
 69         </dependency>
 70 
 71 
 72         <dependency>
 73             <groupId>commons-lang</groupId>
 74             <artifactId>commons-lang</artifactId>
 75             <version>2.6</version>
 76         </dependency>
 77 
 78         <!-- servlet-api -->
 79         <dependency>
 80             <groupId>javax.servlet</groupId>
 81             <artifactId>servlet-api</artifactId>
 82             <version>2.5</version>
 83             <scope>provided</scope>
 84         </dependency>
 85         <!--mysql驱动-->
 86         <dependency>
 87             <groupId>mysql</groupId>
 88             <artifactId>mysql-connector-java</artifactId>
 89             <version>5.1.21</version>
 90         </dependency>
 91         <!-- druid -->
 92 
 93         <!-- common-lang -->
 94 
 95 
 96         <dependency>
 97             <groupId>org.springframework</groupId>
 98             <artifactId>spring-test</artifactId>
 99             <version>${org.springframework.version}</version>
100             <scope>test</scope>
101         </dependency>
102 
103         <dependency>
104             <groupId>org.springframework</groupId>
105             <artifactId>spring-core</artifactId>
106             <version>${org.springframework.version}</version>
107         </dependency>
108 
109         <dependency>
110             <groupId>org.springframework</groupId>
111             <artifactId>spring-beans</artifactId>
112             <version>${org.springframework.version}</version>
113         </dependency>
114 
115         <dependency>
116             <groupId>org.springframework</groupId>
117             <artifactId>spring-context</artifactId>
118             <version>${org.springframework.version}</version>
119         </dependency>
120 
121         <dependency>
122             <groupId>org.springframework</groupId>
123             <artifactId>spring-context-support</artifactId>
124             <version>${org.springframework.version}</version>
125         </dependency>
126 
127         <dependency>
128             <groupId>org.springframework</groupId>
129             <artifactId>spring-expression</artifactId>
130             <version>${org.springframework.version}</version>
131         </dependency>
132 
133         <dependency>
134             <groupId>org.springframework</groupId>
135             <artifactId>spring-jdbc</artifactId>
136             <version>${org.springframework.version}</version>
137         </dependency>
138 
139         <dependency>
140             <groupId>org.springframework</groupId>
141             <artifactId>spring-tx</artifactId>
142             <version>${org.springframework.version}</version>
143         </dependency>
144 
145         <dependency>
146             <groupId>org.springframework</groupId>
147             <artifactId>spring-web</artifactId>
148             <version>${org.springframework.version}</version>
149         </dependency>
150 
151         <dependency>
152             <groupId>org.springframework</groupId>
153             <artifactId>spring-aop</artifactId>
154             <version>${org.springframework.version}</version>
155         </dependency>
156 
157         <dependency>
158             <groupId>org.springframework</groupId>
159             <artifactId>spring-webmvc</artifactId>
160             <version>${org.springframework.version}</version>
161         </dependency>
162 
163 
164         <dependency>
165             <groupId>org.aspectj</groupId>
166             <artifactId>aspectjrt</artifactId>
167             <version>1.7.4</version>
168         </dependency>
169 
170         <dependency>
171             <groupId>org.aspectj</groupId>
172             <artifactId>aspectjweaver</artifactId>
173             <version>1.7.4</version>
174         </dependency>
175 
176 
177         <dependency>
178             <groupId>cglib</groupId>
179             <artifactId>cglib</artifactId>
180             <version>3.1</version>
181         </dependency>
182 
183 
184 
185 
186         <dependency>
187             <groupId>com.fasterxml.jackson.core</groupId>
188             <artifactId>jackson-core</artifactId>
189             <version>2.9.4</version>
190         </dependency>
191 
192         <dependency>
193             <groupId>com.fasterxml.jackson.core</groupId>
194             <artifactId>jackson-databind</artifactId>
195             <version>2.9.4</version>
196         </dependency>
197 
198         <dependency>
199             <groupId>com.fasterxml.jackson.core</groupId>
200             <artifactId>jackson-annotations</artifactId>
201             <version>2.9.4</version>
202         </dependency>
203 
204 
205         <dependency>
206             <groupId>org.slf4j</groupId>
207             <artifactId>slf4j-api</artifactId>
208             <version>1.7.6</version>
209         </dependency>
210 
211         <dependency>
212             <groupId>org.slf4j</groupId>
213             <artifactId>slf4j-log4j12</artifactId>
214             <version>1.7.6</version>
215         </dependency>
216 
217         <dependency>
218             <groupId>log4j</groupId>
219             <artifactId>log4j</artifactId>
220             <version>1.2.17</version>
221         </dependency>
222         <!--commons-fileupload-->
223         <dependency>
224             <groupId>commons-fileupload</groupId>
225             <artifactId>commons-fileupload</artifactId>
226             <version>1.3.1</version>
227         </dependency>
228         <!-- jstl -->
229         <dependency>
230             <groupId>jstl</groupId>
231             <artifactId>jstl</artifactId>
232             <version>1.2</version>
233         </dependency>
234         <!-- standard -->
235         <dependency>
236             <groupId>taglibs</groupId>
237             <artifactId>standard</artifactId>
238             <version>1.1.2</version>
239         </dependency>
240         <!--pagehelper-->
241 
242 
243         <dependency>
244         <groupId>mysql</groupId>
245         <artifactId>mysql-connector-java</artifactId>
246         <version>5.1.21</version>
247         </dependency>
248 
249         <dependency>
250             <groupId>aopalliance</groupId>
251             <artifactId>aopalliance</artifactId>
252             <version>1.0</version>
253         </dependency>
254 
255 
256         <dependency>
257             <groupId>org.springframework.data</groupId>
258             <artifactId>spring-data-jpa</artifactId>
259             <version>2.1.8.RELEASE</version>
260         </dependency>
261 
262         <dependency>
263             <groupId>com.alibaba</groupId>
264             <artifactId>druid</artifactId>
265             <version>1.0.14</version>
266         </dependency>
267 
268 
269     </dependencies>
270 
271 
272         
273 </project>

sql文件

/*
 Navicat Premium Data Transfer

 Source Server         : 47.101.143.152
 Source Server Type    : MySQL
 Source Server Version : 50728
 Source Host           : 47.101.143.152:3306
 Source Schema         : bookShop

 Target Server Type    : MySQL
 Target Server Version : 50728
 File Encoding         : 65001

 Date: 09/11/2019 19:16:30
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for book
-- ----------------------------
DROP TABLE IF EXISTS `book`;
CREATE TABLE `book`  (
  `bookId` int(11) NOT NULL AUTO_INCREMENT,
  `bookName` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `bookAuthor` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `bookPrice` int(11) NOT NULL,
  `bookContent` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `bookPicture` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `bookTypeId` int(11) NOT NULL,
  `createTime` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  PRIMARY KEY (`bookId`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 12 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of book
-- ----------------------------
INSERT INTO `book` VALUES (1, java编程基础, 黄药师, 50, java编程基础是好书啊!好书。好书啊!好书。好书啊!好书。, java01.jpg, 1, 2017-09-12);
INSERT INTO `book` VALUES (2, java从入门到精通, 欧阳锋, 60, java从入门到精通是好书啊!好书。好书啊!好书。好书啊!好书。, java02.jpg, 1, 2017-09-15);
INSERT INTO `book` VALUES (3, javaWeb开发技术大全, 洪七公, 65, javaWeb开发技术大全是好书啊!好书。好书啊!好书。好书啊!好书。, java03.jpg, 1, 2017-09-05);
INSERT INTO `book` VALUES (4, javaWeb开发技术详解, 黄药师, 63, javaWeb开发技术详解是好书啊!好书。好书啊!好书。好书啊!好书。, java04.jpg, 1, 2017-09-06);
INSERT INTO `book` VALUES (5, C程序设计, 洪七公, 30, C程序设计是好书啊!好书。好书啊!好书。好书啊!好书。, c01.jpg, 2, 2017-09-22);
INSERT INTO `book` VALUES (6, C语言基础教程, 欧阳锋, 35, C语言基础教程是好书啊!好书。好书啊!好书。好书啊!好书。, c02.jpg, 2, 2017-09-12);
INSERT INTO `book` VALUES (7, C语言程序设计实训, 黄药师, 40, C语言程序设计实训是好书啊!好书。好书啊!好书。好书啊!好书。, c03.jpg, 2, 2017-09-23);
INSERT INTO `book` VALUES (8, VB.NET程序设计, 洪七公, 45, VB.NET程序设计是好书啊!好书。好书啊!好书。好书啊!好书。, net01.jpg, 3, 2017-09-25);
INSERT INTO `book` VALUES (9, CLR VIA C#, 黄药师, 55, CLR VIA C#是好书啊!好书。好书啊!好书。好书啊!好书。, net02.jpg, 3, 2017-09-01);
INSERT INTO `book` VALUES (10, intro to ASP.NET, 洪七公, 58, intro to ASP.NET是好书啊!好书。好书啊!好书。好书啊!好书。, net03.jpg, 3, 2017-09-11);
INSERT INTO `book` VALUES (11, 一次就掌握ASP.NET, 欧阳锋, 66, 一次就掌握ASP.NET是好书啊!好书。好书啊!好书。好书啊!好书。, net04.jpg, 3, 2017-09-19);

-- ----------------------------


-- ----------------------------
-- Table structure for cart
-- ----------------------------
DROP TABLE IF EXISTS `cart`;
CREATE TABLE `cart`  (
  `cartId` int(11) NOT NULL AUTO_INCREMENT,
  `bookId` int(11) NOT NULL,
  `userId` int(11) NOT NULL,
  `bookQuantity` int(11) NOT NULL,
  PRIMARY KEY (`cartId`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;



-- ----------------------------
-- Table structure for users
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users`  (
  `userId` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `password` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  PRIMARY KEY (`userId`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of users
-- ----------------------------
INSERT INTO `users` VALUES (1, test, 111);
INSERT INTO `users` VALUES (2, oracle, 222);


SET FOREIGN_KEY_CHECKS = 1;
 1 @Entity
 2 @Table(name = "book")
 3 
 4 public class Book {
 5 
 6   @GeneratedValue(strategy = GenerationType.IDENTITY)
 7   @Id
 8   private Integer bookId;
 9   private String bookName;
10   private String bookAuthor;
11   private Integer bookPrice;
12   private String bookContent;
13   private String bookPicture;
14   private Integer bookTypeId;
15   private String createTime;
16   @OneToMany(mappedBy = "book")
17   private List<Cart> carts = new ArrayList<>(0);
18 
19 //get set 省略
20 
21 
22 
43 1 @Entity
44  2 @Table(name = "users")
45  3 public class Users {
46  4 
47  5   @Id
48  6   @GeneratedValue(strategy = GenerationType.IDENTITY)
49  7   private Integer userId;
50 
51  
52 
53  8   private String username;
54  9   private String password;
55 10 
56 11   @OneToMany(mappedBy = "user")
57 12   private  List<Cart> carts = new ArrayList<>(0);
58 13 
59 14 //get set 省略
60 
61 
62 
63 @Entity
64 @Table(name = "cart")
65 public class Cart {
66 
67   @Id
68   @GeneratedValue(strategy = GenerationType.IDENTITY)
69   private Integer cartId;
70 
71   @ManyToOne
72   @JoinColumn(name = "bookId",referencedColumnName = "bookId")
73   private Book book;
74 
75   @ManyToOne
76   @JoinColumn(name = "userId",referencedColumnName = "userId")
77   private Users user;
78 
79   @Column(name = "bookQuantity")
80   private Integer bookQuantity;
//get set 省略

 

测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/spring/applicationContext.xml")
public class testdb {
    @Autowired
    private BookDao bookDao;

    @Autowired
    private Userdao userdao;

    @Autowired
    private CartDao cartDao;

   
    @Test
    public void test3(){
        Users users = new Users();
        Book book = new Book();
        book.setBookId(4);
        users.setUsername("lzy");
        users.setUserId(5);
        book.setBookName("hello");
        Cart cart = new Cart();
        cart.setBookQuantity(20);
        cart.setBook(book);
        cart.setUser(users);
        userdao.save(users);
        bookDao.save(book);
        cartDao.save(cart);

    }
}

 

springdatajpa多表对多表有额外字段的情况处理

标签:jackson   ali   char   structure   foreign   text   incr   字段   -o   

原文地址:https://www.cnblogs.com/liuzhongyiboke/p/11858774.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!