Spring Boot整和MyBatis!!!

目标:

  • 实现添加功能
  • 实现查询功能
  • 实现删除功能
  • 实现修改功能
  • 添加日期转换器

 1.搭建项目

        1.1pom文件:


        org.springframework.boot
        spring-boot-starter-parent
        2.3.2.RELEASE
         
    

   
    
        8
        8
        UTF-8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.0.1
        
        
            mysql
            mysql-connector-java
            5.1.47
        
        
            com.alibaba
            druid
            1.2.19
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
    
    
        
        
            
                src/main/java
                
                    **/*.xml
                
            
            
                src/main/resources
            
        
    

        1.2链接数据库application.yml:

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3305/springboot
    username: root
    password:
    type: com.alibaba.druid.pool.DruidDataSource
logging:
  level:
    com:
      by:
        mapper: DEBUG

        1.3构建启动类:SpringbootMybatisApplication

@SpringBootApplication
@MapperScan("com.by.mapper")
public class SpringbootMybatisApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisApplication.class, args);
    }
}

2.添加功能实现:

        2.1add_user.html:




    
  添加用户


新增用户


姓名:
密码:
性别:
生日:

        2.2对应实现controller,service,mapper层代码

                2.2.1controller层代码:

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.controller;

import com.by.pojo.User;
import com.by.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 

Project: springboot - UserController

*

Powered by scl On 2024-01-15 16:25:51

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ @Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService; /** * 添加 * * @param user * @return */ @RequestMapping("/addUser") public String addUser(User user) { userService.addUser(user); return "redirect:/user/selectUser"; } }

                2.2.2实现service层代码:

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.service;

import com.by.pojo.User;

import java.util.List;


public interface UserService{
    void addUser(User user);

}

  实现类:

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public void addUser(User user) {
        userMapper.addUser(user);
    }
}

                2.2.3实现mapper层代码:

package com.by.mapper;

import com.by.pojo.User;

import java.util.List;

/**
 * 

Project: springboot - UserMapper

*

Powered by scl On 2024-01-15 14:51:39

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ public interface UserMapper { void addUser(User user); }

映射文件:





    
        insert into user(name,sex,pwd,birth) values(#{name},#{sex},#{pwd},#{birth})
    

3.查询功能实现

        3.1select_user.html




    
  首页
  

  


欢迎光临!
添加
id 姓名 密码 性别 生日 操作
删除 修改

        3.2实现对应的controller、service、mapper层代码

                3.2.1controller层代码

/**
     * 查询所有用户信息
     *
     * @param model
     * @return
     */
    @RequestMapping("/selectUser")
    public String selectUser(Model model) {
        List userList = userService.selectUser();
        model.addAttribute("userList", userList);
        return "select_user";
    }

                3.2.2service层代码

 List selectUser();
 @Override
    public List selectUser() {
        return userMapper.selectUser();
    }

                3.2.3mapper层代码

List selectUser();
    

 

4.删除功能实现

          4.1实现对应的controller、service、mapper层代码

            4.1.1controller层代码

/**
     * 根据id删除用户
     *
     * @param id
     * @return
     */
    @GetMapping("/deleterUserById/{id}")
    public String deleterUserById(@PathVariable Integer id) {
        userService.deleterUserById(id);
        return "redirect:/user/selectUser";
    }

            4.1.2service层代码

    void deleterUserById(Integer id);
 @Override
    public void deleterUserById(Integer id) {
        userMapper.deleterUserById(id);
    }

            4.1.3mapper层代码

void deleterUserById(Integer id);
 
        delete from user where id =#{id}
    

5.修改功能实现

          5.1update_user.html




    
    添加用户


新增用户


姓名:
密码:
性别:
生日:

          5.2实现对应的controller、service、mapper层代码

                    5.2.1controller层代码

 /**
     * 数据回显
     *
     * @param id
     * @return
     */
    @GetMapping("/getUserById/{id}")
    public String getUserById(@PathVariable Integer id, Model model) {
        User user = userService.getUserById(id);
        model.addAttribute("user", user);
        return "update_user";
    }

    /**
     * 修改
     *
     * @param
     * @return
     */
    @PostMapping("/updateUserById")
    public String updateUserById(User user) {
        userService.updateUserById(user);
        return "redirect:/user/selectUser";
    }

                    5.2.2service层代码


    User getUserById(Integer id);

    void updateUserById(User user);
 @Override
    public User getUserById(Integer id) {
        return userMapper.getUserById(id);
    }

    @Override
    public void updateUserById(User user) {
        userMapper.updateUserById(user);
    }

                    5.2.3mapper层代码

 User getUserById(Integer id);

    void updateUserById(User user
 


        update user
        
            
                name=#{name},
            
            
                sex=#{sex},
            
            
                pwd=#{pwd},
            
            
                birth=#{birth},
            
        
        where id =#{id}
    

 6.添加日期转换器

DateConverter:(日期转换器)

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.converter;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 

Project: springboot - DateConverter

*

Powered by scl On 2024-01-15 17:09:15

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ /** * 日期转换器 */ @Component public class DateConverter implements Converter { @Override public Date convert(String source) { try { DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); return format.parse(source); } catch (Exception e) { e.printStackTrace(); } return null; } }

WebmvcConfig:(将日期转换器装到容器中)

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.config;

import com.by.converter.DateConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.FormatterRegistry;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 

Project: springboot - WebmvcConfig

*

Powered by scl On 2024-01-15 17:17:46

*

描述:

* * @author 孙臣龙 [[email protected]] * @version 1.0 * @since 17 */ /** * 将日期转换器注入到容器中 */ @Component public class WebmvcConfig implements WebMvcConfigurer { @Autowired private DateConverter dateConverter; @Override public void addFormatters(FormatterRegistry registry) { registry.addConverter(dateConverter); } }

 

你可能感兴趣的:(spring,boot,mybatis,java)