新聞中心
一、創(chuàng)建項(xiàng)目出現(xiàn)的問(wèn)題

- 問(wèn)題1、application.yml文件無(wú)法識(shí)別
- *解決方法:File->Settions->Plugins目錄下 選中YAML,重啟IDEA問(wèn)題2、application.yml文件圖標(biāo)錯(cuò)誤
- *解決方法:File->Settings->File Types下 刪除*.yml
二、創(chuàng)建一個(gè)SpringBoot的項(xiàng)目
1 New Project
2 選擇Spring Initializr
3 選擇Web下的Web,然后Flish
4 導(dǎo)入依賴
org.springframework.boot spring-boot-starter-parent 2.1.5.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools
IDEA設(shè)置,完成熱部署
在Settings中找到Compiler,選中Build Project automatically
按Ctrl+Shift+Alt+/:選中compiler.automake.allow.when.app.running
底層分析:
- spring-boot-starter-parent:springboot起步依賴
- 在spring-boot-starter-parent中 resources資源引入: ${basedir}/src/main/resources下的 application*.yml application*.yaml application*.properties文件 在spring-boot-dependencies中 自動(dòng)根據(jù)spring-boot-starter-parent的版本匹配相應(yīng)的版本,進(jìn)行了版本控制的作用 自動(dòng)配置分析: @SpringBootApplication 標(biāo)志該類是一個(gè)配置類:@Configration
三、SpringBoot整合Mybatis
第1步:導(dǎo)依賴:
-
org.mybatis.spring.boot -
mybatis-spring-boot-starter -
2.0.1 -
mysql -
mysql-connector-java -
5.1.6
第2步:yml配置文件
數(shù)據(jù)庫(kù)配置spring:
配置Mybatis配置信息
spring集成Mybatis環(huán)境
pojo別名掃描包
- spring:
- datasource:
- driver-class-name: com.mysql.jdbc.Driver
- url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&useSSL=false
- username: root
- password: 123456
- mybatis:
- type-aliases-package: com.zero.domain
- mapper-locations: classpath:mapper/*Mapper.xml
第3步:創(chuàng)建實(shí)體
- package com.zero.domain;
- public class User {
- private Integer id;
- private String name;
- private String pass;
- public Integer getId() {
- return id;
- } public void setId(Integer id) {
- this.id = id;
- } public String getName() {
- return name;
- } public void setName(String name) {
- this.name = name;
- } public String getPass() {
- return pass;
- } public void setPass(String pass) {
- this.pass = pass;
- } @Override
- public String toString() {
- return "User{" +
- "id=" + id +
- ", name='" + name + '\'' +
- ", pass='" + pass + '\'' +
- '}';
- } }
第4步:創(chuàng)建接口
- package com.zero.mapper;
- import com.zero.domain.User;
- import org.apache.ibatis.annotations.Mapper;
- import java.util.List;
- @Mapper
- public interface UserMapper {
- public List
queryUserList(); - }
第5步:創(chuàng)建映射文件
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- SELECT * FROM demo
第6步:測(cè)試
- package com.zero.controller;
- import com.zero.domain.User;
- import com.zero.mapper.UserMapper;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import java.util.List;
- //以json格式或字符串格式回寫
- @RestController
- public class democontroller {
- @Autowired
- private UserMapper userMapper;
- @RequestMapping("/quick")
- public List
queryUserList(){ - List
users = userMapper.queryUserList(); - return users;
- }
- }
四、SpringBoot整合Spring Data JPA
第1步:導(dǎo)入依賴
org.springframework.boot spring-boot-starter-data-jpa mysql mysql-connector-java 5.1.6 javax.xml.bind jaxb-api 2.3.0
第2步:創(chuàng)建實(shí)體,使用注解進(jìn)行配置
- package com.zero.domain;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- @Entity
- public class User {
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private Integer id;
- private String name;
- private String pass;
- public Integer getId() {
- return id;
- }public void setId(Integer id) {
- this.id = id;
- }public String getName() {
- return name;
- }public void setName(String name) {
- this.name = name;
- }public String getPass() {
- return pass;
- }public void setPass(String pass) {
- this.pass = pass;
- }@Override
- public String toString() {
- return "User{" +
- "id=" + id +
- ", name='" + name + '\'' +
- ", pass='" + pass + '\'' +
- '}';
- }}
第3步:創(chuàng)建接口
- package com.zero.reposytory;
- import com.zero.domain.User;
- import org.springframework.data.jpa.repository.JpaRepository;
- import java.util.List;
- public interface UserReposytory extends JpaRepository
{ - public List
findAll(); - }
第4步:創(chuàng)建yml配置
- spring:
- datasource:
- driver-class-name: com.mysql.jdbc.Driver
- url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&useSSL=false
- username: root
- password: 123456
- #JPA Configuration
- jpa:
- database: MySQL
- show-sql: true
- generate-ddl: true
- hibernate:
- ddl-auto: update
第5步:測(cè)試
- package com.zero;
- import com.zero.domain.User;
- import com.zero.reposytory.UserReposytory;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.test.context.junit4.SpringRunner;
- import java.util.List;
- @RunWith(SpringRunner.class)
- @SpringBootTest(classes = Demo614Application.class)
- public class JpaTest {
- @Autowired
- private UserReposytory userReposytory;
- @Test
- public void test(){
- List
all = userReposytory.findAll(); System.out.println(all); - }}
五、Redis緩存
第1步:配置yml文件信息
- #redis
- redis:
- host: 127.0.0.1
- port: 6379
第2步:測(cè)試
package com.zero;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.zero.domain.User;
- import com.zero.reposytory.UserReposytory;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.test.context.junit4.SpringRunner;
- import java.util.List;
- @RunWith(SpringRunner.class)
- @SpringBootTest(classes = Demo614Application.class)
- public class RedisTest {
- @Autowired
- private RedisTemplate
redisTemplate; - @Autowired
- private UserReposytory userReposytory;
- @Test
- public void test() throws JsonProcessingException {
- //1.從Redis中獲取數(shù)據(jù),json字符串
- String s = redisTemplate.boundValueOps("user.findall").get();
- //2.判斷Redis中是否存在想要的數(shù)據(jù)
- if(null==s){
- //3.1:不存在,從數(shù)據(jù)庫(kù)中查詢
- List
all = userReposytory.findAll(); - //3.2:將查詢出的數(shù)據(jù)存儲(chǔ)到Redis中
- //通過(guò)web先將集合換換成json的字符串,使用Jackson進(jìn)行轉(zhuǎn)換
- ObjectMapper ob = new ObjectMapper();
- s = ob.writeValueAsString(all);
- redisTemplate.boundValueOps("user.findall").set(s);
- System.out.println("從數(shù)據(jù)庫(kù)中獲取數(shù)據(jù)");
- }else {
- System.out.println("從Redis緩存中獲取數(shù)據(jù)");
- }
- //4.將數(shù)據(jù)打印在控制臺(tái)
- System.out.println(s);
- }
當(dāng)前標(biāo)題:SpringBoot入門練習(xí)
當(dāng)前網(wǎng)址:http://fisionsoft.com.cn/article/cdhijsc.html


咨詢
建站咨詢
