第1关:项目整合 - SpringBoot + MyBatis

本文介绍如何使用SpringBoot与MyBatis整合,快速构建查询用户信息的应用。从引入MyBatis-Spring-Boot-Starter依赖,配置DataSource,到编写实体类、Mapper接口及Controller,详细步骤助你轻松上手。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

声明:本博文如存在问题,欢迎各位dalao指正!!!

任务描述

本关任务:使用SpringBoot+MyBatis编写一个通过id查询用户信息的小程序。

相关知识

MyBatis-Spring-Boot-Starter可帮助你在Spring Boot之上快速构建MyBatis应用程序。为了完成本关任务,你需要掌握:1.使用MyBatis-Spring-Boot-Starter进行整合SpringBoot + MyBatis。2.使用SpringBoot + MyBatis编写一个查询用户信息的接口

Spring整合MyBatis资料下载:
自动生成demo压缩包

1.首先引入MyBatis-Spring-Boot-Starter依赖:
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.0</version>
    </dependency>
2.接下来配置application.properties来定义一个DataSource
    #mysql驱动
    spring.datasource.driverClassName = com.mysql.jdbc.Driver
    #mysql地址
    spring.datasource.url = jdbc:mysql://localhost:3306/information_schema?useUnicode=true&cha\fracterEncoding=utf-8
    #mysql用户名
    spring.datasource.username = root
    #mysql密码
    spring.datasource.password = 123123
3.使用SpringBoot + MyBatis编写一个查询用户信息的接口
    //我们先使用注解的方式创建一个mapper接口:
    @Mapper
    public interface DemoMapper {
        @Select("select * from users where userId = #{id}")
        Users selectUser(int id);
    }
//Users实体类如下:
@Data
public class Users {
    private int id;
    private String username;
    private String password;
}
//接下来让mapper像下面这样注入:
    @Controller
    public class DemoController {
        @Autowired
        DemoMapper demoMapper;
        @RequestMapping("/query")
        @ResponseBody
        public Users query(int id) {
            Users user = demoMapper.selectUser(id);
            return user;
        }
    }
  // 最后我们只需要创建一个普通的Spring启动应用程序:
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }

编程要求及实现

*1.在 pom.xml 里增加MyBatis-Spring-Boot-Starter依赖;
*

		<!--              Begin           -->
		<dependency>
        	<groupId>org.mybatis.spring.boot</groupId>
        	<artifactId>mybatis-spring-boot-starter</artifactId>
        	<version>2.1.0</version>
 	   </dependency>

		<!--               End           -->

2.在application.properties定义DataSource,用户名和密码前后不要有空格,不然会导致密码错误;

logging.config=classpath:log4j.properties
spring.datasource.initialization-mode=always
/********** Begin **********/
 	#mysql驱动
    spring.datasource.driverClassName = com.mysql.jdbc.Driver
    #mysql地址
    spring.datasource.url = jdbc:mysql://localhost:3306/information_schema?useUnicode=true&cha\fracterEncoding=utf-8
    #mysql用户名
    spring.datasource.username =root
    #mysql密码
    spring.datasource.password =123123
 
/********** End **********/

3.在Users中创建用户信息实体类userId、userName、passWord;

package net.educoder.entity;

import javax.validation.constraints.NotBlank;

import lombok.Data;

@Data
public class TUser {

/********* Begin *********/
private int userId;
private String userName;
private String passWord;
/********* End *********/


}

4.在DemoMapper增加查询用户信息的数据库访问接口;

package net.educoder.mapper;

import net.educoder.entity.TUser;
import net.educoder.entity.Users;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface DemoMapper {

   /********* Begin *********/
	@Select("select * from t_user where userId = #{id}")
	TUser selectUser(int id);
/********* End *********/

}

5.在DemoController增加前端请求接口,设置请求地址为/query, 调用DemoMapper的接口获取数据库数据。

package net.educoder.controller;

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;

import net.educoder.entity.TUser;
import net.educoder.mapper.DemoMapper;

@Controller
public class DemoController {


	/********* Begin *********/
	@Autowired
	DemoMapper demoMapper;

	@RequestMapping("/query")
	@ResponseBody
	public TUser query(int id){
		TUser user = demoMapper.selectUser(id);
		return user;
	}
  /********* End *********/

}

总结:

当我们使用Spring+MyBatis进行项目整合后,给人的感受就是配置一些东西变得更加简单了,我们可以很方便的从前端发送请求到后台,并操作数据。而以前需要的Servlet,dao层等,则需要配置很多文件,需要外导入很多架包,开发起来比较麻烦。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

村头卖假发的小郑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值