springboot整合mybatis

springboot整合mybatis

项目创建
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

包结构

在这里插入图片描述

在pom.xml添加阿里连接池和

	   <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.21</version>
        </dependency>
   		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

application.properties

###springboot 整合mybatis的基础设置
#别名
mybatis.type-aliases-package=cn.hp.springboot04.domain
#驱动配置可省略,springboot可以自动识别
#spring.datasource.driver-class-name =com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot_mybatis
spring.datasource.username =root
spring.datasource.password =root
#如果不配置阿里的druid,会自动使用默认的数据源 (com.zaxxer.hikari.HikariDataSource)
spring.datasource.type =com.alibaba.druid.pool.DruidDataSource
#控制台显示SQL
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

Controller层


package cn.hp.springboot04.controller;

import cn.hp.springboot04.domain.User;
import cn.hp.springboot04.mapper.UserMapper;
import cn.hp.springboot04.service.UserService;
import cn.hp.springboot04.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired()
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/findall")
    public Object findAll() {
        List<User> list = userMapper.findAll();
        return JsonData.buildSuccess(list);
    }

    @GetMapping("/add")
    public Object add(User user) {
        return JsonData.buildSuccess(userService.add(user));
    }


    @PostMapping("/update")
    public Object update(User user) {
        System.out.println("更改信息");
        userService.update(user);
        return JsonData.buildSuccess();
    }

    @GetMapping("/del_by_id")
    public Object delete(Integer id) {
        userService.delete(id);
        return JsonData.buildSuccess();
    }

    @GetMapping("/find_by_Id")
    public Object findById(Integer id) {
        User user = userMapper.findById(id);
        return JsonData.buildSuccess(user);
    }

    @GetMapping("/trans")
    public Object testTrans(User user){
        userService.useTrans(user);
        return JsonData.buildSuccess();
    }

}

JsonDate工具类代码

Mapper


import cn.hp.springboot04.domain.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface UserMapper {

    /**
     * 查询用户信息
     * @return
     */
    @Select("select * from user")
    @Results(@Result(property = "createTime",column = "create_time"))
    public List<User> findAll();

    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */
    @Select("select * from user where id =#{id}")
    @Results(@Result(property = "createTime",column = "create_time"))
    User findById(Integer id);

    /**
     * 根据id更新用户
     * @param user
     */
    @Update("update user set name = #{name} where id = #{id}")
    void update(User user);

    /**
     * 根据id删除信息
     * @param id
     */
    @Delete("delete from user where id =#{id}")
    void delete(Integer id);

    /**
     * 添加用户
     * @param user
     */
    @Insert("insert into user(name,age,phone,create_time) values(#{name},#{age},#{phone},#{createTime})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    int add(User user);

}

Service层


import cn.hp.springboot04.domain.User;

import java.util.List;

public interface UserService {


    /**
     * 查询用户信息
     * @return
     */
    public List<User> findAll();

    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */
    User findById(Integer id);

    /**
     * 根据id更新用户
     * @param user
     */
    void update(User user);

    /**
     * 根据id删除信息
     * @param id
     */
    void delete(Integer id);

    /**
     * 添加用户
     * @param user
     */
    int add(User user);
}

ServiceImpl


import cn.hp.springboot04.domain.User;
import cn.hp.springboot04.mapper.UserMapper;
import cn.hp.springboot04.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImp implements UserService {

    @Autowired
    private UserMapper userMapper;

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

    @Override
    public User findById(Integer id) {
        return userMapper.findById(id);
    }

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

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

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

User


import java.util.Date;

public class User {

    private  Integer id;
    private String name;
    private String phone;
    private Integer age;
    private Date createTime;

    public User(Integer id, String name, String phone, Integer age, Date createTime) {
        this.id = id;
        this.name = name;
        this.phone = phone;
        this.age = age;
        this.createTime = createTime;
    }

    public User() {
    }

    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 getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Integer getAge() {
        return age;
    }

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

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}

最后在Springboot_mybatisApplication添加@MapperScan(basePackages{“cn.hp.springboot_mybatis.mapper”})注解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值