spring boot简单的小demo(适合于初学者)

import com.example.demo2.com.example.dao.ShopDao;
import com.example.demo2.com.example.entity.Shops;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
public class ShopController {

    @Autowired
    private ShopDao shopdao;

    /*
    * 查询
    * */
    @RequestMapping("sele")
    @ResponseBody
    public List test(){
        List<Shops> finl = shopdao.finalAll();
        for (Shops shop : finl){
            System.out.println(shop.getNames());
            System.out.println(shop.getPass());
        }
        return finl;
    }

    /*
    * 添加
    * */

    @RequestMapping("add")
    @ResponseBody
    public String test1(String names,String pass,String sex){
        Shops shop = new Shops();
        shop.setNames(names);
        shop.setPass(pass);
        shop.setSex(sex);
       Integer result = shopdao.add(shop);
       String st = "添加失败";
       if(result>0){
           st = "添加成功";
       }
       return st;
    }

    /*
     *  修改
     * */
    @RequestMapping("upda")
    @ResponseBody
    public String test2(Integer id,String names,String pass,String sex){
        Shops shop = new Shops();
        shop.setId(id);
        shop.setNames(names);
        shop.setPass(pass);
        shop.setSex(sex);
        Integer result = shopdao.updates(shop);
        String st = "修改失败";
        if(result>0){
            st = "修改成功";
        }
        return st;
    }

    /*
     * 删除
     * */
    @RequestMapping("dele")
    @ResponseBody
    public String test3(Integer id){
        Integer result = shopdao.deletes(id);
        String st = "删除失败";
        if(result>0){
            st = "删除成功";
        }
        return st;
    }

}

  

dao层

import com.example.demo2.com.example.entity.Shops;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper//声明是一个Mapper,与springbootApplication中的@MapperScan二选一写上即可
public interface ShopDao {
    //查询
    public List<Shops> finalAll();

    //添加
    public Integer add(Shops shop);

    //修改
    public Integer updates(Shops shop);

    //删除
    public Integer deletes(Integer id);

}

mapper

<?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.demo2.com.example.dao.ShopDao">

    <select id="finalAll" resultType="com.example.demo2.com.example.entity.Shops">
       select * from first
    </select>

    <insert id="add" parameterType="com.example.demo2.com.example.entity.Shops" useGeneratedKeys="true">
        INSERT INTO first(names,pass,sex) values(#{names},#{pass},#{sex})
    </insert>

    <update id="updates" parameterType="com.example.demo2.com.example.entity.Shops">
      update first set names =#{names},pass=#{pass},sex=#{sex} where id = #{id}
    </update>

    <delete id="deletes" parameterType="com.example.demo2.com.example.entity.Shops">
      delete from first where id =#{id}
    </delete>

</mapper>

application.yml

server:
  port: 8088
  context-path: /

spring:
    profiles:
        active: dev
    application:
        name: demo-2
    jackson:
        date-format: yyyy-MM-dd HH:mm:ss
        time-zone: GMT+8
        default-property-inclusion: non_null
    datasource:
        url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF8
        username: root
        password: root
        # 使用druid数据源
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        filters: stat
        maxActive: 20
        initialSize: 1
        maxWait: 60000
        minIdle: 1
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: select ‘x‘
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: true
        maxOpenPreparedStatements: 20

mybatis:
    typeAliasesPackage: com.example.demo2.com.example.dao
    mapperLocations: classpath:mapper/*.xml

项目启动文件

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@EnableTransactionManagement//开启事务管理
@MapperScan("com.example.demo2.com.example.dao")//与dao层的@Mapper二选一写上即可(主要作用是扫包)
public class Demo2Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo2Application.class, args);
    }
}

pom文件

<?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>demo-2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo-2</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.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>      <!-- 这个可以使用和spring boot的mybatis两个选一  -->
        <!--<mybatis.version>3.3.1</mybatis.version>-->
        <!--<mybatis.spring.version>1.2.4</mybatis.spring.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-aop</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>

        <!-- druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.11</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-jdbc</artifactId>
        </dependency>

        <!--Mybatis 二选一  -->
        <!--<dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>${mybatis.spring.version}</version>
        </dependency>-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>

        <!-- Mybatis Generator -->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.2</version>
            <scope>compile</scope>
            <optional>true</optional>
        </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>
            </plugin>
        </plugins>
    </build>

</project>
时间: 2024-10-29 06:27:17

spring boot简单的小demo(适合于初学者)的相关文章

RabbitMq 集成 spring boot 消息队列 入门Demo

spring boot 集成 RabbitMq还是很方便的.现在来一个简单的例子来集成rabbitmq.入门demo. 主要概念: 其中比较重要的概念有 4 个,分别为:虚拟主机,交换机,队列,和绑定. 虚拟主机:一个虚拟主机持有一组交换机.队列和绑定.为什么需要多个虚拟主机呢?很简单,RabbitMQ当中,用户只能在虚拟主机的粒度进行权限控制. 因此,如果需要禁止A组访问B组的交换机/队列/绑定,必须为A和B分别创建一个虚拟主机.每一个RabbitMQ服务器都有一个默认的虚拟主机"/"

基于Angular和Spring WebFlux做个小Demo

前言 随着Spring Boot2.0正式发布,Spring WebFlux正式来到了Spring Boot大家族里面.由于Spring WebFlux可以通过更少的线程去实现更高的并发和使用更少的硬件资源去实现扩展,我对此很感兴趣.同时Angular6也发布了,也想试试自己Angular的功底,便基于Angular和Spring WebFlux做个一个简单礼品管理系统的demo.至于angular和spring boot的集成可以参考该博客Angular和Spring Boot一起做个项目 响

spring AOP 环绕增强小Demo

前面写了一个前置增强,后置增强的小demo,前置增强即在方法调用前对方法增强:后置增强即在方法调用后对方法增强.环绕增强允许在目标类方法调用前后织入横切逻辑,它综合了前置.后置增强两者的功能. 还继续沿用之前的代码,这里介绍环绕增强的实现类和测试类. 环绕增强类 GreetingAroundAdvice.java package com.paic.zhangqi.spring.aop; import org.aopalliance.intercept.MethodInterceptor; imp

Spring Boot 简单的打包部署

Spring Boot 支持使用 Jar 内嵌 Web 服务器(Tomcat)的方式发布,也支持生成 war 包放在外置的 web 服务器运行. 1.使用 Jar 发布应用 配置步骤: ① pom.xml 要显示加入插件 org.springframework.boot,否则无法产生 jar 清单文件,导致打出来的 jar 无法使用命令运行. 1 <build> 2 <plugins> 3 <plugin> 4 <groupId>org.springfram

Spring WebSocket简单入门测试Demo(网页及时聊天)

说明 本demo运行的环境是:ssm框架 + tomcat8 + jdk7+,由于只是demo.很简陋.能运行调式通过就行,再在此基础上进行扩展研究. 在网上看了很多列子.对于没有接触过websocket的人来说.真的是看不懂.本文列子应该算是比较简单的. Spring WebSocket API的核心接口是WebSocketHandler.我把它叫做消息处理中心. 其他的详细解说可以参考别人的博客 <servlet-mapping> <servlet-name>springmvc

Spring Boot 简单的请求示例(包括请求体验证)

1.先做个最简单的Get请求 新建一个Controller , 并给他添加注解@RestController 它是@Controller和@ResponseBody的组合注解,告诉Spring我是一个Controller,请求过来的时候,记得扫描我 ok,在给他添加@RequestMapping注解,为这个Controler指明路径 @RestController @RequestMapping("/Demo") public class DemoController { @Respo

gulp安装+一个超简单入门小demo

gulp安装參考.gulp安装參考2. 一.NPM npm是node.js的包管理工具.主要功能是管理.更新.搜索.公布node的包. Gulp是通过npm安装的. 所以首先,须要安装node.js.而且升级npm到最新版本号. 安装nodeJS 基本參考传送门戳这里:安装nodeJs.安装nodeJs2.nodeJs下express的安装到执行 1.    从nodejs.org下载最新的nodejs安装包.并安装. 2.    进入nodejs的安装路径下: 天真的以为这样就能够了. wat

FastDFS简单入门小demo

图片上传 需要引入 FastDFS 相关的jar包,但是这个jar没有在中央仓库,所以还得需要找到这个jar手动安装到自己的本地仓库才能使用. 需要一个配置文件   fdfs_client.conf 需要一个 FastDFS 服务器 看代码: fdfs_client.conf # connect timeout in seconds # default value is 30s connect_timeout=30 # network timeout in seconds # default v

Spring Boot简单xml配置集成mybatis

一.xml配置版 1.properties文件中增加的配置: mybatis.config-locations=classpath:mybatis/mybatis-config.xml mybatis.mapper-locations=classpath:mybatis/mapper/*.xml 2.mybatis-config.xml文件的配置 mapper文件里面的jdbcType重命名 <configuration> <typeAliases> <typeAlias a