资讯

展开

springboot的配置以及增删改查的实现

作者:快盘下载 人气:

springboot一个人担任 springmvc和spring的任务

springboot内部集成tomcat就不需要自己配置tomcat

目录

springboot的配置以及增删改查的实现

一、创建项目;idea上;

;1;首先创建新项目

;2;点击下一步

springboot

二、配置文件

;1;pom.xml文件

;下面有swagger的依赖配置;

<?xml version=;1.0; encoding=;UTF-8;?>
<project xmlns=;http://maven.apache.org/POM/4.0.0; xmlns:xsi=;http://www.w3.org/2001/XMLSchema-instance;
         xsi:schemaLocation=;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd;>
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springFramework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.qcby</groupId>
    <artifactId>SpringBootDemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringBootDemo</name>
    <description>SpringBootDemo</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>


        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.22</version>
        </dependency>
        <!--  图像化依赖 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!--引入swagger2依赖 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

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

;2;配置application.yml文件;一开始可能没有;是propretis;可以改这个文件或者创建新的;

;含swagger配置;

Spring:
  thymeleaf:
    cache: false #关闭页面缓存
    encoding: UTF-8 #模板编码
    suffix: .html #返回页面的后缀
    mode: HTML5 #模板样式
    prefix: classpath:/templates/ #页面映射路径
  mvc:
    static-path-pattern: /static/**
  datasource:
    url: jdbc:mysql://localhost:3306/PingJiao?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false
    username: root
    password: 2020
    driver-class-name: com.mysql.cj.jdbc.Driver

    #上传文件使用
  servlet:
    multipart:
    #单个文件最大上传大小
    max-file-size: 10MB
    #每次请求上传文件大小最大值
    max-request-size: 30MB
mybatis:
  mapper-locations: classpath:mapper/*.xml  #对应mapper映射xml文件所在路径
  type-aliases-package: com.qcby.springboot.model  #对应实体类路径

#自定义参数
define:
  nginx:
    path: D:uploadFile

;3;修改启动类;把dao层地址填上;

springboot

到此配置结束;可以正常去写代码了。

三、使用(增删改查;

**

注意;下面代码;注释千万别忘了加;好多次错误都是忘了注释

**

;Controller
 ;RequestMapping(;/delete;)
 ;ResponseBody//导入json数据,让它显示的都是html

;1;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.qcby.springboot.dao.UserDao;>

    <select id=;findAll; resultType=;com.qcby.springboot.model.User; >
        select * from demo
    </select>
    <insert id=;insert; parameterType=;com.qcby.springboot.model.User;>
        insert into demo(username,password)
        values (#{username},#{password})
    </insert>
    <delete id=;delete; parameterType=;java.lang.Integer;>
        delete from demo where id = #{id};
    </delete>
    <update id=;update; parameterType=;com.qcby.springboot.model.User;>
        update demo
        <set>
            <if test=;username != null and username !=;;;>
                username = #{username},
            </if>
            <if test=;password != null and password !=;;;>
                password = #{password},
            </if>
        </set>
        where id = #{id}
    </update>
</mapper>
;2;dao层和service层;都一样内容这里面;


public interface UserDao {
    List<User> findAll();
    public int insert(User user);
    public int delete(Integer id);
    public int update(User user);
}

;3;实现IMPL里的接口方法

;Service
public class UserServiceImpl implements UserService {
    ;Autowired
    UserDao userDao;
    ;Override
    public List<User> findAll() {
        return this.userDao.findAll();
    }
    ;Override
    public int insert(User user) {
        return this.userDao.insert(user);
    }
    ;Override
    public int delete(Integer id) {
        return this.userDao.delete(id);
    }
    ;Override
    public int update(User user) {
        return this.userDao.update(user);
    }
}

;4;controller层的实现

;Controller
;RequestMapping(;/demo;)
public class UserController {
    ;RequestMapping(;/hello;)
    public String  heloo(){
        return ;index;;
    }//这里是在templates有个index.html;直接跳转到页面去
    ;Autowired
    UserService userService;
    ;RequestMapping(;/user;)
    ;ResponseBody
    public  List<User> findAll(){
        List<User> users=userService.findAll();
        return users;
    }
    ;RequestMapping(;/insert;)
    ;ResponseBody//导入json数据
    public String insert(User user){
        int code=userService.insert(user);
        String data=;;;
        if(code==1){
            data=;{;code;:;200;,;message;:;success;};;
        }else{
            data=;{;code;:;500;,;message;:;fail;};;
        }
        return data;
    }
    ;RequestMapping(;/delete;)
    ;ResponseBody//导入json数据,让它显示的都是html
    public String delete(Integer id){
        int code= userService.delete(id);
        String data=;;;
        if(code==1){
            data=;{;code;:;200;,;message;:;success;};;
        }else{
            data=;{;code;:;500;,;message;:;fail;};;
        }
        return data;
    }
    ;RequestMapping(;/update;)
    ;ResponseBody//导入json数据,让它显示的都是html
    public String update(User user){
        int code = userService.update(user);
        String data = ;;;
        if(code == 1 ){
            data = ;{;code;:;200;,;message;:;success;};;
        }else {
            data = ;{;code;:;999;,;message;:;error;};;
        }
        return data;
    }
}

;5;最后;model层忘了说了;应该先写model的;里面是你数据库里的字段定义; 我的数据库里面表名demo;里面字段id;username;password。

public class User {
    private Integer id;
    private String username;
    private  Integer password;

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getPassword() {
        return password;
    }

    public void setPassword(Integer password) {
        this.password = password;
    }

      ;Override
    public String toString() {
        return ;User{; ;
                ;id=; ; id ;
                ;, username=;; ; username ; ;;; ;
                ;, password=; ; password ;
                ;};;
    }
}

OK;这样直接去浏览器搜索就可以了;会显示success就成功了springboot

加载全部内容

相关教程
猜你喜欢
用户评论
快盘暂不提供评论功能!