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>
以上是ssm的简单流程
以下是jpa的简单流程
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.autoconfigure.domain.EntityScan;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
- @ComponentScan(basePackages = {"com.example.demo3.action"})
- @EnableJpaRepositories(basePackages = "com.example.demo3.dao")
- @EntityScan("com.example.demo3.entity")
- @EnableAutoConfiguration
- @SpringBootApplication
- public class Demo3Application {
- public static void main(String[] args) {
- SpringApplication.run(Demo3Application.class, args);
- }
- }
- import com.example.demo3.dao.FirstDao;
- import com.example.demo3.entity.First;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
- import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- public class Action {
- @Autowired
- private FirstDao firstDao;
- @RequestMapping("hello")
- @ResponseBody
- public First test1(Integer id){
- First first = firstDao.findOne(id);
- return first;
- }
- }
- import javax.persistence.*;
- @Entity
- @Table(name="first")
- public class First {
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "id")
- private Integer id;
- @Column(name="names")
- private String names;
- @Column(name="pass")
- private String pass;
- @Column(name="sex")
- private String sex;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getNames() {
- return names;
- }
- public void setNames(String names) {
- this.names = names;
- }
- public String getPass() {
- return pass;
- }
- public void setPass(String pass) {
- this.pass = pass;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- }
- import com.example.demo3.entity.First;
- import org.springframework.data.jpa.repository.JpaRepository;
- import java.util.List;
- public interface FirstDao extends JpaRepository<First,Integer>{
- }
- spring:
- datasource:
- url: jdbc:mysql:/localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
- username: root
- password: root
- driver-class-name: com.mysql.jdbc.Driver
- type: com.alibaba.druid.pool.DruidDataSource
- 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
- jpa:
- show-sql: true
- hibernate:
- ddl-auto: create
- server:
- port : 8088
- context-path : /
- <?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-3</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>demo-3</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>
- </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-data-jpa</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-aop</artifactId>
- <version>1.5.4.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-tomcat</artifactId>
- </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>
- <!-- 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>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
以上就是全部的demo,中间出现了一些问题,也都慢慢的解决了,有了解的朋友可以加我微信
如可以解决的问题,私信发我.
微信
spring boot简单的小demo(适合于初学者)的更多相关文章
- 玩转spring boot——简单登录认证
前言 在一个web项目中,某些页面是可以匿名访问的,但有些页面则不能.spring mvc提供了HandlerInterceptor接口来应对,只需要重写preHandle方法便可以实现此功能.那么使 ...
- Spring Boot 简单小Demo 转载!!!
Spring Boot简介 接下来我们所有的Spring代码实例将会基于Spring Boot,因此我们先来了解一下Spring Boot这个大杀器. Spring早期使用XML配置的方式来配置Spr ...
- 基于Angular和Spring WebFlux做个小Demo
前言 随着Spring Boot2.0正式发布,Spring WebFlux正式来到了Spring Boot大家族里面.由于Spring WebFlux可以通过更少的线程去实现更高的并发和使用更少的硬 ...
- windows下elasticsearch配置及spring boot 简单demod的 整合
学习过程: elasticsearch 下载安装 elasticsearch-head 安装 spring boot 下elasticsearch的配置 使用ElasticsearchReposito ...
- RabbitMQ(三):RabbitMQ与Spring Boot简单整合
RabbitMQ是目前非常热门的一款消息中间件,不管是互联网大厂还是中小企业都在大量使用.Spring Boot的兴起,极大地简化了Spring的开发,本文将使用Spring Boot与RabbitM ...
- spring Boot 简单的登录功能,利用了jdbcTemplate.class完成sql语句的执行,无需service层、dao层和.xml文件
1.搭建SpringBoot项目首先我们先在IDEA上创建一个SpringBoot的Web项目(1)file ——> new ——> project——> Spring Initia ...
- Spring Boot简单操作
目录 一.自定义异常页面 二.单元测试 三.多环境选择 四.读取主配置文件中的属性 五.读取List属性 一.自定义异常页面 对于404.405.500等异常状态,服务器会给出默认的异常页面,而这些 ...
- Spring Boot整合Dubbo框架demo
Dubbo框架原理见之前的博文:http://www.cnblogs.com/umgsai/p/5836925.html 首先启动zookeeper Server端 Pom配置如下 <?xml ...
- Spring Boot简单应用——会员管理系统
简介 本项目是使用Spring Boot编写的一个简单的会员管理系统. 提供了会员的解决方案,主要有会员模块,管理员模块,礼品模块,商品模块,会员等级模块,生日提醒模块,积分模块,详细模块如下图 准备 ...
随机推荐
- Design Mobile实现国际化
参考:https://mobile.ant.design/components/locale-provider-cn/
- Selenium自动化-调用Mysql数据库
上几篇博客发布了几篇Selenium入门知识和进阶, 现在附上如何 从数据库中取值 能够逐行取值,并且返回二维数组 import java.io.FileInputStream; import jav ...
- Vsphere 回收未消使用的磁盘空间
下载sdelete.exe 执行 sdelete.exe -z E: ,然后又恢复为原可用空间 关机 SHH进入物理主机,找到对应的虚机文件 执行vmkfstools -K test-Win200 ...
- Android Activity生命周期图解
Android activity的生命周期这一张图就够了. 验证结果: 值得注意的是从activity A--->activity B是先执行A的onPause然后走B的生命周期最后才走A的on ...
- JavaScript中闭包的写法和作用详解
1.什么是闭包 闭包是有权访问另一个函数作用域的变量的函数. 简单的说,Javascript允许使用内部函数---即函数定义和函数表达式位于另一个函数的函数体内.而且,这些内部函数可以访问它们所在的外 ...
- SQL Server非域(跨域)环境下镜像(Mirror)的搭建步骤及注意事项
在实际的生产环境下,我们经常需要跨域进行数据备份,而创建Mirror是其中一个方案.但跨域创建Mirror要相对复杂的多,需要借助证书进行搭建. 下面我们将具体的步骤总结如下: 第一部分 创建证书 S ...
- logback日志配置
第一步:加入jar包.要加入slf4j和logback的jar包,slf4j需要的jar包为slf4j-api,logback需要2个jar包(logback-classic.logback-core ...
- Redis持久化的方式
Redis小知识: redis是键值对的数据库,有5中主要数据类型: 字符串类型(string),散列类型(hash),列表类型(list),集合类型(set),有序集合类型(zset) Redis持 ...
- Linux内存描述之内存节点node--Linux内存管理(二)
1 内存节点node 1.1 为什么要用node来描述内存 这点前面是说的很明白了, NUMA结构下, 每个处理器CPU与一个本地内存直接相连, 而不同处理器之前则通过总线进行进一步的连接, 因此相对 ...
- 【PAT】B1015 德才论
这道题算是挺简单,我在群里看到的别人琢磨好久过不去,大多是因为没有考虑好四种等级的判断条件 一定要保证四种判断条件正确. 下面这是我的代码,比较笨.后边有别人那学来的聪明些的方法 #include&l ...