idea搭建spring boot项目
一、创建项目
1.File->new->project;
2.选择“Spring Initializr”,点击next;(jdk1.8默认即可)
3.完善项目信息,组名可不做修改,项目名可做修改;最终建的项目名为:test,src->main->java下包名会是:com->example->test;点击next;
4.Web下勾选Spring Web Start,(网上创建springboot项目多是勾选Web选项,而较高版本的Springboot没有此选项,勾选Spring Web Start即可,2.1.8版本是Spring Web);Template Englines勾选Thymeleaf;SQL勾选:MySQL Driver,JDBC API 和 MyBatis Framework三项;点击next;
5.选择项目路径,点击finish;打开新的窗口;
6.刚创建好的项目目录结构
7.点击右侧的Maven,点击设置(扳手图标)进行项目Maven仓库的配置;
8.(1)选择本地Maven路径;(2)勾选配置文件后边的选项,然后修改为本地Maven的配置文件,它会根据配置文件直接找到本地仓库位置;
9.配置完后,如果没有自动导包,可以点击左上角重新导包按钮,或者呢个下载按钮,选择下载所有源文件和文档
10.在templates文件下新建index.html页面,作为启动的初始页面;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>hello</title>
</head>
<body>
你好!初学者,我是SpringBoot的简单启动页面!
</body>
</html>
11.在com.example.test下新建controller文件夹,在controller文件夹下建一个简单的helloController类;(Controller类要添加@Controller注解,项目启动时,SpringBoot会自动扫描加载Controller)
package com.example.test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/index")
public String sayHello(){
return "index";
}
}
12.在resources文件夹下application中先配置DataSource基本信息,application文件有两种文件格式,一种是以.properties为后缀,一种是以.yml为后缀的,两种配置方式略有差别,详情可参考这个网址:https://blog.csdn.net/qq_29648651/article/details/78503853;在这我是用.yml后缀的文件格式。右键application文件选择Refact,选择Rename,将后缀改为yml;
spring:
datasource:
name: test #数据库名
url: jdbc:mysql://localhost:3306/test #url
username: root #用户名
password: 123456 #密码
driver-class-name: com.mysql.jdbc.Driver #数据库链接驱动
13.运行项目启动类TestApplication.java
可以发现上面有一个WARN警告,那是因为还没有配置编写MyBatis的相关文件,下面会进行详解;
14.在浏览器中输入localhost:8080,回车显示初始的index界面;到这项目的初步搭建已经完成,下面可以下一些简单的业务逻辑,比如从数据库获取信息,登录之类的简单功能;
15.在进行下一步编写时,我们先来链接一下数据库;点击右侧的Database,点“加号”,新建数据库链接;
16.填写数据库相关信息,点击Test Connection;
17.如果链接失败可能是驱动的问题,点击左上角的小扳手,进入数据库设置界面
18.连接成功后,显示数据库信息,user表的基本信息也显示了,下面就照这个来了;
19.SpringBoot项目大概分为四层:
(1)DAO层:包括XxxMapper.java(数据库访问接口类),XxxMapper.xml(数据库链接实现);(这个命名,有人喜欢用Dao命名,有人喜欢用Mapper,看个人习惯了吧)
(2)Bean层:也叫model层,模型层,entity层,实体层,就是数据库表的映射实体类,存放POJO对象;
(3)Service层:也叫服务层,业务层,包括XxxService.java(业务接口类),XxxServiceImpl.java(业务实现类);(可以在service文件夹下新建impl文件放业务实现类,也可以把业务实现类单独放一个文件夹下,更清晰)
(4)Web层:就是Controller层,实现与web前端的交互。
依照上面四层,创建目录结构如下:
20.代码展示:
(1)在application配置文件中添加MyBatis配置:
spring:
datasource:
name: test #数据库名
url: jdbc:mysql://localhost:3306/test #url
username: root #用户名
password: 123456 #密码
driver-class-name: com.mysql.jdbc.Driver #数据库链接驱动
mybatis:
mapper-locations: classpath:mapper/*.xml #配置映射文件
type-aliases-package: com.example.test.bean #配置实体类
(2)pom.xml文件配置信息(备注:这个文件以前没有,2019/12/9日粉丝发现的,这个里面也添加了单元测试所需的配置,记得要重新导一下Maven包哦)
<?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.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--thymeleaf模板引擎配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--Web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--MyBatis配置-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!--MySQL数据库配置-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
<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>
(3)Bean实体类,依据数据库表,生成set和get方法;
package com.example.test.bean;
public class UserBean {
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;
}
}
(4)DAO层访问数据库接口文件:
package com.example.test.mapper;
import com.example.test.bean.UserBean;
public interface UserMapper {
UserBean getInfo(String name,String password);
}
(5)DAO层访问数据库实现文件(需在resource包下创建mapper文件夹,然后再创建一个UserMapper.xml.在application配置文件中mybatis:mapper-locations:对应的就是该文件地址),注意<mapper>标签的namespace属性要填写 访问数据库接口类文件路径:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.test.mapper.UserMapper">
<select id="getInfo" parameterType="String" resultType="com.example.test.bean.UserBean">
SELECT * FROM user WHERE name = #{name} AND password = #{password}
</select>
</mapper>
(6)Service层业务接口类编写:
package com.example.test.service;
import com.example.test.bean.UserBean;
public interface UserService {
UserBean loginIn(String name,String password);
}
(7)Service层业务实现类编写,注意要注解@Service,注入DAO:
package com.example.test.serviceImpl;
import com.example.test.bean.UserBean;
import com.example.test.mapper.UserMapper;
import com.example.test.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 UserBean loginIn(String name, String password) {
return userMapper.getInfo(name,password);
}
}
(8)项目启动类要添加注解@MapperScan项目启动时扫描mapper接口,否则会报错找不到mapper文件:
package com.example.test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.test.mapper")
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
(9)编写测试类,看是否能成功 访问数据库,获取数据库信息:
package com.example.test;
import com.example.test.bean.UserBean;
import com.example.test.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() {
UserBean userBean = userService.loginIn("a","a");
System.out.println("该用户ID为:");
System.out.println(userBean.getId());
}
}
(10) controller层,注意添加@controller注解,注入Service服务:
package com.example.test.controller;
import com.example.test.bean.UserBean;
import com.example.test.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){
UserBean userBean = userService.loginIn(name,password);
if(userBean!=null){
return "success";
}else {
return "error";
}
}
}
(11)html文件:
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<form role="form" action = "/loginIn" method="post">
账号:<input type="text" id="name" name = "name"> <br>
密码:<input type="password" id = "password" name = "password"> <br>
<input type="submit" id = "login" value = "login">
</form>
</body>
</html>
success.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>success</title>
</head>
<body>
<h1>登录成功!</h1>
</body>
</html>
error.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>error</title>
</head>
<body>
<h1>登录失败!</h1>
</body>
</html>
21.先运行测试类,看是否成功获取数据库信息:
22.同时发现一条警告信息,是数据库连接的jar包问题:
2019-08-02 11:25:04.150 WARN 16868 --- [ main] com.zaxxer.hikari.util.DriverDataSource : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
打开pom.xml文件,发现配置文件中未指定数据库连接的jar包的版本号,用version标签引入
重新运行测试类,WARN警告消除
23.运行TestApplication.java文件,启动项目,无任何WARN警告信息,进入浏览器输入localhost:8080/login
项目到这里就算完美结束了。
项目源码放在GitHub上,可以下载参考:https://github.com/redesperado/SpringBoot.git
有一个基于本项目添加增删改查功能的项目,仅供参考:https://github.com/redesperado/test1.git
附一个微服务项目搭建过程,有想学的可以参考一下
IDEA基于springboot采用Dubbo+zookeeper+Redis搭建微服务项目-详细教程:https://blog.csdn.net/baidu_39298625/article/details/108330298
大家如果在创建过程 中遇到什么问题,可以在下边提供的链接中看看,这些是我在创建项目过程遇到的问题,希望可以帮到大家:
1.启动报错:Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
https://blog.csdn.net/baidu_39298625/article/details/98261102
2.mapper.xml文件数据库字段报红
https://blog.csdn.net/baidu_39298625/article/details/98265845
3.项目正常启动,访问默认index页面时404
https://blog.csdn.net/baidu_39298625/article/details/98501840
4. 链接MySQL数据库报错:java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
https://blog.csdn.net/baidu_39298625/article/details/100915264
5.中文用户名登录失败,无报错信息
https://blog.csdn.net/baidu_39298625/article/details/103494461