微信点餐系统

项目搭建与配置

一、搭建项目

参考前面笔记

二、配置

1
2
3
4
5
6
7
8
spring:
datasource:
url: jdbc:mysql://10.128.26.223:3306/sell?characterEncoding=utf-8&useSSL=false
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
jpa:
show-sql: true
这个主要是数据库的配置

三、测试

1、写一个ProductCategory的bean,对应数据的一张表。主要注解
@Entity、
@Id、
GeneratedValue 的作用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@Entity
public class ProductCategory {

/**
* 类目ID
*/
@Id
@GeneratedValue
private Integer categoryId;

/**
* 类目名称
*/
private String categoryName;

/**
* 类目分类编号
*/
private Integer categoryType;

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 getCategoryType() {
return categoryType;
}

public void setCategoryType(Integer categoryType) {
this.categoryType = categoryType;
}

@Override
public String toString() {
return "ProductCategory{" +
"categoryId=" + categoryId +
", categoryName='" + categoryName + '\'' +
", categoryType=" + categoryType +
'}';
}
}
如果在ProduceCategory类中新增了CreateTime 与 UpdateTime,由于数据设置了默认时间,为了使数据库的的改字段更新要在类上加上@DynamicUpdate注解.
为了避免平凡的写getter、setter方法,可以使用@Data、@Getter、@Setter注解代替,要使用这个注解需要加入依赖:
1
2
3
4
      <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@Entity
@DynamicUpdate
@Data
public class ProductCategory {

/**
* 类目ID
*/
@Id
@GeneratedValue
private Integer categoryId;

/**
* 类目名称
*/
private String categoryName;

/**
* 类目分类编号
*/
private Integer categoryType;

// private Date createTime;

// private Date updateTime;


}
2、写一个jpa的 接口
1
2
public interface ProductCategoryRepository extends JpaRepository<ProductCategory,Integer> {
}
3、对Jpa方法进行测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductCategoryRepositoryTest {

@Autowired
private ProductCategoryRepository repository;

@Test
public void testFindOne() {
ProductCategory productCategory = repository.findOne(1);
System.out.println(productCategory);
}

@Test
public void testSaveOne() {
ProductCategory productCategory = new ProductCategory();
productCategory.setCategoryName("女生最爱");
productCategory.setCategoryType(211);
repository.save(productCategory);
}

}
文章目录
  1. 1. 一、搭建项目
  2. 2. 二、配置
  3. 3. 三、测试