菜鸟的成长之路——springboot部署威客项目

今天我们使用springboot技术来搭建一下威客的项目,springboot出现的目的就是简化spring的搭建和配置,他避免了各种各样的配置文件。springboot使用内置tomcat,而不需要我们安装tomcat,而且打成的jar包可以直接运行在java环境里。我们看一下今天要做哪些步骤吧:

  • 创建一个springboot项目
  • 将静态页面加入springboot项目中
  • 热加载springboot项目
  • springboot整合mybatis

创建springboot项目

我们创建一个maven项目。
pom.xml中需要导入包如下:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>1.5.9.RELEASE</version>
</dependency>

在maven项目中创建一个包“com.witkey”,在包里创建一个启动类WitkeyApplication,以后所有的包都要在这个包下面创建,因为启动类智能加载同级包内和子包内的类,代码如下:
WitkeyApplication.java

package com.witkey;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

//这个注解标准本类是springboot启动类
@SpringBootApplication
public class WitkeyApplication {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
//      通过本类启动springboot
        SpringApplication.run(WitkeyApplication.class, args);
    }

}

等上述操作做完以后,我们就可以测试我们的springboot是否搭建好了。我们再创建一个controller,直接使用@RestController标明这个类是一个controller,然后创建一个方法,并用@RequestMapping标注映射路径,控制器代码如下:
ControllerTest.java

package com.witkey;

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

@RestController
public class ControllerTest {

    @RequestMapping("/test.action")
    public String test() {
        return "create boot success.";
    }
}

我们在启动类运行,发现控制台打印如下,证明启动成功:
这里写图片描述

然后在网址访问路径http://localhost:8080/test.action,发现可以正常访问到我们创建的控制器,至此,我们的springboot项目创建成功。
但是我们观察一下我们访问的路径,和我们以前动态站项目路径相比,没有了项目的名字,要注意这点。


将静态页面加入springboot项目中

我们创建完springboot项目,再将静态页面添加到项目中,静态页面的下载路径:https://download.csdn.net/download/madman_donghui/10603033。我们在项目的resource包下创建static文件夹,springboot加载静态页面会从这个文件夹下查找,如果把静态页面放到webcontent中,是找不到的。
解压出所有的静态页面,全选复制到我们的resource/static/pages文件夹中,如下:
这里写图片描述
这样,我们再从浏览器中输入http://localhost:8080/pages/index.html,就可以看到我们的静态页面的主页如下:
这里写图片描述
至此,静态页面添加到项目完成。


热部署springboot项目

我们在使用springboot的时候,确实省去了很多配置文件的麻烦,但是我们开发过程中的效率并没有提高,我们每次修改代码,仍然需要重启启动类,才能继续测试。
springboot给我们提供了解决这个问题的功能,这就是热部署。
我们首先将如下配置导入到pom中:

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>1.5.9.RELEASE</version>
        </dependency>

这样,我们启动后,再修改代码,就不需要反复启动我们的springboot了。
但是寡人在这却一直失败,弄了一上午也没发现问题,在这写出来给大家做个参考。
我热部署弄好了之后,修改我已经创建好的controller,却怎么也不能自动重新启动,但是除了这个controller其他的文件都是可以的。
最后我决定不跟这个问题置气,选择了重新创建了一个controller,才将这个问题解决。我们暂且当做是eclipse在抽筋吧。


springboot整合mybaits

我们需要导入mybatis需要的依赖,我们在这里使用c3p0的连接池连接。导入依赖如下:
pom.xml


<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.38</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.1</version>
</dependency>

springboot中是没有xml配置文件的,我们需要将数据源配置到resource中的application.properties中,但是这个配置文件不是很简洁,我们将配置放到application.yml中。数据源配置如下:

spring:
 datasource:
  driverClassName: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost:3306/user
  username: root
  password: 

mybatis:
 mapper-locations: classpath:com/witkey/dao/*Mapper.xml

结合数据源,我们在数据库中创建user表,字段如下:
这里写图片描述
其中id是自动递增的主键。
然后我们为了将项目按照mvc模式开发,在默认包中创建dao、service、serviceImpl、controller、entity五个包。
我们在entity包中创建实体类User,代码如下:
User.java

package com.witkey.entity;

import org.springframework.stereotype.Component;

@Component
public class User {
    private String id;
    private String account;
    private String password;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

我们在dao包中创建一个测试接口TestMapper,代码如下:
TestMapper.java

package com.witkey.dao;

import org.apache.ibatis.annotations.Select;

import com.witkey.entity.User;
public interface TestMapper {
    @Select("select * from user where id = #{id}")
    public User getUser(String id);
}

再resource创建和dao包一样的路径,然后在包中创建一个测试xmlTestMapper,配置如下:
TestMapper.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.witkey.dao.TestMapper"></mapper>

在service包中创建业务逻辑接口TestService,代码如下:
TestService.java

package com.witkey.service;

import com.witkey.entity.User;

public interface TestService {
    public User getUser(String id);
}

在serviceImpl包中创建业务逻辑实现类TestServiceImpl,代码如下:
TestServiceImpl.java

package com.witkey.serviceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.witkey.dao.TestMapper;
import com.witkey.entity.User;
import com.witkey.service.TestService;

@Service
public class TestServiceImpl implements TestService{

    @Autowired
    private TestMapper testMapper;

    public User getUser(String id) {
        // TODO Auto-generated method stub
        return testMapper.getUser(id);
    }

}

在Controller包中创建我们的控制器类TestController,代码如下:
TestController.java

package com.witkey.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.witkey.entity.User;
import com.witkey.service.TestService;

@RestController
public class TestController {

    @Autowired
    public TestService testService;
    @RequestMapping("test.action")
    public String getUser() {
        return testService.getUser("1").getAccount();
    }
}

我们还需要在启动类的类级别加一个注解@MapperScan(“com.witkey.dao”),其中的参数是mapper接口所在包名。
我们访问http://localhost:8080/test.action,网页正常打印数据库中的用户account,至此,springboot整合mybatis成功。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值