使用spring-boot部署自己的第一个web项目

构建项目

1、访问 http://start.spring.io/

2、获取一个项目压缩包,导入IDE中

pom文件

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</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-web</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!--mabatis所需依赖-->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.1.1</version>
    </dependency>
    <!-- 跳转到templates目录下的页面,同时可以使用thymeleaf模板 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <!-- 开启热部署 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <!-- 测试true,false,不加都可以;但是其他人都加上了,我也就加上吧 -->
        <optional>true</optional>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

开启热部署:

在 pom 中直接引入依赖
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
</dependency>
设置以下两项(第一项如已设置直接设置第二项)

- IDEA设置
1) "File" -> "Settings" -> "Build,Execution,Deplyment" -> "Compiler",勾选 "Build project automatically" 。

2) 组合键:"Shift+Ctrl+Alt+/" ,选择 "Registry" ,勾选 "compiler.automake.allow.when.app.running"

- Eclipse设置
勾选 eclipse –> Project –> Build Automatically

配置文件application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/Demo?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.database=mysql
# 关闭thymeleaf缓存,在项目启动时支持页面修改,但是等待时间比较久,如果小项目的话,可能还没有重启项目快
spring.thymeleaf.cache=false
  • 这里需要有一个mysql的数据库,结构参考实体类,存入一条数据,供接下来使用

实体类


public class Score {

    private Integer id;
    private String name;
    private Integer language;
    private Integer math;
    
    get、set ……
}

    

Dao


// @Mapper //标记这是一个mapper类,如果在这里配置了那么启动类上就不需要配置@MapperScan注解
@Repository
public interface ScoreDao {

    @Select("select * from score where id=#{id}")
    Score queryScoreOne(@Param("id") Integer id);
    
}

Controller

@Controller
public class ScoreController {

    @RequestMapping("/index")
    public String getIndex(Model model){
        // 查询刚刚存入数据库的数据
        Score score = scoreDao.queryScoreOne(1);
        model.addAttribute("score",score);
        return "index";
        
    }

启动项目文件

@SpringBootApplication
@EnableAutoConfiguration
@MapperScan("com.example.dao") //标记dao层;如果不想在这里配置,那么可以在dao层的每个接口上添加@Mapper注解,二选其一
public class ScoreApplication {

    public static void main(String[] args) {
        SpringApplication.run(SsmdemoApplication.class, args);
        
    }
}

HTML页面

页面在resources下的templates文件夹下面,如果没有就建立一个同名(文件夹名为templates)

index.html

    <table>
        <tr>
            <th>学号</th>
            <th>姓名</th>
            <th>数学</th>
            <th>语文</th>
        </tr>
        <tr>
            <!-- 以下注释是为了让IDEA不报显示红线,不写也可以 -->
            <!--/*@thymesVar id="score" type="com.example.pojo.Score"*/-->
            <td th:text="${score.getId()}"></td>
            <td th:text="${score.getName()}"></td>
            <td th:text="${score.getLanguage()}"></td>
            <td th:text="${score.getMath()}"></td>
        </tr>
    </table>

启动项目

运行ScoreApplication.java,然后访问localhost:8080/index,就可以看到所查出来的数据了

评论

暂无评论