Spring boot 整合SSM框架三層架構並前後臺restful風格互動
阿新 • • 發佈:2019-01-02
pom.xml檔案
<?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>
<groupId >com.example</groupId>
<artifactId>springboot_ssm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot_ssm</name>
<description>Demo project for Spring Boot</description>
<parent >
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties >
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!--整合mybatis所需的jar -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--熱啟動:每自修改後, 程式自動啟動spring Application上下文。 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- 阿里jeon -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.9</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
專案目錄
ProductController控制層程式碼
package com.example.controller;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.example.pojo.Product;
import com.example.service.ProductService;
/**
* 產品控制層
* @author pengfei.xiong
* @date 2017年11月16日
*/
@RestController //證明是controller層並且返回json
@EnableAutoConfiguration
@ComponentScan(basePackages={"com.example.service"})//新增的註解
public class ProductController {
//依賴注入
@Autowired
ProductService productService;
/**
* @RestController代表這個類是用Restful風格來訪問的,如果是普通的WEB頁面訪問跳轉時,我們通常會使用@Controller
value = "/users/{username}" 代表訪問的URL是"http://host:PORT/users/實際的使用者名稱"
method = RequestMethod.GET 代表這個HTTP請求必須是以GET方式訪問
consumes="application/json" 代表資料傳輸格式是json
@PathVariable將某個動態引數放到URL請求路徑中
@RequestParam指定了請求引數名稱
*/
@RequestMapping(value = "qp/{name}",method = RequestMethod.GET)
public List<Product> queryProduct(@PathVariable String name,HttpServletResponse httpServletResponse) {
System.out.println(name);
List<Product> p = productService.queryProductByName(name);
return p;
}
}
service介面
package com.example.service;
import java.util.List;
import com.example.pojo.Product;
/**
* 產品業務層介面
* @author pengfei.xiong
* @date 2017年11月16日
*/
public interface ProductService {
public List<Product> queryProductByName(String name);
}
service實現類
package com.example.service;
import java.util.List;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.dao.ProductMapper;
import com.example.pojo.Product;
/**
* 產品業務層實現類
* @author XSWL pengfei.xiong
* @date 2017年11月16日
*/
@Service
@MapperScan("com.example.dao") //與dao層的@Mapper二選一寫上即可(主要作用是掃包)
public class ProductServiceImpl implements ProductService {
//依賴注入
@Autowired
ProductMapper mapper;
@Override
public List<Product> queryProductByName(String name) {
List<Product> pro = mapper.selectProductByName(name);
return pro;
}
}
mapper介面
package com.example.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import com.example.pojo.Product;
/**
* 產品資料層介面
* @author pengfei.xiong
* @date 2017年11月16日
*/
@Mapper
@Repository
public interface ProductMapper {
/**
* 根據名稱查詢產品
* @param name 名稱
* @return 返回產品實體物件
*/
@Select(" SELECT * FROM product WHERE name = #{name}")
@ResultType(Product.class)
List<Product> selectProductByName(@Param("name") String name);
}
實體類
package com.example.pojo;
/**
* 產品實體類
* @author pengfei.xiong
* @date 2017年11月16日
*/
public class Product {
private int id;
private String name;
private int count;
private double price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Product() {
super();
}
public Product(int id, String name, int count, double price) {
super();
this.id = id;
this.name = name;
this.count = count;
this.price = price;
}
}
程式的入口
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.example.controller.ProductController;
/**
* 專案入口
* @author XSWL pengfei.xiong
* @date 2017年11月16日
*/
@SpringBootApplication
public class SpringbootSsmApplication {
//專案子入口
public static void main(String[] args) {
SpringApplication.run(ProductController.class, args);
}
}
最後還有一個配置檔案要配置
#mysql
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.database = mysql
#tomcat 埠
server.port=4560
#Mybatis掃描(配置xml模式使用) mybatis.mapper-locations=classpath*:mapper/*.xml
#起別名。可省略寫mybatis的xml中的resultType的全路徑
mybatis.type-aliases-package=com.example.pojo
這樣就已經可以訪問了,但是大家要自己根據實體類建資料庫表,或者替換實體類,還有配置檔案中資料的密碼和資料庫名稱
訪問路徑:http://localhost:4560/qp/name 這裡name是引數的值 這是rustful風格 測試話先可以查詢所有 不帶條件自己修改下程式碼
下面是前臺介面 我把它寫在static目錄下的 採用的ajax提交
前臺程式碼
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Spring boot</title>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
//查詢的內容
function quary(){
var str = $("#name").val();
alert(str)
$.ajax({
url:"qp/"+str,
type:"get",
success : function(data) {
alert(data);
//i迴圈的次數 value物件 id,name等是屬性 <接收list>
$.each(data, function(i, value) {
$("#remark").append(
" <tr><td>" + value.id + "</td><td>"
+ value.name + "</td><td>" + value.count
+ "</td><td>" + value.count + "</td></tr>");
});
},
error:function(){
alert("沒有查詢到該商品");
}
});
}
</script>
</head>
<body>
<form action="">
查詢:<input type="text" id="name" onchange="quary()"/>
<table class="table table-striped" id="remark">
<tr>
<td>編號</td>
<td>名稱</td>
<td>總數</td>
<td>價格</td>
</tr>
</table>
</form>
</body>
</html>
這樣就大功完成,當然還少了些什麼日誌等等 不會的可以留言