栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot学习笔记(二)——整合Mybatis并实现用户查询

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot学习笔记(二)——整合Mybatis并实现用户查询

springboot整合Mybatis实现用户查询 1、添加jar包

	org.mybatis.spring.boot
	mybatis-spring-boot-starter
	2.2.2


	mysql
	mysql-connector-java
2、配置数据源和Mapper.xml文件位置
# application-dev.properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/login?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root123
mybatis.mapper-locations:classpath:mapping/*.xml

3、创建目录结构如下

4、编写代码实现getUserById 4.1 编写实体类User
package com.galago.demo_web.entry;

public class User {
    private Integer id;
    private String username;
    private String password;

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
4.2 编写UserMapper接口——dao层(数据持久层)
package com.galago.demo_web.mapper;

import com.galago.demo_web.entry.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

@Mapper // 该注解可以帮助mybatis找到mapper接口,生成动态代理类。或者在启动文件配置@MapperScan
public interface UserMapper {

    User getUser(Integer id);

}
4.3 编写UserMapper.xml


 
    
        
        
        
        
    

    
        select * from t_user where id = #{id}
    

注意:resources/mapping目录下的xml文件名需和Mapper接口名一致

4.4 编写UserService接口——Service层

package com.galago.demo_web.services;

import com.galago.demo_web.entry.User;
import com.galago.demo_web.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

public interface UserService {
    User getUser(Integer id);
}

4.5 编写UserServiceImpl实现类——service层

package com.galago.demo_web.impl;

import com.galago.demo_web.entry.User;
import com.galago.demo_web.mapper.UserMapper;
import com.galago.demo_web.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

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

4.6 编写UserController类——Controller层

package com.galago.demo_web.controller;

import com.galago.demo_web.entry.User;
import com.galago.demo_web.impl.UserserviceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/user") // 此处表示该类中所有相应请求的方法都是以该地址作为父地址, /user/selectone
@Controller
public class UserController {

    @Autowired
    UserserviceImpl userserviceImpl;

    @RequestMapping("/selectone")
    @ResponseBody // 返回值转为json数据
    public User getUser(Integer id) {
        System.out.println(id);
        return  userserviceImpl.getUser(id);
    }
}
5、测试:

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/821943.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号