第一个SpringBoot程序

Spring Boot编写Hello World

因为内嵌Tomcat,所以可以直接运行GmspringbootApplication类。

创建UserInfoController类,在类中编写“Hello World”,网页中输出。

@ResponseBody的作用是将java对象转换为json格式的数据。
使用该注解后,数据直接被写入到输入流中,效果 等同于通过response对象输出指定格式的数据。

package com.zr.gmspringboot.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
public class UserInfoController {

    @RequestMapping("hello")
    @ResponseBody
    public String hello(){
        return "Hello World!";
    }
}

若出现如下错误:

只需在GmspringbootApplication类中的注解后添加如下代码即可:

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})

因为内嵌Tomcat,所以直接在浏览器的地址栏中输入设定的地址。

SpringBoot对表的增删操作

1.导包

将pom.xml中的配置文件全部替换为如下配置文件。
注意:德鲁伊数据池的版本号要为 1.1.10及以上。

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

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

<!--前端模板thymeleaf启动器-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<!--mybaits 启动器-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.1.1</version>
</dependency>
<!-- 数据库 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<!--德鲁伊数据池-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.10</version>
</dependency>
2.配置连接数据库

在 resources 下的application.properties文件中对数据库进行配置。

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/java?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

#与实体类和其他配置文件进行绑定
mybatis.type-aliases-package=com.zr.gmspringboot.pojo
mybatis.mapper-locations=classpath*:mapper/*.xml
3.dao层

在dao中创建接口,编写方法。
对数据库进行操作一共有两种方法:
1.通过在resources下的mapper.xml中编写sql语句。

2.通过注解直接编写sql语句。

package com.zr.gmspringboot.dao;

import com.zr.gmspringboot.pojo.UserInfo;
import org.apache.ibatis.annotations.Delete;

public interface IUserInfoDao {
//    模拟增加
    void addUser(UserInfo userInfo);
//    模拟删除
    @Delete("delete from tb_userinfo where id=#{id}")
    void delUser(Integer id);
}
4.service层

一般情况下,service层中的接口与dao层中接口一致。

package com.zr.gmspringboot.service;

import com.zr.gmspringboot.pojo.UserInfo;

public interface IUserInfoService {
    //    模拟增加
    void addUser(UserInfo userInfo);
    //    模拟删除
    void delUser(Integer id);
}

注意:serviceImpl中添加注解。

package com.zr.gmspringboot.service.impl;

import com.zr.gmspringboot.dao.IUserInfoDao;
import com.zr.gmspringboot.pojo.UserInfo;
import com.zr.gmspringboot.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private IUserInfoDao userInfoDao;
    @Override
    public void addUser(UserInfo userInfo) {
        userInfoDao.addUser(userInfo);
    }

    @Override
    public void delUser(Integer id) {
        userInfoDao.delUser(id);
    }
}
5.mapper.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.zr.gmspringboot.dao.IUserInfoDao">
    <insert id="addUser" parameterType="com.zr.gmspringboot.pojo.UserInfo">
        insert into tb_userinfo(id,username,password)values (#{id},#{username},#{password})
    </insert>
</mapper>
6.HTML

在 resources 下的 templates 文件夹中操作html。
在html的标签前加入下列代码。

<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

注意:这里的action为th:action,且路径不再是$,而是@。

th:action="@{/user/addUser}",user前记得加上/,否则报404错误。

<!DOCTYPE html>
<html lang="en">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>增加页面</title>
</head>
<body>
<center>
<form th:action="@{/user/addUser}" method="post">
    用户ID:<input type="text" id="id" name="id"/><br/>
    用户名:<input type="text" id="username" name="username"/><br/>
    用户密码:<input type="password" id="password" name="password"/><br/>
    <input type="submit" id="submit" name="submit" value="提交"/>
</form>
</center>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>删除页面</title>
</head>
<body>
<center>
<form th:action="@{/user/delUser}" method="post">
    请输入需要删除的用户ID<br/>
    <input type="text" id="id" name="id"/>
    <input type="submit" id="submit" name="submit" value="提交"/>
</form>
</center>
</body>
</html>
7.controller层

添加page方法用于跳转页面,这里不需要加@ResponseBody
通过page跳转的html来决定要使用的方法。

package com.zr.gmspringboot.controller;

import com.zr.gmspringboot.pojo.UserInfo;
import com.zr.gmspringboot.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
public class UserInfoController {

    @Autowired
    private IUserInfoService userInfoService;

    @RequestMapping("hello")
    @ResponseBody
    public String hello(){
        return "Hello World!";
    }

    @RequestMapping("page")
    public String page(){
        return "delUser";
    }


    @RequestMapping("addUser")
    @ResponseBody
    public String addUser(UserInfo userInfo){
        userInfoService.addUser(userInfo);
        return "增加成功!";
    }

    @RequestMapping("delUser")
    @ResponseBody
    public String delUser(Integer id){
        userInfoService.delUser(id);
        return "删除成功";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值