A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 柠檬leung不酸 黑马粉丝团   /  2019-1-22 14:52  /  919 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 柠檬leung不酸 于 2019-1-22 14:56 编辑

前言
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:
11.png


2.创建项目文件结构

12.png

3.POM依赖

如果使用阿里云的连接池不选择JDBC,小编用的阿里云这里不选择


点击Finish;
注:可能Maven中无Jar包需要从仓库下载,需要耐心等待(可以去听首歌)

4.项目结构
14.png

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失效解决方法
15.png


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报红解决方法
16.png


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.界面显示

17.png

注:后续功能再写!
小编这是Java web项目,@Controller注解是界面、@RestController是写接口
哪里又不对的给提醒
---------------------
作者:沐荔
来源:CSDN
原文:https://blog.csdn.net/qq_41920732/article/details/85936063

13.png

1 个回复

倒序浏览
看一看。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马