001 简单的springboot整合mybatis

首先新建一个springboot项目在这里插入图片描述

在这里插入图片描述
右边可以看到挑选的依赖

  ### 配置application.yml

在这里插入图片描述

 server:
 port: 8080

spring:
 datasource:
   username: root
   password: XXX
   url: jdbc:mysql://XXX:3306/books?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai
   driver-class-name: com.mysql.jdbc.Driver
mybatis:
 mapper-locations: classpath:mapping/*Mapper.xml 
 type-aliases-package: com.example.demo.entity
#showSql
logging:
 level:
   com:
     example:
       mapper : debug

然后分别建

controller entity mapper service 包,以及mapping文件夹
项目结构
在这里插入图片描述

代码

controller
package com.example.demo.controller;

import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/")
public class UserController {

 @Autowired
 private UserService userService;

 @RequestMapping("/{id}")
 public String GetUser(@PathVariable int id){
     return userService.Sel(id).toString();
 }
}
entity
package com.example.demo.entity;

    public class User {
        private String id;
        private String userId;
        private String roleId;

        public String getId() {
            return id;
        }

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

        public String getUserId() {
            return userId;
        }

        public void setUserId(String userId) {
            this.userId = userId;
        }


        public String getRoleId() {
            return roleId;
        }

        public void setRoleId(String roleId) {
            this.roleId = roleId;
        }
        @Override
        public String toString() {
            return "User{" +
                    "id='" + id + '\'' +
                    ", userId='" + userId + '\'' +
                    ", roleId='" + roleId + '\'' +
                    '}';
        }
}
mapper
package com.example.demo.mapper;

import com.example.demo.entity.User;
import org.springframework.stereotype.Repository;

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



service
package com.example.demo.service;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    UserMapper userMapper;
    public User Sel(int id){
        User a= userMapper.Sel(id);
        return a;
    }

}

application.run

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.example.demo")
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

mapping
<?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.example.demo.mapper.UserMapper">

    <select id="Sel" resultType="com.example.demo.entity.User">
        select id ,
               user_id userId,
               role_id roleId
               from sys_user_role where id = #{id}
    </select>
</mapper>

启动成功

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值