Spring Boot

一。springboot简介

   Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

二。springboot容器

  spring容器提供了bean的创建和管理  springboot沿用spring容器  提供了简化的操作方式 全程使用注解  以下通过实例讲解bean的管理 构建项目使用maven


spring boot 不需要向spring那样写很多的配置




案例 使用spring Boot连接数据库进行CURD

创建一个maven项目

在pom.xml中添加依赖

<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>cn.et</groupId>
  <artifactId>SbLesson</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <!-- 集成spring的父项目 统一进行版本管理   从Spring Boot继承默认值 -->
  <parent>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-parent</artifactId>
	    <version>1.5.9.RELEASE</version>
  </parent>
  
  <dependencies>
  	<!-- 
  		springboot每一个框架的集成都是一个starter
  		spring-boot-starter-web加载javaee 内嵌tomcat
  	 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
    
    <!-- 连接数据库 -->
    <dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-data-jpa</artifactId>
	</dependency>
    
    
    <!-- 如果使用jsp  需要条件的jar包 -->
    <dependency>
		<groupId>org.apache.tomcat.embed</groupId>
		<artifactId>tomcat-embed-jasper</artifactId>
	</dependency>
    
    
    <!-- 自动启动  每次修改代码时会自动启动 -->
    <dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-devtools</artifactId>
		<optional>true</optional>
	</dependency>
    
    
    <!-- mysql数据库的jar包 -->
    <dependency>
	    <groupId>mysql</groupId>
	    <artifactId>mysql-connector-java</artifactId>
	    <version>5.1.44</version>
	</dependency>
    
	
	<!-- 查看程序是否健康的jar包 -->
	<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
  </dependencies>
</project>

创建一个测试类  默认springboot只会扫描启动类相同包或者子包中带有注解的bean

package cn.et.lesson01;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication//自动扫描和装配
public class AA {   
       public static void main(String[] args) {
           SpringApplication.run(AA.class, args);
    }
}


springboot默认读取src/main/resources下的application.properties或者application.yml文件 该文件是springboot的配置文件

 比如配置端口  server.port=80 可以修改tomcat端口 其他配置参考springboot官方文档

(https://docs.spring.io/spring-boot/docs/1.5.7.RELEASE/reference/htmlsingle/#common-application-properties)


application.properties    连接数据库的四要素

spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.jpa.show-sql=true
server.port=80
server.servlet-path=/sb
spring.devtools.restart.enabled=true
debug=true

定义实体类  与数据库的列明一致

package cn.et.lesson01.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Emp2 {
   @Id//主键
   @GeneratedValue(strategy=GenerationType.AUTO)//自动增长
   private int id;
   @Column
   private String ename;
   @Column
   private double sal;
  @Column
   private int deptid;
	public int getId() {
	    return id;
	}
	public void setId(int id) {
	    this.id = id;
	}
	public String getEname() {
	    return ename;
	}
	public void setEname(String ename) {
	    this.ename = ename;
	}
	public double getSal() {
	    return sal;
	}
	public void setSal(double sal) {
	    this.sal = sal;
	}
	public int getDeptid() {
	    return deptid;
	}
	public void setDeptid(int deptid) {
	    this.deptid = deptid;
	}
   
    
}

dao层

package cn.et.lesson01.dao;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import cn.et.lesson01.entity.Emp2;

//定义一个接口  继承CrudRepository<Emp2, Integer>  内部有curd方法
/**
 * 
 * Emp2 对象
 * Integer  主键类型
 * @author Administrator
 *
 */

@Repository
public interface EmpRepository extends CrudRepository<Emp2, Integer> {
    
}

service层====接口

package cn.et.lesson01.service;

import cn.et.lesson01.entity.Emp2;;

public interface Emp2Service {
    public void save(Emp2 e);

    public void delete(Emp2 e );
    
    public Iterable queryall();
    
    public Emp2 queryEmp(Integer id);
    
    public void updateEmp(Emp2 e);
}

service层====实现类

package cn.et.lesson01.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.et.lesson01.dao.EmpRepository;
import cn.et.lesson01.entity.Emp2;
import cn.et.lesson01.service.Emp2Service;

@Service
public class Emp2ServiceImpl implements Emp2Service {

    @Autowired
    EmpRepository emp;
    
    public void save(Emp2 e) {
        emp.save(e);
    }

    @Override
    public void delete(Emp2 e) {
        emp.delete(e);
    }

    @Override
    public Iterable queryall() {
    	//返回的是一个迭代器
         Iterable<Emp2> findAll = emp.findAll();
        return findAll;
    }

    @Override
    public Emp2 queryEmp(Integer id) {
        return emp.findOne(id);
    }

    @Override
    public void updateEmp(Emp2 e) {
        emp.save(e);
    }
    
}


controller层

package cn.et.lesson01.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.et.lesson01.entity.Emp2;
import cn.et.lesson01.service.Emp2Service;



/*
 	@Target(ElementType.TYPE)
	@Retention(RetentionPolicy.RUNTIME)
	@Documented
	@Controller
	@ResponseBody
	
	@RestController继承自以上的注解  以上注解有的功能@RestController都有
 */
@RestController
public class Emp2Controller {
    //通过装配jdbc 查询
	@Autowired
    JdbcTemplate jdbc;
    @RequestMapping("/food/{foodid}")
    public List<Map<String, Object>> getFood(@PathVariable String foodid){
        List<Map<String, Object>> queryForList = jdbc.queryForList("select * from food where foodid="+foodid);
        return queryForList;
    }
    
    
    //@RestController继承@Controller @ResponseBody
    @RequestMapping("/hello")//访问该url返回一个json
    public Map hello(){
        Map map=new HashMap();
        map.put("id", 1);
        map.put("name", "zs");
        return map;
    }
    
    
    @Autowired
    Emp2Service e;
    //保存
    @RequestMapping("/saveemp")
    public String saveEmp(){
        Emp2 emp=new Emp2();
        emp.setEname("hhh");
        emp.setSal(12);
        emp.setDeptid(4);
        e.save(emp);
        return "1";
    }
    
   
    @RequestMapping("/queryAllEmp")
    public Iterable<Emp2> queryAllEmp(){
       return e.queryall();
       
    }
    
    //根据id查询
    @RequestMapping("/queryEmp/{id}")
    public Emp2 queryEmp(@PathVariable Integer id){
        return e.queryEmp(id);
    }
    
    //删除
    @RequestMapping("/deleteEmp")
    public String deleteEmp(){
        Emp2 emp=new Emp2();
        emp.setId(23);
        e.delete(emp);
        return "1";
        
    }
    
    
    //修改
    @RequestMapping("/updateEmp")
    public String updateEmp(){
        Emp2 emp=new Emp2();
        emp.setId(22);
        emp.setEname("AAAA");
        e.updateEmp(emp);
        return "1";
    }
}


这里没有界面  在浏览器直接访问controller层

例如:localhost:80/sb/updateEmp =======>修改






根据上图实现一个servlet

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;


import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


import com.alibaba.druid.support.http.StatViewServlet;


@Configuration
public class ConfigBean {
	
	/**
	 * 相当于<bean id="druidStatView" class="org.springframework.boot.web.servlet.ServletRegistrationBean"></bean>
	 * @return
	 */
	@Bean
	public ServletRegistrationBean druidStatView(){
		ServletRegistrationBean srb = new ServletRegistrationBean();
		
		srb.setName("DruidStatView");
		StatViewServlet svs=new StatViewServlet();
		srb.setServlet(svs);
		
		LinkedHashMap<String, String> initParameters = new LinkedHashMap<String, String>();
		initParameters.put("loginUsername", "admin");
		initParameters.put("loginPassword", "123456");
		srb.setInitParameters(initParameters);
		
		String url="/druid/*";
		List<String> urls=new ArrayList<String>();
		urls.add(url);
		srb.setUrlMappings(urls);
		
		return srb;
	}
	
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值