SpringBoot整合Mybatis

该文章展示了如何使用SpringBoot集成MyBatis进行数据库操作,包括配置Druid数据源、MySQL连接、Lombok注解简化对象,以及Thymeleaf模板引擎。同时,文章还详细说明了如何利用PageHelper实现分页查询,并通过RESTfulAPI设计实现了用户管理的增删改查功能。
摘要由CSDN通过智能技术生成

pom.xml

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.20</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.4</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
         <!--添加分页插件-->
                 <dependency>
                     <groupId>com.github.pagehelper</groupId>
                     <artifactId>pagehelper-spring-boot-starter</artifactId>
                     <version>1.3.0</version>
                 </dependency>

applicati.yml

server:
  port: 8888

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/user?serverTimezone=GMT
    username: root
    password: root123

mybatis:
  type-aliases-package: com.itheima.pojo #别名
  mapper-locations: classpath:mapper/*.xml

实体类pojo

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User {
    private Integer id;
    private String username;
    private String password;
    private String gender;
    private String addr;
}

mapper数据层

import com.itheima.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;

@Mapper
public interface UserMapper {

    public List<User> getAll();

    public List<User> getById(@Param("id") Integer id);

    public int deleteById(Integer id);

    public int updateUser(User user);

    public int insertUser(User user);


}

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.itheima.mapper.UserMapper">
    <insert id="insertUser">
        insert into user (username,password,gender,addr)
        VALUES (#{username},#{password},#{gender},#{addr})
    </insert>

    <update id="updateUser">
        update user 
        set username=#{username},password=#{password},gender=#{gender},addr=#{addr}
        where id =#{id}
    </update>

    <delete id="deleteById">
        delete from user where id = #{id}
    </delete>


    <select id="getAll" resultType="com.itheima.pojo.User">
         select * from user
    </select>
    <select id="getById" resultType="com.itheima.pojo.User">
         select * from user
         where id = #{id}
    </select>
</mapper>

service业务层接口

import com.itheima.pojo.User;
import java.util.List;

public interface UserService {

    public List<User> getAll();

    public List<User> getById(Integer id);

    public int deleteById(Integer id);

    public int updateUser(User user);

    public int insertUser(User user);
}

serviceIpml业务层实现类

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> getAll() {
        return userMapper.getAll();
    }

    @Override
    public List<User> getById(Integer id) {
        return userMapper.getById(id);
    }

    @Override
    public int deleteById(Integer id) {
        return userMapper.deleteById(id);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public int insertUser(User user) {
        return userMapper.insertUser(user);
    }
}

controller控制层

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping
    public PageInfo getAll(@RequestParam(value = "categoryId",required = false) Integer categoryId,
                             @RequestParam(value = "currentPageNo", defaultValue = "1", required = false) Integer currentPageNo,
                             @RequestParam(value = "pageSize", defaultValue = "3", required = false) Integer pageSize) {

        PageHelper.startPage(currentPageNo, pageSize);
        List<User> list = userService.getAll();
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @GetMapping("/{id}")
    public List<User> getById(@PathVariable Integer id) {
        return userService.getById(id);
    }
    @DeleteMapping("/{id}")
    public int deleteById(@PathVariable Integer id) {
        return userService.deleteById(id);
    }
    @PutMapping
    public int updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }
    @PostMapping
    public int insertUser(@RequestBody User user) {
        return userService.insertUser(user);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值