springboot的搭建

springboot简介

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

Spring boot的优点

spring boot 可以支持你快速的开发出 restful 风格的微服务架构
自动化确实方便,做微服务再合适不过了,单一jar包部署和管理都非常方便。只要系统架构设计合理,大型项目也能用,加上nginx负载均衡,轻松实现横向扩展
spring boot 要解决的问题, 精简配置是一方面, 另外一方面是如何方便的让spring生态圈和其他工具链整合(比如redis, email, elasticsearch)	  

springboot的核心功能

1.独立运行的Spring项目
Spring Boot可以以jar包的形式来运行,运行一个Spring Boot项目我们只需要通过java -jar xx.jar类运行。非常方便。
2.内嵌Servlet容器
Spring Boot可以内嵌Tomcat,这样我们无需以war包的形式部署项目。
3.提供starter简化Maven配置
使用Spring或者SpringMVC我们需要添加大量的依赖,而这些依赖很多都是固定的,这里Spring Boot 通过starter能够帮助我们简化Maven配置。
4.自动配置Spring 
5.准生产的应用监控 
6.无代码生成和xml配置

搭建环境

file–>new project

next—>finish

使用mavern导入依赖

pom.xml

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>log4j-over-slf4j</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
	<--oracle的依赖,导入本地的jar(直接在resources下建立一个lib目录)-->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.1.0.6.0</version>
            <scope>system</scope>
            <systemPath>${basedir}/src/main/resources/lib/ojdbc6-11.1.0.6.0.jar</systemPath>
        </dependency>
		<--mysql的依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
	<--json的依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>

配置连接数据库的配置文件

application.properties

//连接mysql
#spring.datasource.platform=mysql
#spring.datasource.url=jdbc:mysql://localhost/mydb
#spring.datasource.username=root
#spring.datasource.password=123456
#spring.datasource.driverClassName=com.mysql.jdbc.Driver
//连接oracle
spring.datasource.platform=oracle
spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521/orcl
spring.datasource.username=jtf
spring.datasource.password=123456

server.port=8080
server.session-timeout=30
server.tomcat.uri-encoding=UTF-8

#####springboot 整合 mybatis
mybatis.mapper-locations= classpath:/com/teng/springboot02/mapper/*Mapper.xml
#####定义别名
mybatis.type-aliases-package=com.teng.springboot02.domain

项目的结构

实体类

Teacher

package com.teng.springboot02.domain;

import java.util.List;

public class Teacher {

    private Integer id;
    private String name;
    private String className;
    private List<Stu> stus;

    public List<Stu> getStus() {
        return stus;
    }

    public void setStus(List<Stu> stus) {
        this.stus = stus;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }
}

Stu

package com.teng.springboot02.domain;

public class Stu {

    private Integer id;
    private String name;
    private Integer teacherId;
    private String className;
    private Teacher teacher;

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getTeacherId() {
        return teacherId;
    }

    public void setTeacherId(Integer teacherId) {
        this.teacherId = teacherId;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }
}

mapper接口

TeacherMapper

package com.teng.springboot02.mapper;

import com.teng.springboot02.domain.Teacher;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;

import java.util.List;

@Mapper
@Component
public interface TeacherMapper {
    public List<Teacher> selectAllTeacher();
    public List<Teacher> findAllTeacher();
}

Service层

TeacherService

package com.teng.springboot02.service;


import com.teng.springboot02.domain.Teacher;

import java.util.List;

public interface TeacherService {
    public List<Teacher> selectAllTeacher();
    public List<Teacher> findAllTeacher();
}

impl TeacherServiceImpl

package com.teng.springboot02.service.impl;

import com.teng.springboot02.domain.Teacher;
import com.teng.springboot02.mapper.TeacherMapper;
import com.teng.springboot02.service.TeacherService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Resource
    private TeacherMapper teacherMapper;

    public List<Teacher> selectAllTeacher() {
        return null;
    }

    public List<Teacher> findAllTeacher() {
        return teacherMapper.findAllTeacher();
    }

    public void setTeacherMapper(TeacherMapper teacherMapper) {
        this.teacherMapper = teacherMapper;
    }
}

resources

com/teng/spring02/mapper TeacherMapper.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">
<!--namespace 命名空间 唯一的-->
<mapper namespace="com.teng.springboot02.mapper.TeacherMapper">
    <!--一对多的第一种写法,一般考虑到性能,不会这样实现-->
    <resultMap id="TeacherMap" type="Teacher" autoMapping="true">
    <collection property="stus" ofType="Stu" column="id" autoMapping="true">
    </collection>
    </resultMap>
    <!--查询所有的老师及各自的所有学生,第一种形式 一一对应-->
    <select id="selectAllTeacher" parameterType="Teacher" resultMap="TeacherMap">
        select t.id,t.name,t.class_name,
        s.id as sid,s.name as sname,s.class_name as className
        from
        teacher t join stu s
        on t.id = s.teacher_id
    </select>
    <resultMap type="Teacher" id="teacherMaps" autoMapping="true">
        <collection property="stus" ofType="Stu" select="getStudents" column="id">
        </collection>
    </resultMap>
    <!-- 查询所有的老师级各自的所有学生,一对多关联-->
    <select id="findAllTeacher" parameterType="Teacher" resultMap="teacherMaps">
		SELECT
			t.id,
			t.name,
			t.class_name
		FROM
			teacher t
	</select>
    <select id="getStudents" parameterType="int" resultType="Stu">
		select
			s.id,
			s.name,
			s.class_name as className
		from stu s
		where teacher_id = #{id}
	</select>
</mapper>

controller

TeacherController

package com.teng.springboot02.controller;

import com.alibaba.fastjson.JSON;
import com.teng.springboot02.service.TeacherService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class TeacherController {

    @Resource
    private TeacherService teacherService;

    @RequestMapping("/showTeacher")
    public String showTeacher(){
        List list = teacherService.findAllTeacher();
        return JSON.toJSONString(list);
    }

    public void setTeacherService(TeacherService teacherService) {
        this.teacherService = teacherService;
    }
}

测试结果

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券