springboot整合常用组件和服务【3】springboot支持mybatis

本文详细介绍了如何在SpringBoot项目中整合Mybatis,包括环境配置、pom.xml修改、目录结构设置、配置文件调整、数据库表创建及启动测试等步骤,帮助开发者实现SpringBoot与Mybatis的无缝对接。
摘要由CSDN通过智能技术生成

1、环境约束

  • win10 64位操作系统
  • idea2018.1.5
  • maven-3.0.5
  • jdk-8u162-windows-x64
  • mysql6.5

2、前提约束

完成springboot创建web项目 https://www.jianshu.com/p/de979f53ad80
注意:笔者创建项目的时候约束的包前缀是net.wanho.springboot.springbootweb,读者可以自行创建包名,只是要注意本文中的代码也要修改包名

3、修改pom.xml

在dependencies标签中加入以下依赖:

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

4、在主入口类同等路径下分别创建controller、service、mapper、entity包

在entity中加入User.java

package net.wanho.springboot.springbootweb.entity;
public class User {
    private int id;
    private String name;

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public User() {
    }

    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;
    }
}

在mapper中加入UserMapper.java

package net.wanho.springboot.springbootweb.mapper;

import net.wanho.springboot.springbootweb.entity.User;

import java.util.List;

public interface UserMapper {
    List<User> query();
}

在service中加入UserService.java

package net.wanho.springboot.springbootweb.service;

import net.wanho.springboot.springbootweb.entity.User;
import net.wanho.springboot.springbootweb.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    public List<User> queryUsers()
    {
        return userMapper.query();
    }
}

在controller中加入UserController.java

package net.wanho.springboot.springbootweb.controller;

import net.wanho.springboot.springbootweb.entity.User;
import net.wanho.springboot.springbootweb.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/user/query")
    @ResponseBody
    public List<User> queryUsers() {
        return userService.queryUsers();
    }
}

5、在resources下创建一个mapper文件夹

在该文件下加入UserMapper.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="net.wanho.springboot.springbootweb.mapper.UserMapper">
    <select id="query" resultType="net.wanho.springboot.springbootweb.entity.User">
        select * from t_user
    </select>
</mapper>

6、修改application.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
#笔者的数据库密码是zhangli,请读者根据自己数据库密码修改
spring.datasource.password=zhangli
mybatis.mapper-locations=classpath:mapper/*.xml

7、登录mysql数据库,创建表并完成初始化

执行以下sql脚本

create database test;
use test;
create table t_user(id int , name varchar(20));
insert into t_user(id,name) values(1,'ali');
insert into t_user(id,name) values(2,'zhangli');
insert into t_user(id,name) values(3,'xiaoli');

8、启动并测试

在浏览器中输入http://localhost:8080/user/query,回车查看结果。具体操作如下:

测试


至此,我们完成了springboot对mybatis的支持,并完成了测试。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值