springboot

SpringBoot:
目的:
简化新Spring应用的初始搭建以及开发过程。

为什么要使用SpringBoot:
1.不需要过多繁琐的配置文件。
(抛除了ApplicationContext.xml、SpringMVC.xml、Web.xml)

2.内嵌了Tomcat版本9.0。

3.尽可能的全自动配置Spring容器。

SpringBoot:
目的:
为什么要使用SpringBoot:
创建一个SpringBoot
Spring Boot编写Hello World
SpringBoot对表的增删操作
1.导包
2.配置连接数据库
3.dao层
4.service层
5.mapper.xml
6.HTML
7.controller层

创建一个SpringBoot
在Idea上创建一个新的项目,选择 Spring Initializr;

注意:这里的SDK必须选择 1.8及以上 版本。

输入项目名 Group 和 Artifact。

命名规则 :不可以有大写字母

一般的Spring Boot项目,勾选:

Web -—— Spring Web

Template Engines —— Thymeleaf

SQL —— JDBC API
—— MyBatis Framework
—— MySQL Driver

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})
1
因为内嵌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" ?> insert into tb_userinfo(id,username,password)values (#{id},#{username},#{password})

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

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

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

增加页面
用户ID:
用户名:
用户密码:
删除页面
请输入需要删除的用户ID

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值