SpringBoot整合Mybatis(注解方式)

SpringBoot整合Mybatis通常有两种方式,一种是以xml映射文件的,一种是以注解的方式实现的,这里主要是以注解的方式来实现

项目目录结构如下:

src
----main
--------java
------------com.example.app
----------------controller //控制器
--------------------UserController.java
----------------entity    //实体类
--------------------User.java
----------------mapper    //映射接口
--------------------UserRepository.java
----------------service   //业务
--------------------UserService.java
----resource
--------application.properties

首先先建一个表

CREATE TABLE `stus` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) DEFAULT NULL,
  `password` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=42 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

然后随便插几条数据即可

application.properties配置如下:

#端口配置
server.port = 8080 

#数据库配置
spring.datasource.url = jdbc:mysql://localhost:3306/数据库名?useSSL=false
#用户名
spring.datasource.username = 
#密码
spring.datasource.password = 
#驱动
spring.datasource.driver-class-name = com.mysql.jdbc.Driver

还是和以往一样先建一个实体类(User.java)

package com.example.app.entity;

/**
 * @author Woo_home
 * @create by 2019/10/4
 */
public class User {

    private int id;
    private String username;
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int 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;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

接着写一个mapper接口(UserRepository .java)

package com.example.app.mapper;

import com.example.app.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Woo_home
 * @create by 2019/10/4
 */
@Mapper
public interface UserRepository {

    @Select("select * from stus")
    List<User> getAllUser(); //查询所有用户
}

接着就是业务的编写

package com.example.app.service;

import com.example.app.entity.User;
import com.example.app.mapper.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @author Woo_home
 * @create by 2019/10/4
 */
@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public List<User> getAllUser(){
        return userRepository.getAllUser();
    }

}

控制器代码(UserController.java)

package com.example.app.controller;

import com.example.app.entity.User;
import com.example.app.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author Woo_home
 * @create by 2019/10/4
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/userList")
    @ResponseBody
    public List<User> userList(){
        return userService.getAllUser();
    }

}

启动类
@MapperScan是在插件包中定义的注解,该注解的参数是一个包名字符串,只要在@MapperScan中配置需要扫描的Mybatis接口的包路径,Spring就会自动扫描包下面的java接口,把这些类注册为Spring的bean

package com.example.app;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.app.mapper")
public class AppApplication {

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值