苏紫小轩吧 关注:64贴子:1,511
  • 6回复贴,共1

spring-boot项目搭建

只看楼主收藏回复

1.idea下载spring assistant插件。
2.创建spring项目。
3.application.properties文件配置如下
#访问根路径
#应用名称
spring.application.name=lee-demo
#访问端口号
server.port=8080
#编码格式
server.tomcat.uri-encoding=utf-8
#数据库相关配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/lee?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=Huawei@123
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
#session生命周期
server.servlet.session.timeout=30m
########################################################
### JPA持久化配置
########################################################
# 指定数据库的类型
spring.jpa.database = MYSQL
# 指定是否需要在日志中显示sql语句
spring.jpa.show-sql = true
# 指定自动创建|更新|验证数据库表结构等配置,配置成update
# 表示如果数据库中存在持久化类对应的表就不创建,不存在就创建对应的表
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
# 指定命名策略
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# 指定数据库方言
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect


IP属地:北京1楼2019-11-12 17:04回复
    4.LeeAplication如下:
    package com.huawei.seco;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.domain.EntityScan;
    @SpringBootApplication
    @EntityScan("com.huawei.seco.entity")
    public class LeeApplication {
    public static void main(String[] args) {
    SpringApplication.run(LeeApplication.class, args);
    }
    }
    5.新建entity:
    package com.huawei.seco.entity;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import lombok.Getter;
    import lombok.Setter;
    /**
    * GirlEntity
    *
    * @since: 2019/11/12
    */
    @Getter
    @Setter
    @Entity(name = "tb_girl")
    public class GirlEntity {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;
    @Column(name = "name")
    private String name;
    }


    IP属地:北京2楼2019-11-12 17:06
    回复
      6.新建mapper:
      package com.huawei.seco.mapper;
      /**
      * GirlMapper
      *
      * @since: 2019/11/12
      */
      import com.huawei.seco.entity.GirlEntity;
      import org.springframework.data.repository.CrudRepository;
      public interface GirlMapper extends CrudRepository<GirlEntity, Integer> {
      }
      7.新建实现类GirlService:
      package com.huawei.seco.service;
      /**
      * GirlService
      *
      * @since: 2019/11/12
      */
      import com.huawei.seco.entity.GirlEntity;
      import com.huawei.seco.mapper.GirlMapper;
      import javax.annotation.Resource;
      import javax.transaction.Transactional;
      import org.springframework.stereotype.Service;
      @Service
      public class GirlService {
      @Resource
      private GirlMapper girlMapper;
      /*
      * save,update ,delete 方法需要绑定事务. 使用@Transactional进行事务的绑定.
      *
      * @param User 保存对象
      */
      @Transactional
      public void save(GirlEntity User) {
      girlMapper.save(
      User);
      }
      /*
      * 根据id删除对象
      *
      * @param id
      */
      @Transactional
      public void delete(int id) {
      girlMapper.deleteById(id);
      }
      /*
      * 查询数据
      *
      * @return
      */
      public Iterable<GirlEntity> getAll() {
      return girlMapper.findAll();
      }
      /*
      * 修改用户对象数据
      *
      * @param user
      */
      @Transactional
      public void update(GirlEntity user) {
      // 先根据要修改的对象id查询出对应的持久化对象
      GirlEntity sessionUser = girlMapper.findById(user.getId()).get();
      // 直接调用持久化对象的set方法修改对象的数据
      sessionUser.setName(user.getName());
      }
      }


      IP属地:北京3楼2019-11-12 17:07
      回复
        8.新建controller类,相当于northbound:
        package com.huawei.seco;
        import com.huawei.seco.entity.GirlEntity;
        import com.huawei.seco.service.GirlService;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.web.bind.annotation.GetMapping;
        import org.springframework.web.bind.annotation.RequestMapping;
        /**
        * GirlController
        *
        * @since: 2019/11/12
        */
        @org.springframework.web.bind.annotation.RestController
        @RequestMapping("/girl")
        public class GirlController {
        @Autowired
        private GirlService girlService;
        //增加
        @RequestMapping(value="/add")
        public String save()
        {
        GirlEntity girl = new GirlEntity();
        girl.setName("大五");
        girlService.save(girl);
        return "save ok.";
        }
        @RequestMapping("/del")
        public String delete(int id)
        {
        girlService.delete(id);
        return "delete ok.";
        }
        @RequestMapping(value="/getAll", produces="application/json; charset=utf-8")
        public Iterable<GirlEntity> getAll()
        {
        return girlService.getAll();
        }
        }
        9.微软官网下载MySQL. 需要提前下载安装.NET Framework 4.5.2 Microsoft C++ x64(没安装会提示安装mysql失败)
        打开mysql workbench。
        新建database:CREATE DATABASE lee;
        点击左侧SCHEMAS,选择lee。即可执行sql
        10.发送请求
        http://localhost:8080/girl/getAll


        IP属地:北京4楼2019-11-12 17:12
        回复
          前台: resource/static 新建index.html。可以选择任意的前台工具vue等。
          <html>
          <head>
          <script src="/js/vue.js"></script>
          <link rel="stylesheet" type="text/css" href="/css/index.css" />
          </head>
          <body>
          <p> test </p>
          <image class="background"></image>
          <div id="app" v-html="msg">
          {{msg}}
          </div>
          <div id="app2" v-bind:tips="message">this is a file
          <span ></span>
          </div>
          <div id="app-4">
          <ol>
          <li v-for="todo in todos">
          {{ todo.text }}
          </li>
          </ol>
          </div>
          <div id="app-5">
          <p>{{ message }}</p>
          <button v-on:click="reverseMessage">逆转消息</button>
          </div>
          <div id="app8">
          <p>{{ foo }}</p>
          <!-- 这里的 `foo` 不会更新! -->
          <button v-on:click="foo = 'baz'">Change it</button>
          </div>
          <div id="watch-example">
          <p>
          Ask a yes/no question:
          <input v-model="question">
          </p>
          <p>{{ answer }}</p>
          </div>
          <input v-model="message5" placeholder="edit me"></input>
          <p>Message is: {{ message5 }}</p>
          <div>test</div>
          <script>
          var app = new Vue({
          el: '#app',
          data: {
          msg: 'rdgtert'
          }
          })
          var app2 = new Vue({
          el: '#app2',
          data: {
          message: 'Hello Vue!'
          }
          }
          var app5 = new Vue({
          el: '#app-5',
          data: {
          message: 'Hello Vue.js!'
          },
          methods: {
          reverseMessage: function (r) {
          debugger;
          this.message = this.message.split('').reverse().join('')
          }
          }
          })
          var obj = {
          foo: 'bar'
          }
          Object.freeze(obj)
          new Vue({
          el: '#app8',
          data: obj
          }
          </body>
          </html>


          IP属地:北京5楼2019-11-12 17:15
          回复
            资源访问以src/main/resource/static 为基准,给出相对目录即可。
            .background {
            background-image: url(/images/vas.png);
            height: 20px;
            width: 20px;
            }


            IP属地:北京6楼2019-11-12 17:16
            回复
              // 页面加载完成后执行created方法
              created: function () {
              var _this = this;
              axios.get('/girl/getAll')
              .then(function (response) {
              debugger;
              })
              .catch(function (error) {
              })
              },


              IP属地:北京7楼2019-11-12 20:12
              回复