Spring boot + Mybatis + Thymeleaf + Druid +mySql

别裁伪体亲风雅,转益多师是汝师。这篇文章主要讲述Spring boot + Mybatis + Thymeleaf + Druid +mySql相关的知识,希望能为你提供帮助。


目录

??前言??
??开发环境(小编使用的版本)??
??正题??
??步骤??
前言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 boot + Mybatis + Thymeleaf + Druid +mySql

文章图片

2.创建项目文件结构
Spring boot + Mybatis + Thymeleaf + Druid +mySql

文章图片

3.POM依赖
如果使用阿里云的连接池不选择JDBC,小编用的阿里云这里不选择
Spring boot + Mybatis + Thymeleaf + Druid +mySql

文章图片

点击Finish;
注:可能Maven中无Jar包需要从仓库下载,需要耐心等待(可以去听首歌)
4.项目结构
Spring boot + Mybatis + Thymeleaf + Druid +mySql

文章图片

5.POM
< ?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>

< parent>

< groupId> org.springframework.boot< /groupId>

< artifactId> spring-boot-starter-parent< /artifactId>

< version> 2.1.1.RELEASE< /version>

< relativePath/> < !-- lookup parent from repository -->

< /parent>

< groupId> com.spring< /groupId>

< artifactId> boot< /artifactId>

< version> 0.0.1-SNAPSHOT< /version>

< name> boot< /name>

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


< properties>

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

< /properties>


< dependencies>

< !-- Springboot 热部署 -->

< dependency>

< groupId> org.springframework.boot< /groupId>

< artifactId> spring-boot-devtools< /artifactId>

< optional> true< /optional>

< /dependency>

< !-- 阿里的Druid连接池 -->

< dependency>

< groupId> com.alibaba< /groupId>

< artifactId> druid-spring-boot-starter< /artifactId>

< version> 1.1.10< /version>

< /dependency>

< dependency>

< groupId> org.springframework.boot< /groupId>

< artifactId> spring-boot-starter-thymeleaf< /artifactId>

< /dependency>

< dependency>

< groupId> org.springframework.boot< /groupId>

< artifactId> spring-boot-starter-web< /artifactId>

< /dependency>

< dependency>

< groupId> org.mybatis.spring.boot< /groupId>

< artifactId> mybatis-spring-boot-starter< /artifactId>

< version> 1.3.2< /version>

< /dependency>


< dependency>

< groupId> mysql< /groupId>

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

< scope> runtime< /scope>

< /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>



注:IDEA失效解决方法
Spring boot + Mybatis + Thymeleaf + Druid +mySql

文章图片

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< User> AllUser();



@Update("< script> " + "update user" +

"< set> "+ "< if test=uname!=null> uname=#{uname},< /if> "+

"< if test=upassword!=null> upassword=#{upassword},< /if> "+

"< /set> "+ "where uid=#{uid}"+

" < /script> ")

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< User> 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< User> AllUser() {


return userDao.AllUser();

}


@Override

public int Update(User user) {

return userDao.Update(user);

}

}

注:userDao报红解决方法
Spring boot + Mybatis + Thymeleaf + Druid +mySql

文章图片

【Spring boot + Mybatis + Thymeleaf + Druid +mySql】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
< !DOCTYPE html>

< html xmlns:th="http://www.thymeleaf.org">

< head>

< meta charset="UTF-8">

< title> Title< /title>

< /head>

< body>

Spring boot+Thymeleaf!< br>

< span th:text="${user}"> < /span>

< /body>

< /html>

8.??界面??显示
Spring boot + Mybatis + Thymeleaf + Druid +mySql

文章图片

注:后续功能再写!
Java web项目,@Controller注解是界面、@RestController是写接口











    推荐阅读