Spring框架詳解

java愛好者i發表於2019-03-27

前言

Spring boot + Mybatis + Thymeleaf + Druid +mySql

開發環境(小編使用的版本)

JDK版本 :1.8及以上 (JDK1.8);

開發工具 :Intellij IDEA (IDEA2018.2);

伺服器 :Tomcat(務必比JDK版本高,小編不在解釋(Jar包不用配置、War需要配置)) (Tomcat9) ;

JRE包 :Maven倉庫 (Maven3.6);

資料庫 :MySql(MySql5.5) ;

正題

Spring boot :2.1.1RELEASE ;

Thymeleaf

Mybatis

阿里雲的連線池 : Druid ;

步驟

1.建立Springboot:

Spring框架詳解

2.建立專案檔案結構

Spring框架詳解

3.POM依賴

如果使用阿里雲的連線池不選擇JDBC,小編用的阿里雲這裡不選擇

Spring框架詳解

點選Finish;

注:可能Maven中無Jar包需要從倉庫下載,需要耐心等待(可以去聽首歌)

4.專案結構

Spring框架詳解

5.POM

<project xmlns="maven.apache.org/POM/4.0.0" xmlns:xsi="www.w3.org/2001/XMLSch…"

xsi:schemaLocation="maven.apache.org/POM/4.0.0 maven.apache.org/xsd/maven-4…">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.1.1.RELEASE

com.spring

boot

0.0.1-SNAPSHOT

boot

Demo project for Spring Boot

<java.version>1.8</java.version>

org.springframework.boot

spring-boot-devtools

true

com.alibaba

druid-spring-boot-starter

1.1.10

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-starter-web

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.3.2

mysql

mysql-connector-java

runtime

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-maven-plugin

注:IDEA失效解決方法

Spring框架詳解

6.小編使用的application.properties檔案,專案啟動識別兩種格式的檔案properties和yml檔案:

#快取設定為false, 修改之後馬上生效

spring.thymeleaf.cache=false

spring.thymeleaf.encoding=UTF-8

#spring.thymeleaf.prefix=classpath:/templates/

#spring.thymeleaf.suffix=.html

server.port=8080

server.tomcat.uriEncoding=utf-8

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

spring.datasource.url=jdbc:mysql://localhost:3306/web?useUnicode=true&serverTimezone=GMT%2B8&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false

spring.datasource.username=root

spring.datasource.password=root

spring.datasource.druid.driverClassName=com.mysql.cj.jdbc.Driver

#初始化連線

spring.datasource.initialSize=20

#最大空閒數

spring.datasource.maxActive=50

#最小空閒數

spring.datasource.minIdle=10

#獲取連線等待時間

#spring.datasource.druid.max-wait=60000

#最小等待時間

#spring.datasource.minEvictableIdleTimeMillis=3600000

7.其他檔案生成

User.java

package com.spring.boot.bean;

public class User {

public Integer uid;

public String uname;

public String upassword;

public Integer getUid() {

return uid;

}

public void setUid(Integer uid) {

this.uid = uid;

}

public String getUanme() {

return uname;

}

public void setUanme(String uanme) {

this.uname = uanme;

}

public String getUpassword() {

return upassword;

}

public void setUpassword(String upassword) {

this.upassword = upassword;

}

@Override

public String toString() {

return "User{" +

"uid=" + uid +

", uname='" + uname + ''' +

", upassword='" + upassword + ''' +

'}';

}

}

UserDao.java

註解的形式,小編感覺比XML好用

package com.spring.boot.dao;

import com.spring.boot.bean.User;

import org.apache.ibatis.annotations.Select;

import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface UserDao {

@Select("select * from user")

public List AllUser();

@Update(" ")

public int Update(User user);

}

UserService.java

package com.spring.boot.service;

import com.spring.boot.bean.User;

import java.util.List;

public interface UserService {

public List AllUser();

public int Update(User user);

}

UserImpl.java

主要是註解問題Service可以命名,主要還是看自己的日常使用

package com.spring.boot.service.impl;

import com.spring.boot.bean.User;

import com.spring.boot.dao.UserDao;

import com.spring.boot.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;

@Service

public class UserImpl implements UserService {

@Autowired

private UserDao userDao;

@Override

public List AllUser() {

return userDao.AllUser();

}

@Override

public int Update(User user) {

return userDao.Update(user);

}

}

注:userDao報紅解決方法

Spring框架詳解

UserController.java

package com.spring.boot.controller;

import com.spring.boot.bean.User;

import com.spring.boot.service.impl.UserImpl;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

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

@Controller

public class UserController {

@Autowired

private UserImpl userimpl;

@RequestMapping("/index")

public String index(Model model, User user){

model.addAttribute("user",userimpl.AllUser());

user.uid = 2;

user.uname = "nan";

user.upassword = "lou";

userimpl.Update(user);

System.out.println("******************"+userimpl.AllUser());

System.out.println("******************"+userimpl.Update(user));

return "index";

}

}

index.html

Title

Spring boot+Thymeleaf!

8.介面顯示

Spring框架詳解

注:後續功能再寫!

歡迎工作一到五年的Java工程師朋友們加入Java架構師:697558955

群內提供免費的Java架構學習資料(裡面有高可用、高併發、高效能及分散式、Jvm效能調優、Spring原始碼,MyBatis,Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginx等多個知識點的架構資料)合理利用自己每一分每一秒的時間來學習提升自己,不要再用"沒有時間“來掩飾自己思想上的懶惰!趁年輕,使勁拼,給未來的自己一個交代!

相關文章