spring data jpa查询demo

柔光的暖阳◎ 2023-02-21 04:56 57阅读 0赞

pom.xml

  1. <properties>
  2. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  3. <hibernate.version>5.0.7.Final</hibernate.version>
  4. <spring.version>5.0.2.RELEASE</spring.version>
  5. <slf4j.version>1.6.6</slf4j.version>
  6. <log4j.version>1.2.12</log4j.version>
  7. <c3p0.version>0.9.1.2</c3p0.version>
  8. <mysql.version>8.0.19</mysql.version>
  9. <maven.compiler.source>1.8</maven.compiler.source>
  10. <maven.compiler.target>1.8</maven.compiler.target>
  11. </properties>
  12. <dependencies>
  13. <dependency>
  14. <groupId>junit</groupId>
  15. <artifactId>junit</artifactId>
  16. <version>4.12</version>
  17. <scope>test</scope>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.aspectj</groupId>
  21. <artifactId>aspectjweaver</artifactId>
  22. <version>1.6.8</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework</groupId>
  26. <artifactId>spring-aop</artifactId>
  27. <version>${spring.version}</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework</groupId>
  31. <artifactId>spring-context</artifactId>
  32. <version>${spring.version}</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework</groupId>
  36. <artifactId>spring-context-support</artifactId>
  37. <version>${spring.version}</version>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework</groupId>
  41. <artifactId>spring-orm</artifactId>
  42. <version>${spring.version}</version>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework</groupId>
  46. <artifactId>spring-beans</artifactId>
  47. <version>${spring.version}</version>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.springframework</groupId>
  51. <artifactId>spring-core</artifactId>
  52. <version>${spring.version}</version>
  53. </dependency>
  54. <dependency>
  55. <groupId>org.hibernate</groupId>
  56. <artifactId>hibernate-entitymanager</artifactId>
  57. <version>${hibernate.version}</version>
  58. </dependency>
  59. <dependency>
  60. <groupId>org.hibernate</groupId>
  61. <artifactId>hibernate-c3p0</artifactId>
  62. <version>${hibernate.version}</version>
  63. </dependency>
  64. <dependency>
  65. <groupId>org.hibernate</groupId>
  66. <artifactId>hibernate-validator</artifactId>
  67. <version>5.2.1.Final</version>
  68. </dependency>
  69. <dependency>
  70. <groupId>c3p0</groupId>
  71. <artifactId>c3p0</artifactId>
  72. <version>${c3p0.version}</version>
  73. </dependency>
  74. <dependency>
  75. <groupId>log4j</groupId>
  76. <artifactId>log4j</artifactId>
  77. <version>${log4j.version}</version>
  78. </dependency>
  79. <dependency>
  80. <groupId>org.slf4j</groupId>
  81. <artifactId>slf4j-api</artifactId>
  82. <version>${slf4j.version}</version>
  83. </dependency>
  84. <dependency>
  85. <groupId>org.slf4j</groupId>
  86. <artifactId>slf4j-log4j12</artifactId>
  87. <version>${slf4j.version}</version>
  88. </dependency>
  89. <dependency>
  90. <groupId>mysql</groupId>
  91. <artifactId>mysql-connector-java</artifactId>
  92. <version>8.0.19</version>
  93. </dependency>
  94. <dependency>
  95. <groupId>org.springframework</groupId>
  96. <artifactId>spring-test</artifactId>
  97. <version>${spring.version}</version>
  98. </dependency>
  99. <dependency>
  100. <groupId>org.springframework.data</groupId>
  101. <artifactId>spring-data-jpa</artifactId>
  102. <version>1.9.0.RELEASE</version>
  103. </dependency>
  104. <dependency>
  105. <groupId>javax.el</groupId>
  106. <artifactId>javax.el-api</artifactId>
  107. <version>2.2.4</version>
  108. </dependency>
  109. <dependency>
  110. <groupId>org.glassfish.web</groupId>
  111. <artifactId>javax.el</artifactId>
  112. <version>2.2.4</version>
  113. </dependency>
  114. </dependencies>
  115. package cn.neu.test;
  116. import cn.neu.dao.CustomerDao;
  117. import cn.neu.domain.Customer;
  118. import org.junit.Test;
  119. import org.junit.runner.RunWith;
  120. import org.springframework.beans.factory.annotation.Autowired;
  121. import org.springframework.data.domain.Page;
  122. import org.springframework.data.domain.PageRequest;
  123. import org.springframework.data.domain.Pageable;
  124. import org.springframework.data.domain.Sort;
  125. import org.springframework.data.jpa.domain.Specification;
  126. import org.springframework.test.context.ContextConfiguration;
  127. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  128. import javax.persistence.criteria.*;
  129. import java.util.List;
  130. /** * @Author WCJ * @Description **/
  131. @RunWith(SpringJUnit4ClassRunner.class)
  132. @ContextConfiguration("classpath:applicationContext.xml")
  133. public class Spec {
  134. @Autowired
  135. private CustomerDao customerDao;
  136. /** * 查询单个对象:根据条件 * @throws Exception */
  137. @Test
  138. public void testSpec() throws Exception{
  139. Specification spec = (root,query,cb)->{
  140. Path custName = root.get("custName");
  141. Predicate perd = cb.equal(custName, "传智播客");
  142. return perd;
  143. };
  144. Customer c = customerDao.findOne(spec);
  145. System.out.println(c);
  146. }
  147. /** * 查询单个对象:根据多个条件 * @throws Exception */
  148. @Test
  149. public void testSpec1() throws Exception{
  150. Specification spec = (root,query,cb)->{
  151. Path custName = root.get("custName");
  152. Path custIndustry = root.get("custIndustry");
  153. Predicate p1 = cb.equal(custName, "传智播客");
  154. Predicate p2 = cb.equal(custIndustry, "教育");
  155. Predicate predicate = cb.and(p1, p2);
  156. return predicate;
  157. };
  158. Customer c = customerDao.findOne(spec);
  159. System.out.println(c);
  160. }
  161. @Test
  162. public void testSpec3() throws Exception{
  163. Specification<Customer> specification = new Specification<Customer>() {
  164. @Override
  165. public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
  166. Path<Object> custName = root.get("custName");
  167. Predicate like = criteriaBuilder.like(custName.as(String.class), "传智播客%");
  168. return like;
  169. }
  170. };
  171. //List<Customer> all = customerDao.findAll(specification);
  172. //all.forEach(System.out::println);
  173. Sort sort = new Sort(Sort.Direction.DESC,"custId");
  174. List<Customer> all = customerDao.findAll(specification, sort);
  175. all.forEach(System.out::println);
  176. }
  177. @Test
  178. public void testPage() throws Exception{
  179. Pageable pageable = new PageRequest(0,2);
  180. Page<Customer> page = customerDao.findAll(null, pageable);
  181. System.out.println(page.getTotalElements());
  182. }
  183. }
  184. package cn.neu.test;
  185. import cn.neu.dao.CustomerDao;
  186. import cn.neu.domain.Customer;
  187. import org.junit.Test;
  188. import org.junit.runner.RunWith;
  189. import org.springframework.beans.factory.annotation.Autowired;
  190. import org.springframework.test.annotation.Rollback;
  191. import org.springframework.test.context.ContextConfiguration;
  192. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  193. import org.springframework.transaction.annotation.Transactional;
  194. import java.util.Arrays;
  195. import java.util.List;
  196. /** * @Author WCJ * @Description **/
  197. @RunWith(SpringJUnit4ClassRunner.class)
  198. @ContextConfiguration("classpath:applicationContext.xml")
  199. public class CustomerTest2 {
  200. @Autowired
  201. private CustomerDao customerDao;
  202. @Test
  203. public void testJpql() throws Exception{
  204. Customer c = customerDao.findJpql("传智播客");
  205. System.out.println(c);
  206. }
  207. @Test
  208. public void testJpql2() throws Exception{
  209. Customer c = customerDao.findCustomerByCustIdAndCustName("传智播客", 1L);
  210. System.out.println(c);
  211. }
  212. @Test
  213. @Transactional
  214. @Rollback(value = false)
  215. public void testUpdate() throws Exception{
  216. customerDao.updateCustomer("黑马",4L);
  217. }
  218. @Test
  219. public void testFindAllSql() throws Exception{
  220. List<Object[]> list = customerDao.findAllSql();
  221. list.stream().forEach(objects -> System.out.println(Arrays.toString(objects)));
  222. }
  223. @Test
  224. public void testFindAllSql2() throws Exception{
  225. List<Object[]> list = customerDao.findAllSql2("传智播客%");
  226. list.stream().forEach(objects -> System.out.println(Arrays.toString(objects)));
  227. }
  228. @Test
  229. public void testName() throws Exception{
  230. Customer c = customerDao.findByCustName("黑马");
  231. System.out.println(c);
  232. }
  233. @Test
  234. public void testName2() throws Exception{
  235. List<Customer> c = customerDao.findByCustNameLike("传智播客%");
  236. c.stream().forEach(System.out::println);
  237. }
  238. }
  239. package cn.neu.test;
  240. import cn.neu.dao.CustomerDao;
  241. import cn.neu.domain.Customer;
  242. import org.junit.Test;
  243. import org.junit.runner.RunWith;
  244. import org.springframework.beans.factory.annotation.Autowired;
  245. import org.springframework.test.context.ContextConfiguration;
  246. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  247. import org.springframework.transaction.annotation.Transactional;
  248. import java.util.List;
  249. /** * @Author WCJ * @Description **/
  250. @RunWith(SpringJUnit4ClassRunner.class)
  251. @ContextConfiguration("classpath:applicationContext.xml")
  252. public class CustomerTest {
  253. @Autowired
  254. private CustomerDao customerDao;
  255. @Test
  256. public void testFindOne() throws Exception{
  257. Customer c = customerDao.findOne(3L);
  258. System.out.println(c);
  259. }
  260. @Test
  261. public void testSave() throws Exception{
  262. Customer c = new Customer();
  263. c.setCustName("java工程师");
  264. c.setCustLevel("高级");
  265. c.setCustIndustry("电商");
  266. customerDao.save(c);
  267. }
  268. @Test
  269. public void testDelete() throws Exception{
  270. customerDao.delete(2L);
  271. }
  272. @Test
  273. public void testFindAll() throws Exception{
  274. List<Customer> customers = customerDao.findAll();
  275. customers.forEach(System.out::println);
  276. }
  277. /** * 测试统计查询 * @throws Exception */
  278. @Test
  279. public void testCount() throws Exception{
  280. long count = customerDao.count();
  281. System.out.println(count);
  282. }
  283. @Test
  284. public void testExist() throws Exception{
  285. boolean exists = customerDao.exists(1L);
  286. System.out.println(exists);
  287. }
  288. @Test
  289. @Transactional
  290. public void testGetOne() throws Exception{
  291. Customer c = customerDao.getOne(1L);
  292. System.out.println(c);
  293. }
  294. }

发表评论

表情:
评论列表 (有 0 条评论,57人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring Data JPA多表查询

    多表查询在Spring Data JPA中有两种实现方式 第一种创建一个结果集接口来接收多表连查的结果 第二种利用JPA的关联映射来实现 先来熟悉一下几个注解 <t