eclipse开发 SpringBoot 入门

官方文档
https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/reference/html/getting-started.html#getting-started

spring boot

1.安装springboot插件
进help===》eclipse user storage ==> open remarkplace Favorites
搜索 Spring Tools 4 - for Spring Boot 版本 4.3.1(必须)
Mybatis Generator (Mybatis 编辑插件,可选)
Thymeleaf Plugin for Eclipse 2.12 (Thymeleaf视图页面模版 可选)

2.直接创建项目
new ===》other ===》springboot ==》spring starter project
修改name 和 artifact 项目名
packaging 打包方式 war和jar war放tomcat 目录 jar 直接运行 java -jar name.jar

  1. next==》选择需要的 支持,
    可以直接忽略 直接finish。以后可以在 pom.xml里面加。

4.创建完毕以后 在window ===》show view ===>other ===>Boot Dashboard 打开 springboot 视图

可以看到 自己创建的Springboot项目 直接右键 start 运行
访问地址 http://localhost:8080/
出现 Whitelabel Error Page 找不到页面

解决办法
写一个静态页面 (目录src/main/resources/static所有静态方法)
src/main/resources/static/index.html

<!DOCTYPE html>
<html>
	<head>
		<title>首页</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	</head>
	<body>首页</body>
</html>

或者 写一个后台Controller
src/main/java/创建controller的package 和 PageController的Class

package controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PageController {
	@RequestMapping("/")
    public String page_index() {
        return "后台首页";
    }
}

@RestController 等于 @Controller + @ResponseBody 合体 返回 字符串或者json

继续访问http://localhost:8080/
返回内容 “后台首页”
后台@RequestMapping("/") 的返回值 会覆盖 index.html的结果。

项目跑起来后 ,配置数据库,这里用的是mysql

//建库
Create database livi;
//建表
CREATE TABLE `livi`.`user` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(45) NULL,
  `password` VARCHAR(45) NULL,
  PRIMARY KEY (`id`));
//数据
insert into livi.user(username,password) values('tom','123456');
insert into livi.user(username,password) values('jerry','123456');
insert into livi.user(username,password) values('alice','123456');
insert into livi.user(username,password) values('linda','123456');

配置数据库链接
pom.xml配置保存后 会自动下载jar包,而且下载的jar包代源文件 可以直接看到jar文件源码,直接查看返回值类型,方法等信息。
还有个好处 用到哪些jar包加入就好了 不用拷贝大量jar文件。整个项目打包文件小很多。
maven资源包搜索网站 https://search.maven.org 版本错了会有红线提醒上这个网站找资源。

pom.xml

	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-jdbc</artifactId>
	</dependency>

application.propertities

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/demo
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=1000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

springboot 支持多种数据库链接方式,下面是用jdbcTemplate,查询效率5ms/次。
使用的连接池HikariPool。
有个缺点queryForMap 查询单条记录 如果找不到直接会空指针异常。只能try catch处理,没有其他办法。


package tech.controller;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DataController {
	//@Autowired 等于在application.properties.xml 加入bean id 
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	//用户列表
	//请求url http://localhost:8080/userlist
	@RequestMapping("/userlist")    
	public List<Map<String,Object>> userlist() {
		String sql="select * from user";
		List<Map<String,Object>> list=jdbcTemplate.queryForList(sql);
		return list;
	}
	
	//查询单条数据
	//请求url http://localhost:8080/userinfo/1
	//等效于 http://localhost:8080/userinfo?id=1
	//userinfo/{id} 其中id改成请求路径变量,据说这样写可以提升速度
	@RequestMapping("/userinfo/{id}")    
	public Object userinfo(@PathVariable String id) {
		Map<String, Object> map = null;
		String sql="select * from user where id="+id;
		try {
			map=jdbcTemplate.queryForMap(sql);
		} catch (Exception e) {
			return "找不到数据,异常了";
		}
		return map;
	}
	
	//添加
	//请求url http://localhost:8080/user_add?username=jack&password=123456
	@RequestMapping("/user_add")
	public String user_add(String username,String password) {
		String sql="insert into user(username,password) values(?,?)";
		try {
			int rows=jdbcTemplate.update(sql,username,password);
			if(rows==1)
			{
				return "插入成功";
			}else if(rows==0)
			{
				return "插入失败";
			}
		} catch (Exception e) {
			return "插入失败,异常了";
		}
		return "";
	}
	
	//更新
	//请求url localhost:8080/user_update/5?username=maria&password=654321
	@RequestMapping("/user_update/{id}")
	public String user_update(@PathVariable String id,String username,String password) {
		Map<String, Object> map = null;
		String sql="update user set username=?, password=? where id="+id;
		try {
			int rows=jdbcTemplate.update(sql,username,password);
			System.err.println(rows);
			if(rows==1)
			{
				return "更新成功";
			}
			else if(rows==0)
			{
				return "更新失败";
			}
		} catch (Exception e) {
			return "更新失败,异常了";
		}
		return "";
	}
	
	//用户删除
	//请求url localhost:8080/user_delete/5
	@RequestMapping("/user_delete/{id}")
	public String user_delete(@PathVariable String id) {
		Map<String, Object> map = null;
		String sql="delete from user where id="+id;
		try {
			int rows=jdbcTemplate.update(sql);
			System.err.println(rows);
			if(rows==1)
			{
				return "已删除";
			}else if(rows==0)
			{
				return "找不到这条记录";
			}
		} catch (Exception e) {
			return "删除失败,异常了";
		}
		return "";
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值