1 环境配置

  JDK  1.8

  MAVEN  3.5

  MYSQL  5.7

  VirtualBox  5.1

2 搭建MYSQL环境

  下载 VM 和 虚拟镜像文件

  虚拟镜像文件:点击前往

  技巧01:安装完virtualBox后直接点击下载好的镜像文件就会自动调到导入镜像文件页面

  问题01:启动虚拟机时启动失败,提示物理机的64位内核没有开启

  解决01:进入系统bios页面,开启虚拟内核即可(详情请问问度娘)

# 虚拟机说明文档
VirtualBox-5.1.
虚拟机系统 centos7.
账号 root
密码
#### 包括软件
* jdk 1.8.0_111
* nginx 1.11.
* mysql 5.7.
* redis 3.2. ##### jdk
* 路径 /usr/local/jdk1..0_111 ##### nginx
* 路径 /usr/local/nginx
* 启动 nginx
* 重启 nginx -s reload ##### mysql
* 配置 /etc/my.conf
* 账号 root
* 密码
* 端口
* 启动 systemctl start mysqld
* 停止 systemctl stop mysqld ##### redis
* 路径 /usr/local/redis
* 配置 /etc/reis.conf
* 端口
* 密码
* 启动 systemctl start redis
* 停止 systemctl stop redis ##### tomcat
* 路径 /usr/local/tomcat
* 启动 systemctl start tomcat
* 停止 systemctl stop tomcat

镜像文件说明

3 编写实体类

  3.1 注解介绍

    @Table(name = "product_category")  实体类和表明对应

    @Entity  该类是一个实体类

    @DynamicUpdate  动态刷新

    @Data  自动为实体类生成get/set/tostring方法

    @Id  主键

    @GeneratedValue  主键自增  

    @Column  指定实体类属性和数据库字段保持一致

    问题01:使用 @Data 后打包后会自动生成相应的方法,但是在IDEA环境运行时需要下载一个插件

    解决01:插件安装教程

     问题02:使用@Data注解时需要导入相应的jar包

    解决02:

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

    问题03:当实体类名和数据库表明不一致时需要利用注解实现一一对应

     解决03:利用@Table指定数据库表名

     问题04:当实体类属性名和数据库表的字段名不一致时需要利用注解实现一一对应

        解决04:利用@Column指定数据库字段名

  3.2 实体类代码

package cn.xinagxu.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; /**
* 类目表实体对象
*/
@Table(name = "product_category")
@Entity
@DynamicUpdate // 实现动态刷新(例如:当数据库中的更新时间字段是自动刷新时,如果修改数据时传入了这个字段的信息时如果不在实体类中添加这个注解那么数据库对该字段的自动更新就会失效)
@Data // 该注解会自动生成一些get/set/tostring方法
public class ProductCategory { /** 类目ID */
@Id
@GeneratedValue
private Integer categoryId;
/** 类目名名字 */
private String categoryName;
/** 类目编号 */
private Integer categoryType;
/** 类目创建时间 */
private Date createTime;
/** 类目更新时间 */
private Date updateTime; public ProductCategory() {
} }

实体类源代码

4 编写Dao层

  4.1 引入数据库相关jar包

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

  4.2 配置数据库连接

spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://172.25.129.244/sell?characterEncoding=utf-8&useSSL=false
jpa:
show-sql: true

  技巧01:持久层必须继承 JpaRepository,继承了该接口后就不用在持久层接口添加注解来设置bean了,因为父类已经实现了

  技巧02:JPA教程

package cn.xinagxu.sell.repository;

import cn.xinagxu.sell.dataObject.ProductCategory;
import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; public interface ProductCategoryRepository extends JpaRepository<ProductCategory, Integer> {
/** 根据一个类型列表去查询类目类型在这个列表中的类目 */
List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList);
}

持久层接口源代码

5 持久层测试类 

package cn.xinagxu.sell.repository;

import cn.xinagxu.sell.dataObject.ProductCategory;
import lombok.extern.slf4j.Slf4j;
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 java.util.Arrays;
import java.util.List; @RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class ProductCategoryRepositoryTest { @Autowired
private ProductCategoryRepository productCategoryRepository; /** 根据主键查询数据 */
@Test
public void findOneTest() {
log.error("===自定义错误日志===");
log.warn("===自定义警告日志===");
log.info("===自定义信息日志===");
log.debug("===自定义调试日志===");
ProductCategory productCategory = productCategoryRepository.findOne(1);
System.out.println(productCategory);
} /** 插入一条数据(主键、创建时间、更新时间是自动实现的不必传入) */
@Test
public void saveOneTest() {
ProductCategory productCategory = new ProductCategory();
productCategory.setCategoryName("女生的最爱");
productCategory.setCategoryType(3);
ProductCategory result = productCategoryRepository.save(productCategory);
Assert.assertNotNull(result); // 利用断言判断:如果结果不为null就表明正确
// Assert.assertNotEquals(null, result); // 利用断言判断:如果结果不为null就表明正确
} /** 根据主键去更新一个条数据 */
@Test
public void updateOneTest() {
ProductCategory productCategory = productCategoryRepository.findOne(2);
productCategory.setCategoryName("三少");
productCategoryRepository.save(productCategory);
} /** 根据类型列表查询 */
@Test
public void findByCategoryTypeTest() {
List<Integer> typeList = Arrays.asList(1,2,3,4);
List<ProductCategory> results = productCategoryRepository.findByCategoryTypeIn(typeList);
Assert.assertNotEquals(0, results.size());
} }

最新文章

  1. 【原】iOS学习之图片拉伸处理(类似qq的气泡)
  2. CSS3动画事件
  3. canvas的默认尺寸
  4. [转]ThreadPoolExecutor线程池的分析和使用
  5. 【转】linux(Ubuntu)配置svn仓库,搭建svn服务器
  6. 利用switch语句进行多选一判断。
  7. ViewPager的使用方法和实现过程
  8. table行转列
  9. Chapter 2 Open Book——30
  10. AWK读书笔记
  11. Spring Boot(一):入门篇+前端访问后端
  12. iptables 指南
  13. linux 下的clock_gettime() 获取时间函数
  14. js 数组、对象转json 以及json转 数组、对象
  15. Echarts动态加载饼状图实例(二)
  16. 利用bat批处理做启动mongodb脚本
  17. jsfl读取xml,图片,并生成swf
  18. 四、获取IP地址工具包
  19. 原生java调用webservice的方法,不用生成客户端代码
  20. php----函数大全

热门文章

  1. PAT 天梯赛 L2-007. 家庭房产 【并查集】
  2. 【leetcode刷题笔记】Single Number
  3. 希尔排序(Shell Sort)
  4. angularjs file upload插件使用总结
  5. css 中 div垂直居中的方法
  6. 十六 Django框架,信号
  7. Windows Server 2008 R2 备份与恢复详细实例
  8. Mybatis异常_04_mapper.xml中不能使用小于号&lt;、大于号&gt;
  9. 素数环:NYOJ--488--dfs||hdu-1016-Prime Ring Problem
  10. leetcode 6 ZigZag Conversion(水题)