栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【Springboot+模板引擎+mybatis】

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Springboot+模板引擎+mybatis】

文章目录
  • 前言
  • 一、Springboot+模板引擎+mybatis
  • 二、使用步骤
    • 1.导入依赖
  • 总结




前言

提示:喜欢就点个赞哦


提示:以下是本篇文章正文内容,下面案例可供参考



一、springboot+模板引擎+mysql+整合mybatis

示例:为了方便



二、使用步骤



1.导入依赖

代码如下(示例):pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.6
         
    
    com.oyjl
    spring-boot-name
    0.0.1-SNAPSHOT
    spring-boot-name
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        

        
            org.springframework.boot
            spring-boot-starter-web
        
        
            com.alibaba
            druid
            1.2.6
        


        
            mysql
            mysql-connector-java
        
        
            org.projectlombok
            lombok
            true
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.0
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.junit.platform
            junit-platform-commons
        

        
            org.apache.commons
            commons-lang3
            3.6
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    

 编写实体类

package com.oyjl.pojo;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)  //链式编程
public class Users {
    private Integer id;
    private String name;
    private String password;
    private Integer age;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

 Mapper接口

package com.oyjl.Mapper;


import com.oyjl.pojo.Users;
import org.apache.ibatis.annotations.Mapper;


import java.util.List;

@SuppressWarnings("all")
@Mapper
public interface UsersMapper {
        List finAll();

        Users findUserById(int id);

        int addUser(Users user);

        int update(Users user);

        int delete(int id);


}

 Svrivce接口

package com.oyjl.Service;

import com.oyjl.pojo.Users;

import java.util.List;

public interface UsersService {
    List finAll();

    Users findUserById(int id);

    int addUser(Users user);

    int update(Users user);

    int delete(int id);
}

 Svrivceimpl类

package com.oyjl.Service.impl;

import com.oyjl.Mapper.UsersMapper;
import com.oyjl.Service.UsersService;
import com.oyjl.pojo.Users;
import jdk.internal.instrumentation.Logger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.util.List;

@SuppressWarnings("all")
@Slf4j
@Service
public class UsersServiceimpl implements UsersService {
    @Autowired
    public UsersMapper usersMapper;

    @Override
    public List finAll() {
        List users = usersMapper.finAll();
        return users;
    }

    @Override
    public Users findUserById(int id) {
        return usersMapper.findUserById(id);
    }

    @Override
    public int addUser(Users user) {
        return  usersMapper.addUser(user);
    }

    @Override
    public int update(Users user) {
        return usersMapper.update(user);
    }

    @Override
    public int delete(int id) {
        return usersMapper.delete(id);
    }
}
 

 mybatis.xml




    
    insert into table_user(name,password,age)
        values (#{name}, #{password}, #{age})
    
    
    update table_user
        set name = #{name},password = #{password},age = #{age}
        where id = #{id}
    

    
    delete from table_user where id=#{id}
    

    
        select * from table_user where id=#{id}
    
application.yml
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1/itveteran?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource
mybatis:
  type-aliases-package: com.oyjl.pojo
  mapper-locations: classpath:mybatis/mapper/*.xml

 controller类

package com.oyjl.conrotller;

import com.oyjl.Service.UsersService;
import com.oyjl.pojo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;
import javax.xml.transform.Result;
import java.util.List;
import java.util.Map;

@SuppressWarnings("all")
@Controller
public class Userscontroller {
    @Autowired
    public UsersService usersService;
    @RequestMapping("/allout")
    public String finAll(Model model){

        List list = usersService.finAll();

        model.addAttribute("list",list);
        return "/indexs.html";
    }


//添加
    @RequestMapping("/toAdd")
    public String toAddPaper() {
        return "adduser";
    }

    @RequestMapping("/add")
    public String addPaper(Users user) {
        System.out.println(user);
        usersService.addUser(user);
        return "redirect:/allout";
    }

//修改

    @RequestMapping("/toEdit")
    public String toEdit(Model model,int id) {
        Users users=usersService.findUserById(id);
        model.addAttribute("user", users);
        return "updateBook";
    }


        @RequestMapping("/edit")
        public String edit(Users user) {
            usersService.update(user);
            return "redirect:/allout";
        }


    //删除
    @RequestMapping("/delete")
    public String delete(int id) {
        usersService.delete(id);

        return "redirect:/allout";
    }
}

 查询全部页面



    书籍列表
    

    
    





    
        
            
                
                    用户列表
                
            
        
    
    
        
            新增
        
    
    
        
            
用户ID 用户名 密码 年龄 操作
更改 | 删除

修改页面

>

    修改信息
    

    
    





    
        
            
                
                    修改信息
                
            
        
    
    







      返回

新增页面



    修改信息
    

    
    





    
        
            
                
                    添加用户
                
            
        
    
    







      返回

页面就不一一发出来了



2.项目结构




总结

这个要多练,加油吧

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/642754.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号