用Spring Boot进行后端开发(一):将Spring Boot项目部署到腾讯云服务器(CentOS)并且成功运行

工具:IDEA,centos服务器,MySQL

一.新建Spring Boot项目

1.打开IDEA新建项目 New Project 选择Spring initalizr快速创建一个spring boot项目 

2.点击Next,除了标红的,其它都不用修改

3.选择打勾依赖

 

4.点击Next,再点击Finish完成创建

二.配置Druid依赖

1.在pom.xml文件中添加Druid依赖

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.18</version>
		</dependency>

2.在src/main/resources文件夹下新建application.yml文件,并且添加数据源信息

   其中username,password,url要改成自己的(Mysql数据库的用户名,密码,地址)

 

spring:
  datasource:
#   数据源基本配置
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://125.36.28.117:3306/mybatis
    type: com.alibaba.druid.pool.DruidDataSource
#   数据源其他配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
#   配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    #filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

 3.编写Druid配置类

@Configuration
public class DruidConfig {
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druid() {
        return new DruidDataSource();
    }

    //配置Druid的监控
    //1、配置一个管理后台的Servlet
    @Bean
    public ServletRegistrationBean statViewServlet() {
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        Map<String, String> initParams = new HashMap<>();

        initParams.put("loginUsername", "admin");
        initParams.put("loginPassword", "123456");
        initParams.put("allow", "");//默认就是允许所有访问
        initParams.put("deny", "192.168.15.21");

        bean.setInitParameters(initParams);
        return bean;

    }


    //2、配置一个web监控的filter
    @Bean
    public FilterRegistrationBean webStatFilter() {
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());

        Map<String, String> initParams = new HashMap<>();
        initParams.put("exclusions", "*.js,*.css,/druid/*");

        bean.setInitParameters(initParams);

        bean.setUrlPatterns(Arrays.asList("/*"));

        return bean;
    }
}

 4.测试Druid是否可用

    运行程序

    在浏览器输入localhost:8080/druid 

    输入配置类DruidConfig里面的账号密码 admin 123456

druid可以正常访问

三.使用Mybatis配置文件版编写程序

1.在com.yl.mybatis包下新建三个类

   第一个类Employee

public class Employee {
    private Integer id;
    private String lastName;
    private Integer gender;
    private String email;
    private Integer dId;

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

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setdId(Integer dId) {
        this.dId = dId;
    }

    public Integer getId() {
        return id;
    }

    public String getLastName() {
        return lastName;
    }

    public Integer getGender() {
        return gender;
    }

    public String getEmail() {
        return email;
    }

    public Integer getdId() {
        return dId;
    }

 第二个类 EmployeeMapper

public interface EmployeeMapper  {

    public Employee getEmpById(Integer id);

    public void insertEmp(Employee employee);
}

第三个类DeptController 

@RestController
public class DeptController {
    @Autowired
    EmployeeMapper employeeMapper;

    @GetMapping("/emp/{id}")
    public Employee getEmp(@PathVariable("id") Integer id){
        return employeeMapper.getEmpById(id);
    }
}

2.在resource文件夹下新建mybatis文件夹,在mybatis文件夹下新建mapper文件夹

3.在mybatis文件夹下新建mybatis-config.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

</configuration>

3.在mapper文件夹下新建EmployeeMapper.xml

<?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.yl.mybatis.mapper.EmployeeMapper">
    <select id="getEmpById" resultType="com.yl.mybatis.bean.Employee">
        SELECT * FROM employee WHERE id=#{id}
    </select>
    
    <insert id="insertEmp">
        INSERT INTO employee(lastName,email,gender,d_id) VALUES (#{lastName},#{email},#{gender},#{dID})
    </insert>
</mapper>

4.将上述两个配置文件添加到全局配置文件中

 

mybatis:
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml

 四.对MySQL数据库的设置

新建一个名字为mybatis的数据库,创建一张名字为employee的表,随意写入一些数据 如下图

五.在本地进行测试

启动Spring boot项目

在浏览器输入 localhost:8080/emp/1 

成功返回数据

 六.将Spring Boot打成jar包部署到服务器

确保服务器上没有apache,nginx等服务器软件,因为spring boot项目自带了tomcat

1.打jar包

   双击maven自带的package插件

打包成功

 将jar包复制到桌面 重命名为mybatis.jar

2.将jar包部署到自己的centos服务器的home文件夹下

确保自己的服务器上有java jdk,推荐安装1.8版本

执行命令      java -jar mybatis.jar

成功运行jar包

在浏览器输入   服务器公网ip:8080/emp/1 

发现无法运行

解决方法:将8080端口改成80端口

在项目的application.yml文件中添加如下代码

server:
  port: 80

 重新将项目打成jar包部署到服务器运行

在浏览器输入 服务器公网ip/emp/1            (由于80端口是默认的,所以不用写端口号)

运行成功

源码自取:https://github.com/BIUBIUBIU-JIAZHOU/springboot-mybatis.git

发布了1 篇原创文章 · 获赞 0 · 访问量 277

猜你喜欢

转载自blog.csdn.net/m0_37640648/article/details/104327941