建立數(shù)據(jù)庫
查詢數(shù)據(jù)服務器IP地址ip address
通過數(shù)據(jù)庫連接工具建立連接
創(chuàng)建新庫
字符集魁衙、排序規(guī)則的選擇
字符集選用utf8,目前足夠了也可以選用utfmb4株搔,它向下兼容utf8能表示更多字符同時也占用更大的空間
排序規(guī)則選用utf8_bin區(qū)分大小寫剖淀,不同場景下排序規(guī)則會影響查詢效率
發(fā)現(xiàn)沒有權限
select * from mysql.user \G
查看權限
之前搭建數(shù)據(jù)服務器的時候明明設置權限了,估計強制重啟服務器時出了問題邪狞,重新賦下權限
grant all privileges on *.* to mysqluser@'%' with grant option;
flush privileges;
新建成功祷蝌,執(zhí)行腳本創(chuàng)建相應的表
創(chuàng)建項目
傻瓜式next操作
image.png
image.png
注意SpringBoot版本,后期可改
項目結構帆卓,啟動入口
修改配置文件application.yml為yml類型
SpringBoot自帶容器可以直接運行
連接數(shù)據(jù)庫
pom添加相關依賴
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
yml配置文件內(nèi)配置數(shù)據(jù)庫信息
spring:
datasource:
url: jdbc:mysql://10.2.27.112/sell?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
username: mysqluser
password: MySQL_user123
創(chuàng)建與表結構對應的實體類PO
PO:persistent object 持久對象
POJO :plain ordinary java object 無規(guī)則簡單java對象
BO:business object 業(yè)務對象
VO:value object 值對象 / view object 表現(xiàn)層對象
DTO(TO):Data Transfer Object 數(shù)據(jù)傳輸對象
DAO:data access object數(shù)據(jù)訪問對象
@Entity//jpa注解巨朦,對應表關系
@Data//lombok自動生成get、set
@DynamicUpdate//更新插入時間
@DynamicInsert
public class ProductInfo {
@Id
private String productId;
private String productName;
private BigDecimal productPrice;
private Integer productStock;
private String productDescription;
private String productIcon;
private Integer productStatus;
private Integer categoryType;
private Date createTime;
private Date updateTime;
}
JPA
public interface ProductInfoRepository extends JpaRepository<ProductInfo, String> {}
創(chuàng)建測試類剑令,測試數(shù)據(jù)庫操作
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductInfoRepositoryTest {
@Autowired
private ProductInfoRepository repository;
@Test
public void insert() {
ProductInfo productInfo = new ProductInfo();
productInfo.setProductId("2");
productInfo.setProductName("商品2");
productInfo.setProductPrice(new BigDecimal(2));
productInfo.setProductStock(2);
productInfo.setCategoryType(2);
Assert.assertNotNull(repository.save(productInfo));
}
@Test
public void select() {
Assert.assertNotNull(repository.findById("1"));
}
@Test
public void update() {
ProductInfo productInfo=repository.findById("1").get();
productInfo.setProductName("商品11");
productInfo.setProductPrice(new BigDecimal(11));
productInfo.setProductStock(11);
productInfo.setCategoryType(11);
Assert.assertNotNull(repository.save(productInfo));
}
@Test
public void delete() {
ProductInfo productInfo=repository.findById("1").get();
repository.delete(productInfo);
}
}