Spring boot整合Mybatis

1.需要的starter:

微信截图_20191020205723.png

2.需要的 配置

# DB 配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.password=root
spring.datasource.username=root
# 必须配置时区,不然报错
spring.datasource.url=jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC

# 配置扫描实体类
mybatis.type-aliases-package=com.itheima.domain
# 配置扫描UserMapper.xml
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
#工程启动端口
server.port=9090
# redis 配置
#spring.redis.host=localhost
#spring.redis.port=6379

3.dao层

/**
 * @author :gzy
 * @date :Created in 2019/10/20
 * @description :
 * @version: 1.0
 */
@Repository
@Mapper//表明当前接口是一个Mapper,被Mybatis框架扫描
public interface UserDao {
    List<User> findall();
}

注意:我这里用的## Free Mybatis plugin插件生成的mapper.xml文件。

微信截图_20191020210423.png

生成的mapper.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="com.guo.lianxi1020.dao.UserDao">
    <select id="findall" resultType="com.guo.lianxi1020.domain.User">
        select * from users
    </select>
</mapper>

3.server

@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
    @Override
    public List<User> findall() {
        return userDao.findall();
    }
}

4.control

package com.guo.lianxi1020.control;

import com.guo.lianxi1020.domain.User;
import com.guo.lianxi1020.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author :gzy
 * @date :Created in 2019/10/20
 * @description :
 * @version: 1.0
 */
@RestController
@RequestMapping("user")
public class UserControl {
    @Autowired
    private UserServiceImpl userService;
    @RequestMapping("all")
    public List<User> showAll(){
        return userService.findall();
    }
}
Snipaste_2019-08-02_15-59-15.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值