1. import com.example.demo2.com.example.dao.ShopDao;
  2. import com.example.demo2.com.example.entity.Shops;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6. import org.springframework.web.bind.annotation.RestController;
  7.  
  8. import javax.annotation.Resource;
  9. import java.util.List;
  10.  
  11. @RestController
  12. public class ShopController {
  13.  
  14. @Autowired
  15. private ShopDao shopdao;
  16.  
  17. /*
  18. * 查询
  19. * */
  20. @RequestMapping("sele")
  21. @ResponseBody
  22. public List test(){
  23. List<Shops> finl = shopdao.finalAll();
  24. for (Shops shop : finl){
  25. System.out.println(shop.getNames());
  26. System.out.println(shop.getPass());
  27. }
  28. return finl;
  29. }
  30.  
  31. /*
  32. * 添加
  33. * */
  34.  
  35. @RequestMapping("add")
  36. @ResponseBody
  37. public String test1(String names,String pass,String sex){
  38. Shops shop = new Shops();
  39. shop.setNames(names);
  40. shop.setPass(pass);
  41. shop.setSex(sex);
  42. Integer result = shopdao.add(shop);
  43. String st = "添加失败";
  44. if(result>0){
  45. st = "添加成功";
  46. }
  47. return st;
  48. }
  49.  
  50. /*
  51. * 修改
  52. * */
  53. @RequestMapping("upda")
  54. @ResponseBody
  55. public String test2(Integer id,String names,String pass,String sex){
  56. Shops shop = new Shops();
  57. shop.setId(id);
  58. shop.setNames(names);
  59. shop.setPass(pass);
  60. shop.setSex(sex);
  61. Integer result = shopdao.updates(shop);
  62. String st = "修改失败";
  63. if(result>0){
  64. st = "修改成功";
  65. }
  66. return st;
  67. }
  68.  
  69. /*
  70. * 删除
  71. * */
  72. @RequestMapping("dele")
  73. @ResponseBody
  74. public String test3(Integer id){
  75. Integer result = shopdao.deletes(id);
  76. String st = "删除失败";
  77. if(result>0){
  78. st = "删除成功";
  79. }
  80. return st;
  81. }
  82.  
  83. }

  

dao层

  1. import com.example.demo2.com.example.entity.Shops;
  2. import org.apache.ibatis.annotations.Mapper;
  3. import org.springframework.stereotype.Repository;
  4.  
  5. import java.util.List;
  6.  
  7. @Mapper//声明是一个Mapper,与springbootApplication中的@MapperScan二选一写上即可
  8. public interface ShopDao {
  9. //查询
  10. public List<Shops> finalAll();
  11.  
  12. //添加
  13. public Integer add(Shops shop);
  14.  
  15. //修改
  16. public Integer updates(Shops shop);
  17.  
  18. //删除
  19. public Integer deletes(Integer id);
  20.  
  21. }

mapper

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3.  
  4. <mapper namespace="com.example.demo2.com.example.dao.ShopDao">
  5.  
  6. <select id="finalAll" resultType="com.example.demo2.com.example.entity.Shops">
  7. select * from first
  8. </select>
  9.  
  10. <insert id="add" parameterType="com.example.demo2.com.example.entity.Shops" useGeneratedKeys="true">
  11. INSERT INTO first(names,pass,sex) values(#{names},#{pass},#{sex})
  12. </insert>
  13.  
  14. <update id="updates" parameterType="com.example.demo2.com.example.entity.Shops">
  15. update first set names =#{names},pass=#{pass},sex=#{sex} where id = #{id}
  16. </update>
  17.  
  18. <delete id="deletes" parameterType="com.example.demo2.com.example.entity.Shops">
  19. delete from first where id =#{id}
  20. </delete>
  21.  
  22. </mapper>

application.yml

  1. server:
  2. port: 8088
  3. context-path: /
  4.  
  5. spring:
  6. profiles:
  7. active: dev
  8. application:
  9. name: demo-2
  10. jackson:
  11. date-format: yyyy-MM-dd HH:mm:ss
  12. time-zone: GMT+8
  13. default-property-inclusion: non_null
  14. datasource:
  15. url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF8
  16. username: root
  17. password: root
  18. # 使用druid数据源
  19. type: com.alibaba.druid.pool.DruidDataSource
  20. driver-class-name: com.mysql.jdbc.Driver
  21. filters: stat
  22. maxActive: 20
  23. initialSize: 1
  24. maxWait: 60000
  25. minIdle: 1
  26. timeBetweenEvictionRunsMillis: 60000
  27. minEvictableIdleTimeMillis: 300000
  28. validationQuery: select 'x'
  29. testWhileIdle: true
  30. testOnBorrow: false
  31. testOnReturn: false
  32. poolPreparedStatements: true
  33. maxOpenPreparedStatements: 20
  34.  
  35. mybatis:
  36. typeAliasesPackage: com.example.demo2.com.example.dao
  37. mapperLocations: classpath:mapper/*.xml

项目启动文件

  1. import org.mybatis.spring.annotation.MapperScan;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.transaction.annotation.EnableTransactionManagement;
  5.  
  6. @SpringBootApplication
  7. @EnableTransactionManagement//开启事务管理
  8. @MapperScan("com.example.demo2.com.example.dao")//与dao层的@Mapper二选一写上即可(主要作用是扫包)
  9. public class Demo2Application {
  10.  
  11. public static void main(String[] args) {
  12. SpringApplication.run(Demo2Application.class, args);
  13. }
  14. }

pom文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5.  
  6. <groupId>com.example</groupId>
  7. <artifactId>demo-2</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <packaging>jar</packaging>
  10.  
  11. <name>demo-2</name>
  12. <description>Demo project for Spring Boot</description>
  13.  
  14. <parent>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-parent</artifactId>
  17. <version>1.5.9.RELEASE</version>
  18. <relativePath/> <!-- lookup parent from repository -->
  19. </parent>
  20.  
  21. <properties>
  22. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  23. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  24. <java.version>1.8</java.version>
         <!-- 这个可以使用和spring boot的mybatis两个选一 -->
  25. <!--<mybatis.version>3.3.1</mybatis.version>-->
  26. <!--<mybatis.spring.version>1.2.4</mybatis.spring.version>-->
  27. </properties>
  28.  
  29. <dependencies>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-web</artifactId>
  33. </dependency>
  34.  
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-aop</artifactId>
  38. </dependency>
  39.  
  40. <dependency>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-starter-tomcat</artifactId>
  43. </dependency>
  44.  
  45. <!-- druid -->
  46. <dependency>
  47. <groupId>com.alibaba</groupId>
  48. <artifactId>druid</artifactId>
  49. <version>1.0.11</version>
  50. </dependency>
  51. <dependency>
  52. <groupId>mysql</groupId>
  53. <artifactId>mysql-connector-java</artifactId>
  54. <scope>runtime</scope>
  55. </dependency>
  56. <dependency>
  57. <groupId>org.springframework.boot</groupId>
  58. <artifactId>spring-boot-starter-jdbc</artifactId>
  59. </dependency>
  60.  
  61. <!--Mybatis 二选一 -->
  62. <!--<dependency>
  63. <groupId>org.mybatis</groupId>
  64. <artifactId>mybatis</artifactId>
  65. <version>${mybatis.version}</version>
  66. </dependency>
  67. <dependency>
  68. <groupId>org.mybatis</groupId>
  69. <artifactId>mybatis-spring</artifactId>
  70. <version>${mybatis.spring.version}</version>
  71. </dependency>-->
  72. <dependency>
  73. <groupId>org.mybatis.spring.boot</groupId>
  74. <artifactId>mybatis-spring-boot-starter</artifactId>
  75. <version>1.1.1</version>
  76. </dependency>
  77.  
  78. <!-- Mybatis Generator -->
  79. <dependency>
  80. <groupId>org.mybatis.generator</groupId>
  81. <artifactId>mybatis-generator-core</artifactId>
  82. <version>1.3.2</version>
  83. <scope>compile</scope>
  84. <optional>true</optional>
  85. </dependency>
  86.  
  87. <dependency>
  88. <groupId>org.springframework.boot</groupId>
  89. <artifactId>spring-boot-starter-test</artifactId>
  90. <scope>test</scope>
  91. </dependency>
  92. </dependencies>
  93.  
  94. <build>
  95. <plugins>
  96. <plugin>
  97. <groupId>org.springframework.boot</groupId>
  98. <artifactId>spring-boot-maven-plugin</artifactId>
  99. </plugin>
  100. </plugins>
  101. </build>
  102.  
  103. </project>

以上是ssm的简单流程

以下是jpa的简单流程

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.autoconfigure.domain.EntityScan;
  5. import org.springframework.context.annotation.ComponentScan;
  6. import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  7.  
  8. @ComponentScan(basePackages = {"com.example.demo3.action"})
  9. @EnableJpaRepositories(basePackages = "com.example.demo3.dao")
  10. @EntityScan("com.example.demo3.entity")
  11. @EnableAutoConfiguration
  12. @SpringBootApplication
  13. public class Demo3Application {
  14.  
  15. public static void main(String[] args) {
  16. SpringApplication.run(Demo3Application.class, args);
  17. }
  18. }
  1. import com.example.demo3.dao.FirstDao;
  2. import com.example.demo3.entity.First;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  5. import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.ResponseBody;
  8. import org.springframework.web.bind.annotation.RestController;
  9.  
  10. @RestController
  11. public class Action {
  12.  
  13. @Autowired
  14. private FirstDao firstDao;
  15.  
  16. @RequestMapping("hello")
  17. @ResponseBody
  18. public First test1(Integer id){
  19.  
  20. First first = firstDao.findOne(id);
  21. return first;
  22.  
  23. }
  24.  
  25. }
  1. import javax.persistence.*;
  2.  
  3. @Entity
  4. @Table(name="first")
  5. public class First {
  6.  
  7. @Id
  8. @GeneratedValue(strategy = GenerationType.IDENTITY)
  9. @Column(name = "id")
  10. private Integer id;
  11.  
  12. @Column(name="names")
  13. private String names;
  14.  
  15. @Column(name="pass")
  16. private String pass;
  17.  
  18. @Column(name="sex")
  19. private String sex;
  20.  
  21. public Integer getId() {
  22. return id;
  23. }
  24.  
  25. public void setId(Integer id) {
  26. this.id = id;
  27. }
  28.  
  29. public String getNames() {
  30. return names;
  31. }
  32.  
  33. public void setNames(String names) {
  34. this.names = names;
  35. }
  36.  
  37. public String getPass() {
  38. return pass;
  39. }
  40.  
  41. public void setPass(String pass) {
  42. this.pass = pass;
  43. }
  44.  
  45. public String getSex() {
  46. return sex;
  47. }
  48.  
  49. public void setSex(String sex) {
  50. this.sex = sex;
  51. }
  52. }
  1. import com.example.demo3.entity.First;
  2. import org.springframework.data.jpa.repository.JpaRepository;
  3.  
  4. import java.util.List;
  5.  
  6. public interface FirstDao extends JpaRepository<First,Integer>{
  7.  
  8. }
  1. spring:
  2. datasource:
  3. url: jdbc:mysql:/localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
  4. username: root
  5. password: root
  6. driver-class-name: com.mysql.jdbc.Driver
  7. type: com.alibaba.druid.pool.DruidDataSource
  8. filters: stat
  9. maxActive: 20
  10. initialSize: 1
  11. maxWait: 60000
  12. minIdle: 1
  13. timeBetweenEvictionRunsMillis: 60000
  14. minEvictableIdleTimeMillis: 300000
  15. validationQuery: select 'x'
  16. testWhileIdle: true
  17. testOnBorrow: false
  18. testOnReturn: false
  19. poolPreparedStatements: true
  20. maxOpenPreparedStatements: 20
  21. jpa:
  22. show-sql: true
  23. hibernate:
  24. ddl-auto: create
  25. server:
  26. port : 8088
  27. context-path : /
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5.  
  6. <groupId>com.example</groupId>
  7. <artifactId>demo-3</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <packaging>jar</packaging>
  10.  
  11. <name>demo-3</name>
  12. <description>Demo project for Spring Boot</description>
  13.  
  14. <parent>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-parent</artifactId>
  17. <version>1.5.9.RELEASE</version>
  18. <relativePath/> <!-- lookup parent from repository -->
  19. </parent>
  20.  
  21. <properties>
  22. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  23. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  24. <java.version>1.8</java.version>
  25. </properties>
  26.  
  27. <dependencies>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-web</artifactId>
  31. </dependency>
  32.  
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-data-jpa</artifactId>
  36. </dependency>
  37.  
  38. <dependency>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-starter-aop</artifactId>
  41. <version>1.5.4.RELEASE</version>
  42. </dependency>
  43.  
  44. <dependency>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-starter-tomcat</artifactId>
  47. </dependency>
  48.  
  49. <dependency>
  50. <groupId>mysql</groupId>
  51. <artifactId>mysql-connector-java</artifactId>
  52. <scope>runtime</scope>
  53. </dependency>
  54. <dependency>
  55. <groupId>org.springframework.boot</groupId>
  56. <artifactId>spring-boot-starter-test</artifactId>
  57. <scope>test</scope>
  58. </dependency>
  59.  
  60. <!-- druid -->
  61. <dependency>
  62. <groupId>com.alibaba</groupId>
  63. <artifactId>druid</artifactId>
  64. <version>1.0.11</version>
  65. </dependency>
  66. <dependency>
  67. <groupId>mysql</groupId>
  68. <artifactId>mysql-connector-java</artifactId>
  69. <scope>runtime</scope>
  70. </dependency>
  71. <dependency>
  72. <groupId>org.springframework.boot</groupId>
  73. <artifactId>spring-boot-starter-jdbc</artifactId>
  74. </dependency>
  75.  
  76. </dependencies>
  77.  
  78. <build>
  79. <plugins>
  80. <plugin>
  81. <groupId>org.springframework.boot</groupId>
  82. <artifactId>spring-boot-maven-plugin</artifactId>
  83. </plugin>
  84. </plugins>
  85. </build>
  86.  
  87. </project>

以上就是全部的demo,中间出现了一些问题,也都慢慢的解决了,有了解的朋友可以加我微信

如可以解决的问题,私信发我.

微信

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

  1. 玩转spring boot——简单登录认证

    前言 在一个web项目中,某些页面是可以匿名访问的,但有些页面则不能.spring mvc提供了HandlerInterceptor接口来应对,只需要重写preHandle方法便可以实现此功能.那么使 ...

  2. Spring Boot 简单小Demo 转载!!!

    Spring Boot简介 接下来我们所有的Spring代码实例将会基于Spring Boot,因此我们先来了解一下Spring Boot这个大杀器. Spring早期使用XML配置的方式来配置Spr ...

  3. 基于Angular和Spring WebFlux做个小Demo

    前言 随着Spring Boot2.0正式发布,Spring WebFlux正式来到了Spring Boot大家族里面.由于Spring WebFlux可以通过更少的线程去实现更高的并发和使用更少的硬 ...

  4. windows下elasticsearch配置及spring boot 简单demod的 整合

    学习过程: elasticsearch 下载安装 elasticsearch-head 安装 spring boot 下elasticsearch的配置 使用ElasticsearchReposito ...

  5. RabbitMQ(三):RabbitMQ与Spring Boot简单整合

    RabbitMQ是目前非常热门的一款消息中间件,不管是互联网大厂还是中小企业都在大量使用.Spring Boot的兴起,极大地简化了Spring的开发,本文将使用Spring Boot与RabbitM ...

  6. spring Boot 简单的登录功能,利用了jdbcTemplate.class完成sql语句的执行,无需service层、dao层和.xml文件

    1.搭建SpringBoot项目首先我们先在IDEA上创建一个SpringBoot的Web项目(1)file ——> new ——> project——> Spring Initia ...

  7. Spring Boot简单操作

    目录 一.自定义异常页面 二.单元测试 ​三.多环境选择 四.读取主配置文件中的属性 五.读取List属性 一.自定义异常页面 对于404.405.500等异常状态,服务器会给出默认的异常页面,而这些 ...

  8. Spring Boot整合Dubbo框架demo

    Dubbo框架原理见之前的博文:http://www.cnblogs.com/umgsai/p/5836925.html 首先启动zookeeper Server端 Pom配置如下 <?xml ...

  9. Spring Boot简单应用——会员管理系统

    简介 本项目是使用Spring Boot编写的一个简单的会员管理系统. 提供了会员的解决方案,主要有会员模块,管理员模块,礼品模块,商品模块,会员等级模块,生日提醒模块,积分模块,详细模块如下图 准备 ...

随机推荐

  1. Design Mobile实现国际化

    参考:https://mobile.ant.design/components/locale-provider-cn/   

  2. Selenium自动化-调用Mysql数据库

    上几篇博客发布了几篇Selenium入门知识和进阶, 现在附上如何 从数据库中取值 能够逐行取值,并且返回二维数组 import java.io.FileInputStream; import jav ...

  3. Vsphere 回收未消使用的磁盘空间

    下载sdelete.exe 执行 sdelete.exe -z E: ,然后又恢复为原可用空间 关机   SHH进入物理主机,找到对应的虚机文件 执行vmkfstools -K test-Win200 ...

  4. Android Activity生命周期图解

    Android activity的生命周期这一张图就够了. 验证结果: 值得注意的是从activity A--->activity B是先执行A的onPause然后走B的生命周期最后才走A的on ...

  5. JavaScript中闭包的写法和作用详解

    1.什么是闭包 闭包是有权访问另一个函数作用域的变量的函数. 简单的说,Javascript允许使用内部函数---即函数定义和函数表达式位于另一个函数的函数体内.而且,这些内部函数可以访问它们所在的外 ...

  6. SQL Server非域(跨域)环境下镜像(Mirror)的搭建步骤及注意事项

    在实际的生产环境下,我们经常需要跨域进行数据备份,而创建Mirror是其中一个方案.但跨域创建Mirror要相对复杂的多,需要借助证书进行搭建. 下面我们将具体的步骤总结如下: 第一部分 创建证书 S ...

  7. logback日志配置

    第一步:加入jar包.要加入slf4j和logback的jar包,slf4j需要的jar包为slf4j-api,logback需要2个jar包(logback-classic.logback-core ...

  8. Redis持久化的方式

    Redis小知识: redis是键值对的数据库,有5中主要数据类型: 字符串类型(string),散列类型(hash),列表类型(list),集合类型(set),有序集合类型(zset) Redis持 ...

  9. Linux内存描述之内存节点node--Linux内存管理(二)

    1 内存节点node 1.1 为什么要用node来描述内存 这点前面是说的很明白了, NUMA结构下, 每个处理器CPU与一个本地内存直接相连, 而不同处理器之前则通过总线进行进一步的连接, 因此相对 ...

  10. 【PAT】B1015 德才论

    这道题算是挺简单,我在群里看到的别人琢磨好久过不去,大多是因为没有考虑好四种等级的判断条件 一定要保证四种判断条件正确. 下面这是我的代码,比较笨.后边有别人那学来的聪明些的方法 #include&l ...