Springboot从易到难(基本页面显示到框架整合、数据交互)
来源:互联网 发布:网络cry是什么意思啊 编辑:程序博客网 时间:2024/05/20 05:53
1、创建一个基于Springboot的项目(简单页面显示不同数据)
项目结构:
pon.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>ssmspringboot_test</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>ssmspringboot_test</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</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 用于注解 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
SsmspringbootTestApplication项目入口:
package com.example.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * 项目入口(直接启动项目即可) * @author xswl_ym */@SpringBootApplicationpublic class SsmspringbootTestApplication { public static void main(String[] args) { SpringApplication.run(SsmspringbootTestApplication.class, args); }}
Student实体类:
package com.example.entity;import java.io.Serializable;/** * 测试实体类 * @author xswl_ym * */public class Student implements Serializable{ private int id; private String name; private int age; public Student() { super(); } public Student(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } 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 getAge() { return age; } public void setAge(int age) { this.age = age; }}
HelloWord.java测试层:
package com.example.controller;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.example.entity.Student;/** 1. 测试控制层 2. @author xswl_ym */@RestController //相当于@Controller和@RequestBody 返回json@EnableAutoConfigurationpublic class HelloWord { @RequestMapping("/hello") public String showHello(){ return "HelloWord!"; } @RequestMapping("/entity") public Student showStudent(){ Student student = new Student(1, "showtime", 22); return student; } @RequestMapping("/map") public Map<String, Object> showMap(){ Map<String, Object> map = new HashMap<String,Object>(); map.put("username", "张无忌"); map.put("age", 15); return map; } @RequestMapping("/list") public List<Student> showList(){ List<Student> list = new ArrayList<Student>(); Student student01 = new Student(1, "showtime", 22); Student student02 = new Student(2, "showtime", 33); list.add(student01); list.add(student02); return list; }}
项目启动:在”SsmspringbootTestApplication”中右键”Run As”,选择”SpringBoot App”即可。
访问路径:http://localhost:8080//map或者http://localhost:8080//list;
2、基于上面的项目对springmvc+mybatis进行整合,实现数据交互
项目结构:
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>
SsmspringbootTestApplication项目入口:
package com.example.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import com.example.controller.ProductController;/** * 项目入口(直接启动项目即可) * @author xswl_ym */@SpringBootApplicationpublic class SsmspringbootTestApplication { public static void main(String[] args) { SpringApplication.run(ProductController.class, args); }}
Product产品实体类 :
package com.example.entity;/** * 产品实体类 * @author xswl_ym */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; }}
ProductController产品控制层:
package com.example.controller;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.entity.Product;import com.example.service.ProductService;/** * 产品控制层 * @author xswl_ym */@RestController //证明是controller层并且返回json@EnableAutoConfiguration@ComponentScan(basePackages={"com.example.service"})//添加的注解public class ProductController { //依赖注入 @Autowired ProductService productService; /** * @RestController代表这个类是用Restful风格来访问的,如果是普通的WEB页面访问跳转时, * 我们通常会使用@Controllervalue = "/users/{username}" 代表访问的URL是"http://host:PORT/users/实际的用户名" * method = RequestMethod.GET 代表这个HTTP请求必须是以GET方式访问 * @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); String json=""; try { json= JSON.toJSONString(p); //httpServletResponse.getWriter().println(json); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return p; }}
ProductService业务层接口:
package com.example.service;import java.util.List;import com.example.entity.Product;/** * 产品业务层接口 * @author xswl_ym */public interface ProductService { public List<Product> queryProductByName(String name);}
ProductServiceImpl业务层实现类:
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.entity.Product;/** * 产品业务层实现类 * @author xswl_ym */@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; }}
ProductMapper数据层接口:
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.entity.Product;/** * 产品数据层接口 * @author xswl_ym */@Mapper@Repositorypublic interface ProductMapper { /** * 根据名称查询产品 * @param name 名称 * @return 返回产品实体对象 */ //@Select(" SELECT * FROM product WHERE name = #{name}") @ResultType(Product.class) List<Product> selectProductByName(@Param("name") String name);}
ProductMapper.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.example.dao.ProductMapper" > <select id="selectProductByName" resultType="com.example.entity.Product"> SELECT * FROM product WHERE name = #{name} </select></mapper>
application.properties文件配置:
#mysqlspring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.username=rootspring.datasource.password=123456spring.datasource.driverClassName=com.mysql.jdbc.Driverspring.jpa.database = mysql#tomcat server.port=8080#Mybatis扫描(配置xml模式使用,决定是否使用映射文件)mybatis.mapper-locations=classpath*:mapper/*.xml#起别名。可省略写mybatis的xml中的resultType的全路径mybatis.type-aliases-package=com.example.pojo
注意:数据层有两种处理方法:分别是使用映射文件和注解开发;
映射文件:在application.properties中配置扫描”mybatis.mapper-locations=classpath*:mapper/*.xml”;
注解开发:将application.properties的扫描删掉,直接在数据层接口中用注解写,如:@Select、@Insert;
restful.html前台页面:
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title><script type="text/javascript" src="js/jquery.min.js"></script><script type="text/javascript">//查询的内容function quary(){ var str = $("#name").val(); $.ajax({ url:"qp/"+str, type:"get", success : function(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=""><!-- 在text文本中加入onchange="quary()"属性为按tab后查找,而onkeydown="EnterPress()"为回车键事件 --> 查询:<input type="text" id="name"/><input type="button" value="查询" onclick="quary();"/> <table class="table table-striped" id="remark"> <tr> <td>编号</td> <td>名称</td> <td>总数</td> <td>价格</td> </tr> </table> </form></body></html>
数据库生成代码:
-- 创建数据库create database `test`;use `test`;-- 建表CREATE TABLE `product` (`Id` int NOT NULL PRIMARY key auto_increment,`name` varchar(20) NULL ,`count` int(11) NULL ,`price` double(10,0) NULL);INSERT into product(name,count,price) values('张三',20,11);INSERT into product(name,count,price) values('李四',15,22);INSERT into product(name,count,price) values('王五',16,33);INSERT into product(name,count,price) values('麻子',17,44);INSERT into product(name,count,price) values('遛鸟',18,55);
项目启动:在”SsmspringbootTestApplication”中右键”Run As”,选择”SpringBoot App”即可。
访问路径:”http://localhost:8080/qp/张三(name属性)”直接访问数据库数据
或”http://localhost:8080/restful.html“在页面中查询。
3、如何从Springboot跳转到jsp页面:
在pom.xml文件中继续设置依赖项:
<!-- springboot开发jsp需要的jar包 S --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <!-- springboot开发jsp需要的jar包 E -->
application.properties文件中配置:
# 配置jsp文件的位置,默认位置为:src/main/webappspring.mvc.view.prefix: /WEB-INF/jsp/spring.mvc.view.suffix: .jsp
Controller控制层文件中配置:
@RequestMapping("/mvjsp") public ModelAndView mvjsp() { ModelAndView mv=new ModelAndView("/show1"); return mv; }
最后启动项目并访问路径:”https://localhost:8080/mvjsp“;
注意:如果出现jar报路径错误直接打开对应路径,删除ecj文件夹,然后重新加载pom.xml。
(可能要加载多次)
友情链接:
http://blog.csdn.net/xpf_user/article/details/78557782;
- Springboot从易到难(基本页面显示到框架整合、数据交互)
- mvc-ajax(从后台取数据显示到页面上)
- 使用echart从后台获取数据动态显示到页面
- 如何从数据库调出数据显示到页面PHP+Mysql+Html(简单实例)
- 如何从数据库调出数据显示到页面 PHP+Mysql+Html(简单实例)
- 从数据库调出数据显示到页面 PHP+Mysql+Html(简单实例)
- springboot+security框架整合
- SpringBoot整合shiro框架
- 打开页面从数据库读出数据显示到页面上的解决方法
- Springboot 页面显示
- 利用springboot,mybatis和luncene整合创建博客系统(只给出全数据库搜索并显示在搜索页面)
- struts2框架从数据库批量取得数据集并在前台页面循环显示
- struts2框架从数据库批量取得数据集并在前台页面循环显示
- 从url到页面的显示
- SSH框架/JSP Servlet 从Action中传递数据到JSP页面
- Ajax的一个简单例子--从一个文件文件中读取数据显示到页面中
- 如何从数据库调出数据显示到页面 PHP+Mysql+Html
- 通过jquery ajax在从服务器获取一个文件的数据,显示到客户端的页面
- C/C++对URL分割进行实现
- elasticsearch-jdbc同步myslq数据到elasticsearch
- python: hasattr()、setattr()、getattr()、delattr() 内建函数
- Linux常用的命令
- 手机访问PC网站自动跳转到手机网站代码
- Springboot从易到难(基本页面显示到框架整合、数据交互)
- spring mvc rocketMq简单配置
- 怎样根据使用环境需求来选择不同参数的光模块
- 国庆假期微信大数据报告
- 用户端接收解析JSON数据
- JavaWeb学习之Servlet(三)----Servlet的映射匹配问题、线程安全问题
- springAop实现日志请求参数和返回结果打印
- springmvc实现简单CRUD
- 查找法的疑惑