码迷,mamicode.com
首页 > 编程语言 > 详细

springboot整合mybatis

时间:2020-12-28 11:33:34      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:扫描   classpath   print   配置数据源   l数据库   return   bat   time   load   

1.创建mysql数据库,并添加数据:

技术图片

 

 

 技术图片

 

 

创建数据库访问层,实体类,业务层,控制层:

数据库访问层:

技术图片

 

 

 代码:

package com.xys.springnew.mapper;

import com.xys.springnew.entity.User;
import org.springframework.stereotype.Repository;

@Repository
public interface UserMapper {
    User getById(int id);
}

技术图片

 

 代码:

<?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.xys.springnew.mapper.UserMapper">

    <select id="getById" resultType="com.xys.springnew.entity.User">
        SELECT
        *
        FROM
        t_user
        where id = #{id}
    </select>

</mapper>

实体类:

技术图片

 

 

 代码:

package com.xys.springnew.entity;

public class User {

    private int id;

    private String name;

    private String password;

    public int getId() {
        return id;
    }

    public void setId(int 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;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name=‘" + name + \‘ +
                ", password=‘" + password + \‘ +
                };
    }
}

业务层接口:

技术图片

 

 

 代码:

package com.xys.springnew.service;

import com.xys.springnew.entity.User;

public interface IUserService {
    public User getById(int id);
}

业务层实现类:

技术图片

 

 

 代码:

package com.xys.springnew.service.impl;

import com.xys.springnew.entity.User;
import com.xys.springnew.mapper.UserMapper;
import com.xys.springnew.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class IUserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User getById(int id) {
        return userMapper.getById(id);
    }
}

控制层:

技术图片

 

 代码:

package com.xys.springnew.controller;

import com.xys.springnew.entity.User;
import com.xys.springnew.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private IUserService iUserService;

    @GetMapping("{id}")
    public User getById(@PathVariable("id") int id) {
        System.out.println("id" + id);
        System.out.println("查询操作");
        return iUserService.getById(id);
    }

}

配置mybatis:

技术图片

 

 

 

 代码:

#配置数据源信息
  datasource:
    url: jdbc:mysql://localhost:3306/springboot_test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver

# 扫描mapper;配置显示执行的sql
mybatis:
  mapper-locations: classpath:/mapper/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

启动类中添加扫描mapper的注解:

技术图片

访问url:

技术图片

 

springboot整合mybatis

标签:扫描   classpath   print   配置数据源   l数据库   return   bat   time   load   

原文地址:https://www.cnblogs.com/xuyinshan/p/14172076.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!