最近在学SpringBoot,将原来的Eclipse工具换成了idea(用起来有点吃力很多快捷键需要百度),简单入了个门(或许入门都算不上),总之,将这些天学习的知识做个总结,记录以此。
一、idea部分:
1.关于idea的下载
idea分为两个版本,分别为旗舰版和社区版。简单了解了一下这两个版本的区别在于,旗舰版的功能较社区办更加完善,所以这里推荐使用的是idea旗舰版。
下载链接:https://www.jetbrains.com/idea/
主页简单粗暴直接点download就可以了
之后会有两个版本以及操作系统的选择
Ultimate是旗舰版,Community是社区版,我本地的操作系统是Windows,这里推荐使用旗舰版。
不过旗舰版只有一个月的免费时间(学生可凭学生证免费使用),要想继续使用可以到网上找相关的注册码或者有相应的破解,这里就不详细说了。
2.idea创建SpringBoot框架的Web工程
选择Spring Initializr,我这里用的是jdk8,点击下一步
之后输入项目的信息,这里要说的是Packageing选择的方式是Jar的形式(Web项目的Packageing依然是Jar),点击下一步
这里勾选上Web即可,下一步
输入项目名称,和路径,点击完成,这样一个简单的SpringBoot框架的web工程就创建好了。
附上idea快捷键说明:https://www.cnblogs.com/zhangpengshou/p/5366413.html
二、SpringBoot(用CRUD来说明知识点)
1.创建表product_category(数据库为Mysql)
Sql:
CREATE TABLE `product_category` (
`category_id` int NOT NULL AUTO_INCREMENT ,
`category_name` varchar(64) NOT NULL ,
`category_type` int NOT NULL ,
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
PRIMARY KEY (`category_id`),
UNIQUE KEY `uqe_category_type` (`category_type`)
) ;
2.创建POJO ProductCategory
代码如下:
package com.wx.sell.pojo; import lombok.Data; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import java.util.Date; /** * @author QiangHu * @date 2018/3/5 下午 3:45 */ @Entity @Data public class ProductCategory { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer categoryId; private String categoryName; private Integer categoryType; private Date createTime; public ProductCategory() { } }
注解解释:
@Entity:Spring容器自动创建对象
@Id:用于标识POJO对应数据库中表的主键
@GeneratedValue(strategy = GenerationType.IDENTITY):用于标识主键生成策略
@Data:代替GET、Set方法。这里需要导入lombok的依赖
除此之外,还需要在idea中加入lombok。
加入插件方法:在Idea中按Ctrl+Shift+A,输如plugins
打开并点击安装(我这边已经安装过显示的是卸载)
3.创建接口ProductCategoryDao
这里的持久层用的是JPA(底层封装的是hibernate)
加入mysql连接依赖,以及JPA的依赖
接口ProductCategoryDao继承JpaRepository,代码如下:
package com.wx.sell.dao; import com.wx.sell.pojo.ProductCategory; import org.springframework.data.jpa.repository.JpaRepository; /** * @author: QiangHu * @date: 2018/3/5 0005 下午 3:56 */ public interface ProductCategoryDao extends JpaRepository<ProductCategory,Integer> { }其中 JpaRepository<ProductCategory,Integer>的泛型分别表示:
ProductCategory 响应的POJO
Integer 主键类型,如果主键的类型是String,则此处填写String
4.将application.properties文件改为application.yml文件
加入配置:
spring: datasource: driver-class-name: com.mysql.jdbc.Driver username: root password: 123456 url: jdbc:mysql://127.0.0.1/wechatsell?characterEncoding=utf-8&useSSL=false jpa: show-sql: true
5.测试dao层
右键接口名-》GO TO-》Test
测试代码如下:
package com.wx.sell.dao; import com.wx.sell.pojo.ProductCategory; 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 static org.junit.Assert.*; /** * @author: QiangHu * @date: 2018/3/5 0005 下午 6:54 */ @RunWith(SpringRunner.class) @SpringBootTest @Transactional public class ProductCategoryDaoTest { @Autowired private ProductCategoryDao productCategoryDao; @Test public void getTest() { ProductCategory result = productCategoryDao.getOne(1); System.out.println(result.toString()); } @Test public void saveTest(){ ProductCategory productCategory = new ProductCategory(); productCategory.setCategoryName("生活品"); productCategory.setCategoryType(2); productCategoryDao.save(productCategory); } @Test public void updateTest(){ ProductCategory productCategory = new ProductCategory(); productCategory.setCategoryName("热门"); productCategory.setCategoryId(1); productCategoryDao.save(productCategory); } @Test public void deleteTest(){ ProductCategory productCategory = new ProductCategory(); productCategoryDao.deleteById(1); } }
@Transactional :开启事务,在Service层加入时异常会回滚,在测试时加入无论如何都会回滚。并不会延迟加载
dao的方法过于简单,都是看方法能猜到功能系列,其中save方法即是插入 又是更新,这些大家应该都能看懂,就不多解释了
还有service层和Controller后续会继续更新