springboot–jpa03

pom.xml

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

application.yml

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/nc
    username: root
    password: root
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

实体类Girl.java

package com.rick;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/**
 * Created by rick on 2017/1/10.
 */
@Entity
public class Girl {
    @Id
    @GeneratedValue
    private Integer id;

    private String cupSize;

    private Integer age;

    public Girl() {
    }

    public Integer getId() {
        return id;
    }

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

    public String getCupSize() {
        return cupSize;
    }

    public void setCupSize(String cupSize) {
        this.cupSize = cupSize;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

daoGirlRepository.java自动被spring管理

package com.rick;

import org.springframework.data.jpa.repository.JpaRepository;

/**
 * Created by rick on 2017/1/10.
 */
public interface GirlRepository extends JpaRepository<Girl, Integer> {
}

service GirlService.java

package com.rick;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;

/**
 * Created by rick on 2017/1/10.
 */
@Service
public class GirlService {
    @Resource
    GirlRepository girlRepository;

    @Transactional //方法纳入事务
    public void addTwoGirls() {
        Girl girl1 = new Girl();
        girl1.setAge(11);
        girl1.setCupSize("A");
        girlRepository.save(girl1);

        Girl girl2 = new Girl();
        girl2.setAge(30);
        girl2.setCupSize("G");
        girlRepository.save(girl2);

    }
}

JpaRepository中的其他方法:

* java.util.List<T> findAll();
* java.util.List<T> findAll(org.springframework.data.domain.Sort sort);
* java.util.List<T> findAll(java.lang.Iterable<ID> iterable);
* <S extends T> java.util.List<S> save(java.lang.Iterable<S> iterable);
* void flush();
* <S extends T> S saveAndFlush(S s);
* void deleteInBatch(java.lang.Iterable<T> iterable);
* void deleteAllInBatch();
* T getOne(ID id);
* <S extends T> java.util.List<S> findAll(org.springframework.data.domain.Example<S> example);
* <S extends T> java.util.List<S> findAll(org.springframework.data.domain.Example<S> example, org.springframework.data.domain.Sort sort);

springboot–属性配置02

spring默认的属性配置文件是:application.propertity
推荐使用application.yml

server:
  port: 8080
cupSize: F
girl:
  cupSize: G
  age: 22

GirlProperty.java

package com.rick;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * Created by rick on 2017/1/10.
 */

@ConfigurationProperties(prefix = "girl")
@Component
public class GirlProperty {

    private Integer age;

    private String cupSize;

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getCupSize() {
        return cupSize;
    }

    public void setCupSize(String cupSize) {
        this.cupSize = cupSize;
    }
}

属性使用HelloController.java

package com.rick;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Created by rick on 2017/1/10.
 */

@RestController
public class HelloController {

    @Value("${cupSize}")
    private String cupSize;

    @Resource
    private GirlProperty girlProperty;

    @RequestMapping(value="/hello", method = RequestMethod.GET)
    public String sayHello() {        
        return girlProperty.getCupSize() + "=>" + girlProperty.getAge();

}

springboot–第一个springboot应用01

简介

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

创建第一个springboot应用

工具:

  • idea2016
  • jdk:1.8
  • maven:3.3.9

idea2016快速创建springboot应用

操作步骤:创建Project/Spring Initializr/Web/Web

自动生成文件如下:

pom.xml

<?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.rick</groupId>
    <artifactId>girl</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>girl</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.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-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>

GirlApplication.java

package com.rick;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class GirlApplication {

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

HelloController.java

package com.rick;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Created by rick on 2017/1/10.
 */

@RestController
public class HelloController {


    @RequestMapping(value="/hello", method = RequestMethod.GET)
    public String sayHello() {
        return "Hello SpringBoot";
    }

}

启动应用,访问:http://localhost:8080/hello
学习网站:http://www.imooc.com/learn/767

vue插件开发

插件MyPlugin.js

module.exports = (function() {
  var install = function install(Vue) {
       if (install.installed) return;

         // 1. 添加全局方法或属性
        Vue.myGlobalMethod = function () {
          // 逻辑...
        }
        // 2. 添加全局资源
        Vue.directive('my-directive', {
          bind (el, binding, vnode, oldVnode) {
            // 逻辑...
            el.innerHTML = 'DDDD'

          }
        })
        // 3. 注入组件
        Vue.mixin({
          created: function () {
            // 逻辑...
          }
        })
        // 4. 添加事例方法
        Vue.prototype.$myMethod = function (options) {
          // 逻辑...
        }
  }

  return install;

})()

调用main.js

import Vue from 'vue'
import App from './App.vue'
import MyPlugin from './MyPlugin'

Vue.use(MyPlugin)

var vm = new Vue({
  el: '#app',
  render: h => h(App)
})

Vue组件App.vue

<template>
  <div id="app">
     <div v-my-directive></div>
  </div>
</template>

<script>
export default {
    data() {
        return {
        }
    },
    methods: {

    },
    mounted: function() {

    }
}
</script>

参考网站
https://cn.vuejs.org/v2/guide/plugins.html

vue组件开发

创建组件rate.vue

<template lang="html">
   <li>{{message}}</li>
</template>

<script>

export default {
  name: 'vb-rate',
  props:['message'],
  methods: {
  }
}
</script>

使用组件

有两种方式,全局注册和局部注册

全局注册:

main.js

import Vue from 'vue'
import App from './App.vue'

import Rate from './components/rate/rate.vue'

Vue.component(Rate.name, Rate); //全局注册组件



new Vue({
  el: '#app',
  render: h => h(App)
})

App.vue.js

<template>
  <div id="app">
    <!--使用全局注册,直接使用-->
    <ol>
      <vb-rate v-for="item in groceryList" :message="item.text"></vb-rate>
      <vb-rate message="Rick"></vb-rate>
    </ol>

  </div>
</template>

<script>

import Rate from './components/rate/rate.vue' //

export default {
  data () {
    return {
      groceryList: [
            { text: 'Vegetables' },
            { text: 'Cheese' },
            { text: 'Whatever else humans are supposed to eat' }
          ]
    }
  }
}
</script>

局部注册

main.js

import Vue from 'vue'
import App from './App.vue'

new Vue({
  el: '#app',
  render: h => h(App)
})
<template>
  <div id="app">
    <!--使用局部注册-->
    <ol>
      <vb-rate v-for="item in groceryList" :message="item.text"></vb-rate>
      <vb-rate message="Rick"></vb-rate>
    </ol>

  </div>
</template>

<script>

import Rate from './components/rate/rate.vue' //

export default {
  data () {
    return {
      groceryList: [
            { text: 'Vegetables' },
            { text: 'Cheese' },
            { text: 'Whatever else humans are supposed to eat' }
          ]
    }
  },
  components: {
    [Rate.name] : Rate// 局部注册组件
  }
}
</script>

这个地方有个注意点:

<vb-rate :message="item.text"></vb-rate>
<vb-rate message="Rick"></vb-rate>
message有`:`表示vue变量,没有`:`表示字符串