栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

使用IDEA搭建一个简单的SpringBoot项目——————超级详细(包含一些报错)本人亲测无误

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用IDEA搭建一个简单的SpringBoot项目——————超级详细(包含一些报错)本人亲测无误

直接上步骤



先上目录表:(每个目录用处就不多解释了)



如果报:Connection to @localhost failed. [08001] Could not create connection to database server. Attempt
在数据库路径后面添加:?serverTimezone=GMT

package com.example.test_1020.bean;
 
public class user {
    private int id;
    private String name;
    private String password;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
package com.example.test_1020.controller;

import com.example.test_1020.bean.user;
import com.example.test_1020.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
@Controller
public class LoginController {
 
    //将Service注入Web层
    @Autowired
    UserService userService;
 
    @RequestMapping("/login")
    public String show(){
        return "login";
    }
 
    @RequestMapping(value = "/loginIn",method = RequestMethod.POST)
    public String login(String name,String password){
        user userBean = userService.loginIn(name,password);
        if(userBean!=null){
            return "success";
        }else {
            return "error";
        }
    }
 
}
package com.example.test_1020.mapper;

import com.example.test_1020.bean.user;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;

@Component
public interface UserMapper {
    // 如果有多个参数,需要加@param
    user getInfo(@Param("name") String name,@Param("password") String password);
 
}
package com.example.test_1020.service;

import com.example.test_1020.bean.user;
import org.springframework.stereotype.Component;

@Component
public interface UserService {
 
    user loginIn(String name, String password);
 
}
package com.example.test_1020.serviceImpl;
 
import com.example.test_1020.bean.user;
import com.example.test_1020.mapper.UserMapper;
import com.example.test_1020.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
@Service
public class UserServiceImpl implements UserService {
 
    //将DAO注入Service层
    @Autowired
    private UserMapper userMapper;
 
    @Override
    public user loginIn(String name, String password) {
        return userMapper.getInfo(name,password);
    }

}
Application文件一定要加@MapperScan注解,并标记mapper目录,不然会找不到
@SpringBootApplication
@MapperScan("com.example.test_1020.mapper")
public class Test1020Application {

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

}
测试类:
package com.example.test_1020;

import com.example.test_1020.bean.user;
import com.example.test_1020.service.UserService;
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;
 
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestApplicationTests {
 
    @Autowired
    UserService userService;
 
    @Test
    public void contextLoads() {
        user userBean = userService.loginIn("qwe","111");
        System.out.println("该用户ID为:");
        System.out.println(userBean.getId());
    }
 
}


在resources下创建mapper文件夹,用来存放xml文件

UserMapper.xml:





    
        SELECT *
        FROM user
         WHERe name = #{name} AND password = #{password}
    


templates:




    
    error


登录失败!




    
    hello


你好!初学者,我是SpringBoot的简单启动页面!





    
    login


账号:
密码:



    
    success


登录成功!

application.yml:
## 应用名称
#spring.application.name=test_1020
## 应用服务 WEB 访问端口
#server.port=8080
##下面这些内容是为了让MyBatis映射
##指定Mybatis的Mapper文件
#mybatis.mapper-locations=classpath:mappers/*xml
##指定Mybatis的实体目录
#mybatis.type-aliases-package=com.example.test_1020.mybatis.entity
## 数据库驱动:
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
## 数据源名称
#spring.datasource.name=defaultDataSource
## 数据库连接地址
#spring.datasource.url=jdbc:mysql://localhost:3306/blue?serverTimezone=UTC
## 数据库用户名&密码:
#spring.datasource.username=***
#spring.datasource.password=***

server:
  port:8080:


spring:
  datasource:
    name: test  #数据库名
    url: jdbc:mysql://localhost:3306/test #url
    username: root  #用户名
    password: root  #密码
    driver-class-name: com.mysql.cj.jdbc.Driver  #数据库链接驱动

mybatis:
  mapper-locations: classpath:mapper/*.xml  #配置映射文件
  type-aliases-package: com.example.test_1020.bean #配置实体类

**
如果报时区异常,可以在url: jdbc:mysql://localhost:3306/test后加?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

pom:

**
一些包必须匹配



    4.0.0
    com.example
    test_1020
    0.0.1-SNAPSHOT
    test_1020
    Demo project for Spring Boot

    
        1.8
        UTF-8
        UTF-8
        2.3.7.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter-jdbc
        

        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        

        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.4
        

        
            mysql
            mysql-connector-java

            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
        
            junit
            junit
        
        
            org.springframework.boot
            spring-boot-test
        
        
            org.springframework
            spring-test

        
    

    
        
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.1
                
                    1.8
                    1.8
                    UTF-8
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                2.3.7.RELEASE
                
                    com.example.test_1020.Test1020Application
                
                
                    
                        repackage
                        
                            repackage
                        
                    
                
            
        
    



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/336527.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号