0. 修改grade镜像,使用阿里云地址,以便于快速加载依赖
参照大佬博客 =====> 阿里云maven镜像
# 项目目录下的build.gradlerepositories {maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'}mavenLocal()mavenCentral()}

# 或者找到GRADLE_HOME/init.d/   或者是 GRADLE_HOME/init.d/
# .gradle目录下新建init.gradle文件allprojects{repositories {def REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public/'all { ArtifactRepository repo ->if(repo instanceof MavenArtifactRepository){def url = repo.url.toString()if (url.startsWith('https://repo1.maven.org/maven2') || url.startsWith('https://jcenter.bintray.com/')) {project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."remove repo}}}maven {url REPOSITORY_URL}}
}

1. 添加mysql依赖,添加JPA依赖
参照大神博客 =====>  mysql依赖
# 项目build.gradlebuildscript {ext {springBootVersion = '1.5.9.RELEASE'}repositories {mavenLocal()mavenCentral()maven { url 'http://repo.spring.io/plugins-release' }}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")}
}apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'group = 'com.dante.imooc'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8repositories {mavenCentral()
}dependencies {runtime('mysql:mysql-connector-java')compile('org.springframework.boot:spring-boot-starter-data-jpa')compile('org.springframework.boot:spring-boot-starter-web')compile('org.projectlombok:lombok')testCompile('org.springframework.boot:spring-boot-starter-test')
}

然后在resources目录下新建application.yml下新增mysql的连接信息
# application.yml
spring:datasource:driver-class-name: com.mysql.jdbc.Driverusername: rootpassword: 123456url: jdbc:mysq:10.14.207.135/sell?characterEncoding=utf-8&useSSL=falsejpa:show-sql: truedatabase: mysql
server:context-path: /sell
#logging:
#  pattern:
##    console: "%d - %msg%n" # 指定控制到输出格式,但日志文件可以看到详细信息
##  path: /var/log/tomcat/   # 指定输出路径
#  file: /var/log/tomcat/sell.log  #指定输出文件
#  level: debug #指定日志级别
#  level:
#    com.dante.imooc.sell.LoggerTest: debug #指定某一类的日志级别

3.创建实体类
首先新建一个 dataobject目录存放所有的实体类,然后新建一个跟数据库表名对应的类。JPA会把驼峰命名法的类名,映射成数据库的 "_" 以此来完成映射。我们也可以使用@Table(name="")来完成映射。
步骤1. 新建实体类及属性名,对应数据的字段
步骤2. 通过Entity注解声明实体
步骤3. 通过Id声明属性为主键,通过GeneratedValue注明生成策略
步骤4. alt + insert 插入setter and getter
package com.dante.imooc.sell.dataobject;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;/*** @Author: Dante* @Desciption: 类目表* @Date: Created in 2018/1/17 0017 17:30* @Nodified By:      in 2018/1/17 0017 17:30**/
@Table(name="product_category")
@Entity
public class ProductCategory {/** 类目id。*/@Id@GeneratedValueprivate Integer categoryId;/**类目名字。*/private String categoryName;/**类目类型。*/private Integer category_type;/**类目描述。*/private String category_desc;public Integer getCategoryId() {return categoryId;}public void setCategoryId(Integer categoryId) {this.categoryId = categoryId;}public String getCategoryName() {return categoryName;}public void setCategoryName(String categoryName) {this.categoryName = categoryName;}public Integer getCategory_type() {return category_type;}public void setCategory_type(Integer category_type) {this.category_type = category_type;}public String getCategory_desc() {return category_desc;}public void setCategory_desc(String category_desc) {this.category_desc = category_desc;}
}

参考链接:springBoot常用注解
优化方案:利用lombok插件完成简单的getter,setter,toString方法,然后重写构造方法,注意一定要有一个无参的构造方法。
引入lombok:
# build.gradle
compile('org.projectlombok:lombok')
//    lombok插件,需要导入,然后IDEA安装Lombok Plugin

在实体类中使用@Data注解:
package com.dante.imooc.sell.dataobject;import lombok.Data;
import org.hibernate.annotations.DynamicUpdate;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;/*** @Author: Dante* @Desciption: 类目表* @Date: Created in 2018/1/17 0017 17:30* @Nodified By:      in 2018/1/17 0017 17:30**/
@Table(name="product_category")
@Entity
@DynamicUpdate  //动态更新
@Data           //包含生成getter,setter,和toString
public class ProductCategory {/** 类目id。*/@Id@GeneratedValueprivate Integer categoryId;/**类目名字。*/private String categoryName;/**类目类型。*/private Integer categoryType;/**类目描述。*/private String categoryDesc;/**创建时间。*/private Date createTime;/**更新时间。*/private Date updateTime;public ProductCategory(String categoryName, Integer categoryType, String categoryDesc) {this.categoryName = categoryName;this.categoryType = categoryType;this.categoryDesc = categoryDesc;};public ProductCategory() {}
}

4.利用JPA快速构建DAO类,实现对数据库的基本操作
package com.dante.imooc.sell.dataobject;import lombok.Data;
import org.hibernate.annotations.DynamicUpdate;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;/*** @Author: Dante* @Desciption: 类目表* @Date: Created in 2018/1/17 0017 17:30* @Nodified By:      in 2018/1/17 0017 17:30**/
@Table(name="product_category")
@Entity
@DynamicUpdate  //动态更新
@Data           //包含生成getter,setter,和toString
public class ProductCategory {/** 类目id。*/@Id@GeneratedValueprivate Integer categoryId;/**类目名字。*/private String categoryName;/**类目类型。*/private Integer categoryType;/**类目描述。*/private String categoryDesc;/**创建时间。*/private Date createTime;/**更新时间。*/private Date updateTime;public ProductCategory(String categoryName, Integer categoryType, String categoryDesc) {this.categoryName = categoryName;this.categoryType = categoryType;this.categoryDesc = categoryDesc;};public ProductCategory() {}
}

5.完成对DAO层的单元测试
package com.dante.imooc.sell.dao;import com.dante.imooc.sell.dataobject.ProductCategory;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import javax.transaction.Transactional;import java.util.Arrays;
import java.util.List;import static org.junit.Assert.*;/*** productCategory接口测试* @Author: Dante* @Desciption: 测试接口* @Date: Created in 2018/1/18 0018 17:18* @Nodified By:      in 2018/1/18 0018 17:18*/
@RunWith(SpringRunner.class)
@SpringBootTestpublic class ProductCategoryDaoTest {@Autowiredprivate ProductCategoryDao dao;@Test@Transactionalpublic void saveTest() {ProductCategory productCategory = new ProductCategory("尿素", 2, "尿素,又称碳酰胺(carbamide),是由碳、氮、氧、氢组成的有机化合物是一种白色晶体。最简单的有机化合物之一,是哺乳动物和某些鱼类体内蛋白质代谢分解的主要含氮终产物。也是目前含氮量最高的氮肥。\n" +"作为一种中性肥料,尿素适用于各种土壤和植物。它易保存,使用方便,对土壤的破坏作用小,是目前使用量较大的一种化学氮肥。工业上用氨气和二氧化碳在一定条件下合成尿素。");ProductCategory result = dao.save(productCategory);Assert.assertNotEquals(null, result);}@Testpublic void modifyTest() {ProductCategory productCategory = new ProductCategory();productCategory.setCategoryId(1);productCategory.setCategoryName("复合肥");productCategory.setCategoryType(1);productCategory.setCategoryDesc("复合肥料是指含有两种或两种以上营养元素的化肥,复合肥具有养分含量高、副成分少且物理性状好等优点,对于平衡施肥,提高肥料利用率,促进作物的高产稳产有着十分重要的作用。\n" +"但它也有一些缺点,比如它的养分比例总是固定的,而不同土壤、不同作物所需的营养元素种类、数量和比例是多样的。因此,使用前最好进行测土,了解田间土壤的质地和营养状况,另外也要注意和单元肥料配合施用,才能得到更好的效果。");dao.save(productCategory);}@Testpublic void findOneTest() {ProductCategory productCategory = dao.findOne(1);System.out.println(productCategory.toString());}@Testpublic void findByCategoryTypeInTest() {List<Integer> list = Arrays.asList(1,2);List<ProductCategory> result = dao.findByCategoryTypeIn(list);Assert.assertNotNull(result);}}

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">

来自为知笔记(Wiz)

Spring Boot微信点餐——实战开发DAO层相关推荐

  1. 如何利用Spring Boot 微信点餐开源系统

    由于细节内容实在太多啦,所以只把部分知识点整理出来粗略的介绍,每个小节点里面都有更细化的内容! 接下来开始分享啦 架构 前后端分离: 补充: setting.xml 文件的作用:settings.xm ...

  2. Spring Boot 微信点餐开源系统!

    点击上方"码农突围",马上关注 这里是码农充电第一站,回复"666",获取一份专属大礼包 真爱,请设置"星标"或点个"在看&quo ...

  3. Spring Boot 微信点餐开源系统

    架构 前后端分离: Nginx与Tomcat的关系在这篇文章,几分钟可以快速了解: " https://www.jianshu.com/p/22dcb7ef9172 补充: setting. ...

  4. 基于Spring Boot的点餐微信小程序设计与实现

    摘 要 近年来,国民收入的提高各个行业都得到了长足发展,其中也带动着互联网行业飞速发展,许多传统行业开始与互联网结合并通过数字化的改造.转型与升级创造出新的发展生态.尤其在国人最关注的"吃& ...

  5. Spring Boot Vue Element入门实战(完结)

    最近给朋友做一个大学运动会管理系统,用作教学案例,正好自己也在自学VUE,决定用spring boot vue做一个简单的系统.vue这个前端框架很火,他和传统的Jquery 编程思路完全不一样,Jq ...

  6. spring boot + vue + element-ui全栈开发入门——基于Electron桌面应用开发

     前言 Electron是由Github开发,用HTML,CSS和JavaScript来构建跨平台桌面应用程序的一个开源库. Electron通过将Chromium和Node.js合并到同一个运行时环 ...

  7. Spring Boot 单元测试详解+实战教程

    转载自   Spring Boot 单元测试详解+实战教程 Spring Boot 的测试类库 Spring Boot 提供了许多实用工具和注解来帮助测试应用程序,主要包括以下两个模块. spring ...

  8. Spring Boot:(四)开发Web应用之JSP篇

    Spring Boot:(四)开发Web应用之JSP篇 前言 上一篇介绍了Spring Boot中使用Thymeleaf模板引擎,今天来介绍一下如何使用SpringBoot官方不推荐的jsp,虽然难度 ...

  9. Spring Boot:(三)开发Web应用之Thymeleaf篇

    Spring Boot:(三)开发Web应用之Thymeleaf篇 前言 Web开发是我们平时开发中至关重要的,这里就来介绍一下Spring Boot对Web开发的支持. 正文 Spring Boot ...

最新文章

  1. Vue的阻止冒泡与阻止默认
  2. Android典型界面设计(3)——访网易新闻实现双导航tab切换
  3. 数据库原理与应用(SQL Server)笔记 第一章 数据定义语言和数据操纵语言
  4. Linux下安装Perl模块
  5. python爬虫运行不出结果_请问这个为什么就是爬不到,运行之后电脑卡的不行,求大佬指导...
  6. GCC/G++编译过程
  7. 大数据从入门到就业的四个必备常识
  8. Android graphic: bitmap and it's principle
  9. 网络管理与维护作业2
  10. ajaxFileUpload 异步上传文件简单使用
  11. 数字图像处理-空间域图像增强
  12. jsp综合开发实例——夏日九宫格日记网
  13. C++程序设计基础(揣锦华版)课后习题答案-第一章:程序设计基础知识
  14. iOS3DTouch功能实现
  15. SPCA5XX摄像头驱动源码分析
  16. DHCP如何分配IP地址
  17. org.quartz.JobPersistenceException: Couldn‘t store job:
  18. 我学ERP 之 金蝶ERP-K3_第4章 销售管理
  19. CANBridge系列本安型智能CAN总线隔离中继器
  20. Java实现 LeetCode 374 猜数字大小 II

热门文章

  1. linux控制流程,Linux-流程控制:for 循环
  2. 自然语言理解属于计算机应用的那个范畴,基于自然语言理解的3D场景构造研究-计算机应用技术专业论文.docx...
  3. 笔记本x31搭建家用win服务器系统,Thinkpad X31怎么硬盘安装win7系统
  4. 计算机硬件市场调查清单,微型计算机组装与维护实用教程王际川第8章节组装计算机.ppt...
  5. linux dd文件系统,原来dd命令也可以模拟块设备(文件系统)读写
  6. java 百分比相加_2019年Java面试题基础系列228道(5),快看看哪些你还不会?
  7. VPS批量管理软件--远程桌面批量管理
  8. Java虚拟机详解(七)------虚拟机监控和分析工具(1)——命令行
  9. Python中断多重循环的几种思路
  10. 转MQTT SERVER 性能测试报告