springboot入门(1)---整合MyBatis

springboot入门(1)—整合MyBatis

1、快速创建springboot项目
1.1、选择spring initializr,默认的为外网访问,选择customer,用阿里的服务地址:
https://start.aliyun.com/在这里插入图片描述
1.2、next,选择web。
2、整合MyBatis
2.1、添加MyBatis起步依赖:

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.1.1</version>
</dependency>

2.2、添加mysql驱动:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

2.3添加数据库连接信息

mybatis.type-aliases-package=com.zdww.domain
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/bk?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=1405284875

3、代码的编写
3.1、domain层

package com.zdww.domain;

/**
 * @ClassName:User
 * @Auther: crazy
 * @Date: 2020/8/1 14:29
 * @Description: TODO
 * @Version:1.0
 */
public class User {
    private String uname;
    private String  mail;
    private String account;
    private String role;

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getmail() {
        return mail;
    }

    public void setmail(String mail) {
        this.mail = mail;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    @Override
    public String toString() {
        return "User{" +
                "uname='" + uname + '\'' +
                ", mail='" + mail + '\'' +
                ", account='" + account + '\'' +
                ", role='" + role + '\'' +
                '}';
    }
}

3.2、mapper层

package com.zdww.mapper;

import com.zdww.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @ClassName:UserMapper
 * @Auther: crazy
 * @Date: 2020/8/1 14:31
 * @Description: TODO
 * @Version:1.0
 */
@Mapper
public interface UserMapper {
    @Select("select * from tuser")
    public List<User> queryUserList();
}

3.3、ctroller层

package com.zdww.ctroller;

import com.zdww.domain.User;
import com.zdww.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @ClassName:MapperController
 * @Auther: crazy
 * @Date: 2020/8/1 14:35
 * @Description: TODO
 * @Version:1.0
 */
@CrossOrigin
@Controller
public class MapperController {

    @Autowired
    private

## UserMapper userMapper;

    @RequestMapping("/queryUser")
    @ResponseBody
    public List<User> queryUser() {
        List<User> users = userMapper.queryUserList();
        return users;
    }

}

4、调试运行
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值