根据需求分析,准备创建的数据库表:
创建数据库
CREATE DATABASE jdpc;
使用数据
USE jdpc;
创建表
CREATE TABLE `jd_item` (
`id` BIGINT(10) NOT NULL AUTO_INCREMENT COMMENT '主键编号',
`spu` BIGINT(15) DEFAULT NULL COMMENT 'spu',
`sku` BIGINT(15) DEFAULT NULL COMMENT 'sku',
`title` VARCHAR(100) DEFAULT NULL COMMENT '商品标题',
`price` BIGINT(10) DEFAULT NULL COMMENT '商品价格',
`image` VARCHAR(200) DEFAULT NULL COMMENT '商品图片',
`itemurl` VARCHAR(200) DEFAULT NULL COMMENT '商品详情地址',
`created` DATETIME DEFAULT NULL COMMENT '创建时间',
`updated` DATETIME DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `sku` (`sku`) USING BTREE
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='京东商品表'
创建Maven工程
添加依赖信息
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
<!-- springmvc依赖包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spingdata jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- mysql连接包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- HttpClient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<!-- jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.3</version>
</dependency>
<!-- 工具包 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
application.properties配置信息
spring.datasource.url=jdbc:mysql://localhost:3306/jdpc?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.database=MySQL
spring.jpa.show-sql=true
编写pojo
根据数据库,编写pojo
记得写get/set
@Entity
@Table(name = "jd_item")
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;//主键
private Long spu;//spu
private Long sku;//sku
private String title;//商品标题
private Double price;//商品价格
private String image;//商品图片
private String itemurl;//商品详情地址
private Date created;//创建时间
private Date updated;//更新时间
}
文件位置和代码
创建Dao
创建service
Service实现类
文件位置
代码实现
创建引导类
添加代码
运行检查一下看是否报错,没有错,成功