添加依赖
<?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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springboot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>springboot Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<!--管理jdk版本 -->
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--web启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--SpringData Jpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<!-- HttpClient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<!--Jsoup-->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.3</version>
</dependency>
<!--工具包-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!-- 通用mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- 指定端口 -->
<port>8080</port>
<!-- 请求路径 -->
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties
#DB Configuration:
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/demo
spring.datasource.username=root
spring.datasource.password=root
#JPA Configuration:
spring.jpa.database=MySQL
spring.jpa.show-sql=true
#mybatis
# 实体类别名包路径
mybatis.type-aliases-package=com.springboot.pojo
# 映射文件路径
#mybatis.mapper-locations=classpath:mappers/*.xml
# 控制台输出执行sql
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
启动类 Application
package com.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
//设置开启定时任务
@EnableScheduling
@MapperScan("com.springboot.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
pojo
package com.springboot.pojo;
import lombok.Data;
import tk.mybatis.mapper.annotation.KeySql;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
@Data
@Table(name = "tb_user")
public class User{
@Id //开启主键自动回填
@KeySql(useGeneratedKeys = true)
private Long id;// id
private String userName; // 用户名
private String password;// 密码
private String name; // 姓名
private Integer age; // 年龄
private Integer sex; // 性别,1男性,2女性
private Date birthday; // 出生日期
private Date created;// 创建时间
private Date updated;// 更新时间
private String note; // 备注
}
controller
package com.springboot.Controller;
import com.springboot.pojo.User;
import com.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.sql.DataSource;
@RestController
public class HelloController {
@Autowired
private UserService userService;
/**
* 根据id获取用户
* @param id 用户id
* @return 用户
*/
@GetMapping("/user/{id}")
public User queryById(@PathVariable Long id){
return userService.queryById(id);
}
}
service
package com.springboot.service;
import com.springboot.dao.UserDao;
import com.springboot.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class UserService {
@Autowired
private UserDao userDao;
public User queryById(Long id){
//根据id查询
return userDao.selectByPrimaryKey(id);
}
@Transactional
public void saveUser(User user){
System.out.println("新增用户...");
}
}
dao
package com.springboot.dao;
import com.springboot.pojo.User;
import tk.mybatis.mapper.common.Mapper;
public interface UserDao extends Mapper<User> {
}
test
package com.springboot.test;
import com.springboot.pojo.User;
import com.springboot.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 UserServiceTest {
@Autowired
private UserService userService;
@Test
public void queryById() {
User user = userService.queryById(1L);
System.out.println("user = " + user);
}
}
整合Redis
依赖
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
application.properties
#redis
spring.redis.host=192.168.0.148
spring.redis.port=6379
test
package com.springboot.test;
import org.junit.runner.RunWith;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import java.util.Set;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void redisTest(){
redisTemplate.boundValueOps("str").set("heima");
System.out.println("str:"+redisTemplate.opsForValue().get("str"));
//hash散列
redisTemplate.boundHashOps("h_key").put("name","heima");
redisTemplate.boundHashOps("h_key").put("age",13);
//获取所有与对应的值
Set set = redisTemplate.boundHashOps("h_key").keys();
System.out.println("hash散列所有的域:"+set);
List list = redisTemplate.boundHashOps("h_key").values();
System.out.println("hash散列所有的值:"+list);
//list列表
redisTemplate.boundListOps("l_key").leftPush("d");
redisTemplate.boundListOps("l_key").leftPush("c");
redisTemplate.boundListOps("l_key").leftPush("b");
redisTemplate.boundListOps("l_key").leftPush("a");
List l_key = redisTemplate.boundListOps("l_key").range(0, -1);
System.out.println("list的值:"+l_key);
//set集合
redisTemplate.boundSetOps("set_key").add("a","b","c");
Set set_key = redisTemplate.boundSetOps("set_key").members();
System.out.println("set的值:"+set_key);
//sorted set 有序集合
redisTemplate.boundZSetOps("z_key").add("a",30);
redisTemplate.boundZSetOps("z_key").add("b",20);
redisTemplate.boundZSetOps("z_key").add("c",10);
Set z_key = redisTemplate.boundZSetOps("z_key").range(0, -1);
System.out.println("sorted set 有序集合:"+z_key);
}
}
Spring Boot项目部署
项目打包
1. 在pom.xml加入插件spring-boot-maven-plugin
<build>
<plugins>
<!-- 打jar包时如果不配置该插件,打出来的jar包没有清单文件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
使用maven的命令package打包;
之后在项目下的target 目录中将有如下jar包:
附录—插件安装
springboot需要创建启动引导类Application.java和application.yml配置文件,安装插件 JBLSpringBootAppGen 在项目上右击之后自动生Application.java和application.yml
在maven项目或src目录上 右击,选择 JBLSpringBootAppGen。