springboot简单搭建

优点

  1. 配置变得简单了,大多数情况使用默认配置即可;
    比如maven依赖,使用starter简化maven的配置
  2. 项目搭建简单了,内置集成第三方的框架,无配置集成;
  3. 项目的部署简单了,内嵌Servlet容器,如Tomcat,Jetty等;
    可以以jar包形式直接运行项目,无需打成war;
  4. 提供了许多的功能,如应用监测等;

项目环境准备

  1. JDK,建议1.8及以上版本;
  2. 开发工具,这里使用Eclipse;
  3. 数据库,这里使用mysql;
  4. springboot,这里版本使用2.4.5;
    官网:https://spring.io/projects/spring-boot
  5. 项目管理工具:maven。版本需3.3+

步骤一:springboot初始环境搭建

  1. 打开eclipse,右键新建maven项目
    (注意:这里eclipse已经配置好Maven环境,eclipse集成maven待后续补充);
    在这里插入图片描述
  2. 填写组织,及工程名,打成jar包,并点击finish完成;
    在这里插入图片描述
  3. 配置pom文件,构建springboot父工厂,添加依赖并保存;
<!-- 构建springboot项目 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.5</version>
	</parent>

<!-- 添加类路依赖 -->
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>
  1. 项目更新,右键/Maven/update Project/ok 即完成springboot初始环境搭建;
    (更改pom.xml文件后,通常需要执行update project更新外部依赖的jar包。以此让eclipse知道更改的内容)
    在这里插入图片描述

运行springboot

  1. 创建类,类不重要,类名路径可以是随意的
    在这里插入图片描述

  2. 编写main函数,标明启动类并加载启动类运行springboot

/**
 * springboot启动类
 * @author jie
 */
 //表明当前类是springboot的启动类
 //启动类包含了配置注解,开启自动扫描等注解。在当前启动类及其子孙类下的注解可被扫描
@SpringBootApplication	
public class StartupApplication {

	public static void main(String[] args) {
		//启动类加载(参数1:启动类字节码;参数2:运行所需参数)
		SpringApplication.run(StartupApplication.class, args);
	}
}

springboot入门

1.springmvc无缝整合,编写controller

//包含@Controller和@ResponseBody
@RestController
@RequestMapping("/springmvc")
public class TestController {

	@RequestMapping("/hello")
	public String hello() {
		return "hello springboot";
	}
}

在springboot中使用静态文件(如html)

1.需在resource下新建文件夹“static”。可在此目录下放置静态文件。
(提问:为什么在resource下命名为static呢?
因为这是springboot给我们定义的默认路径。默认首页为index.html)
在这里插入图片描述

在springboot整合mybatis

1.pom引入mybatis及数据库依赖

	<!-- mybatis -->
		<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- druid -->
        <dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.10</version>
		</dependency>

2.在数据库中建表user

DROP TABLE IF EXISTS auth_user;
CREATE TABLE auth_user
(
  id int(11) NOT NULL AUTO_INCREMENT COMMENT 'PK',
	user_name VARCHAR(20) not null,
	password VARCHAR(20) not null,
	PRIMARY KEY(id)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=COMPACT COMMENT='用户表';

//说明:
//utf8和utf8mb4的区别:mb4就是most bytes 4的意思,专门用来兼容四字节的unicode。
//utf8 编码最大字符长度为 3 字节,如果遇到 4 字节的宽字符就会插入异常了。如Emoji表情。
//COLLATE 字符编码排序的规则,会影响到order By ,group by,where等语句比较查询的结果。
//_ci后缀:Case Insensitive的缩写,即大小写无关,也就是说在查询的时候搜索'a'和‘A’是一样的。 
//_cs后缀的COLLATE,则是Case Sensitive,即大小写敏感的。
//ROW_FORMAT,物理行记录格式。偏硬件方面的问题,不细究。

3.这里,我喜欢先设置配置文件,在resource下新建yml主配置文件:application.yml。并设置内容。
(提问:properties和yml的区别?
其实,都只是配置文件,区别只是格式不同,比如properties以’.‘分隔,而yml文件以’:'分隔。
且yml文件树状结构,对中文支持更好,个人推荐yml)

 spring:
  profiles:
    active: dev 
    ##真实项目中会有很多开发环境,不同环境的配置不尽相同,这里指定项目运行加载哪个配置文件
    ##Spring Boot中多环境配置文件名需要满足application-{profile}.yml的格式

4.需在resource下新建yml开发环境配置文件:application-dev.yml。并设置内容。

server:
  port: 8080 ##指定项目运行在哪个端口

## 数据库连接
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource  ##数据源的类型
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver

## mybatis配置映射文件所在路径
mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml  ##扫描mapper文件
  type-aliases-package: com.xzj.entity ##配置文件中映射到实体类时可以省略改路径名
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl ## mybatis 语句控制台打印

5.配置文件配置好后,可以开始书写java代码了。直接上代码:
Entity实体类

public class UserEntity implements Serializable {
	private Long id;
	private String userName;
	private String password;

	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

Dao层接口

@Mapper
public interface UserDao {
	public List<UserEntity> queryUserByName(String name);
}

Service层接口

public interface UserService {
	List<UserEntity> list(String name);
}

Service层实现类

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;
	
	@Override
	public List<UserEntity> list(String name){
		List<UserEntity> userList = userDao.queryUserByName(name);
		return userList;
	}
}

Controller层

@RestController
@RequestMapping("/test")
public class TestController {

	@Autowired
	private UserService userService;
	
	@RequestMapping("/list/{name}")
	private List<UserEntity> list(@PathVariable("name") String name){
		return userService.list(name);
	}
}

6.在resource下新建mapper文件夹,书写mapper文件(和配置文件里面的路径规则要对应上):

<?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.xzj.dao.UserDao">
 
    <select id="queryUserByName" resultType="com.xzj.entity.UserEntity">
        select * from auth_user where username like concat(concat('%',#{id}),'%')
    </select>
</mapper>

7.此时,一个springboot项目便被我们构建起来了,可以运行启动类,访问下试试:
http://localhost:8080/test/list/dm
在这里插入图片描述
在看看控制台,springboot项目与mybatis整合执行成功
在这里插入图片描述
在此,springboot简单搭建完成。

自我小结:

工作快一年了,整天忙于工作的业务中,本身掌握的知识在工作中没用到竟悄悄消失,一年没写笔记了,用一篇:springboot简单搭建 重新开整。
学无止境,提升自我。
技术不精,可能写的不是很好。文中有错之处,还望各位大佬们帮忙指出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值