經過上一章的學習,咱們已經對SpringBoot有簡單的入門,接下來咱們深刻學習一下SpringBoot,咱們知道任何一個網站的數據大多數都是動態的,也就是說數據是從數據庫提取出來的,而非靜態數據,那麼咱們接下來就是要鏈接數據庫,如今咱們常常使用的數據庫的種類能夠大體分爲兩種,關係型數據庫和非關係型數據庫,而MySQL數據庫,Oracle數據庫SQL Server數據庫等都是關係型數據庫,而Redis,Mongodb等都是非關係型數據庫。java
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.demo02</groupId> <artifactId>demo_02</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo_02</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--mybatis-spring適配器--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!--mysql驅動包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.30</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
#建立商品信息表 create table product ( pid int primary key not null auto_increment COMMENT"商品編號", pname varchar(50) COMMENT"商品名稱", pprice DECIMAL(10,2) COMMENT"商品價格", ptime varchar(50) COMMENT"入庫時間", pcount int COMMENT"庫存", pstatus int COMMENT"商品狀態" #0 表明下架,1表明上架 )COMMENT"商品信息表" insert into product(pname,pprice,ptime,pcount,pstatus)VALUES ("蘋果",11,"2019-10-1",11,1) SELECT * from product
package com.ssm.entity; /* * 商品實體類 * */ public class Product { private long pid;//編號 private String pname;//名稱 private double pprice;//價格 private String ptime;//入庫時間 private long pcount;//數量 private long pstatus;//狀態 //無參構造方法 public Product() {} //帶參構造方法 public Product(long pid, String pname, double pprice, String ptime, long pcount, long pstatus) { this.pid = pid; this.pname = pname; this.pprice = pprice; this.ptime = ptime; this.pcount = pcount; this.pstatus = pstatus; } public long getPid() { return pid; } public void setPid(long pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public double getPprice() { return pprice; } public void setPprice(double pprice) { this.pprice = pprice; } public String getPtime() { return ptime; } public void setPtime(String ptime) { this.ptime = ptime; } public long getPcount() { return pcount; } public void setPcount(long pcount) { this.pcount = pcount; } public long getPstatus() { return pstatus; } public void setPstatus(long pstatus) { this.pstatus = pstatus; } @Override public String toString() { return "Product{" + "pid=" + pid + ", pname='" + pname + '\'' + ", pprice=" + pprice + ", ptime='" + ptime + '\'' + ", pcount=" + pcount + ", pstatus=" + pstatus + '}'; } }
#鏈接數據庫的驅動 spring.datasource.driver-class-name=com.mysql.jdbc.Driver #鏈接數據庫的url spring.datasource.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8 #用戶名 spring.datasource.username=root #密碼 spring.datasource.password=123456
package com.ssm.dao; import com.ssm.entity.Product; import org.apache.ibatis.annotations.Select; import java.util.List; public interface ProductDao { @Select("select * from product") List<Product> findAllProduct(); }
package com.ssm.service; import com.ssm.entity.Product; import java.util.List; public interface ProductService { List<Product> findAllProduct(); }
package com.ssm.service.imple; import com.ssm.dao.ProductDao; import com.ssm.entity.Product; import com.ssm.service.ProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class ProductImple implements ProductService { @Autowired private ProductDao productDao; @Override public List<Product> findAllProduct() { return productDao.findAllProduct(); } }
package com.ssm.controller; import com.ssm.entity.Product; import com.ssm.service.ProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class ProductController { @Autowired private ProductService productService; //查詢所有的商品信息 @GetMapping("/product") public List<Product> findAllProduct(){ return productService.findAllProduct(); } }
package com.demo02.demo_02; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages = {"com.ssm.controller","com.ssm.service"}) @MapperScan(basePackages = {"com.ssm.dao"}) public class Demo02Application { public static void main(String[] args) { SpringApplication.run(Demo02Application.class, args); } }
server: port: 8801 eureka: client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://localhost:8801/eureka/
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8 username: root password: 123456 mybatis: typeAliasesPackage: com.ssm.entity mapperLocations: classpath:mapper/*Mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ssm.dao.ProductDao">
<!--查詢所有商品信息-->
<select id="findAllProduct" resultType="product"> select * from product </select> </mapper>
package com.ssm.dao; import com.ssm.entity.Product; import org.apache.ibatis.annotations.Select; import java.util.List; public interface ProductDao { // @Select("select * from product") List<Product> findAllProduct(); }
package com.ssm.service; import com.ssm.entity.Product; import java.util.List; public interface ProductService { List<Product> findAllProduct(); }
package com.ssm.service.imple; import com.ssm.dao.ProductDao; import com.ssm.entity.Product; import com.ssm.service.ProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class ProductImple implements ProductService { @Autowired private ProductDao productDao; @Override public List<Product> findAllProduct() { return productDao.findAllProduct(); } }
package com.ssm.controller; import com.ssm.entity.Product; import com.ssm.service.ProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class ProductController { @Autowired private ProductService productService; //查詢所有的商品信息 @GetMapping("/product") public List<Product> findAllProduct(){ return productService.findAllProduct(); } }
package com.demo02.demo_02; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages = {"com.ssm.controller","com.ssm.service"}) @MapperScan(basePackages = {"com.ssm.dao"}) public class Demo02Application { public static void main(String[] args) { SpringApplication.run(Demo02Application.class, args); } }