SpringBoot集成MyBatis超详细教程

1、创建项目略…

2、创建数据库数据表

  • 提供的MySQL语句如下
/*创建数据库 springbootdb*/
CREATE DATABASE /*!32312 IF NOT EXISTS*/`springbootdb` /*!40100 DEFAULT CHARACTER SET utf8
*/;
USE `springbootdb`;
/*创建表 city*/
DROP TABLE IF EXISTS `city`;
CREATE TABLE `city` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '城市编号', `provinceId` int(10) unsigned DEFAULT NULL COMMENT '省份编号', `cityName` varchar(20) DEFAULT NULL COMMENT '城市名称', `description` text COMMENT '城市描述', PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
/*插入测试数据 */
insert into `city`(`id`,`provinceId`,`cityName`,`description`) values (1,1,'北京','这是北京市的描述信
息,北京这家伙是中国首都,百年帝都,政治经济文化中心,也将是世界的中心.'),(2,2,'郑州','这是郑
州市的描述信息,郑州这家伙是河南省会,城市中的后起之秀,河南政治经济文化中心,也是中国的
中心城市.'),(3,3,'ZhengZhou','这是郑州市的描述信息,郑州这家伙是河南省会,城市中的后起之秀, 河南政治经济文化中心,也是中国的中心城市.');

3、在pom.xml中导入依赖

  • 版本可以根据自己定义
  • SpringBoot对Mybatis的支持
  • MySQL驱动依赖
  • 依赖不仅限于只有下面两个
`<!-- Spring Boot Mybatis 依赖 -->
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.3.2</version>
</dependency>
<!-- MySQL 连接驱动依赖 -->
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<version>5.1.38</version>
</dependency>`

4、配置application.properties文件

  • 配置连接数据库
  • 实体类起别名配置
  • mapper.xml文件所在位置配置
  • 请注意用户名、密码、数据库名
  • 请不要随便在此文件点击空格
## 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/库名?characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
## Mybatis 配置
# 实体所在包,起别名
mybatis.typeAliasesPackage=实体类所在路径
# 映射文件所在路径
mybatis.mapperLocations=classpath:mapper/*.xml

5、在启动类上添加mapper 接口类扫描包配置

@SpringBootApplication // Spring Boot 应用的标识
@MapperScan("com.wzc.boot.dao") // mapper 接口类扫描包配置
public class Application {
public static void main(String[] args) {
		// 程序启动入口
		// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
		SpringApplication.run(Application.class,args);
	}
}

6、entity实体类

  • 实体类非常简单,根据数据库即可
package com.wzc.boot.entity;

public class City {
	private Integer id;
	private Integer provinceId;
	private String cityName;
	private String description;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getProvinceId() {
		return provinceId;
	}

	public void setProvinceId(Integer provinceId) {
		this.provinceId = provinceId;
	}

	public String getCityName() {
		return cityName;
	}

	public void setCityName(String cityName) {
		this.cityName = cityName;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public City(Integer id, Integer provinceId, String cityName, String description) {
		super();
		this.id = id;
		this.provinceId = provinceId;
		this.cityName = cityName;
		this.description = description;
	}

	public City() {
	}
}

7、编写DAO层接口

  • 注意是接口interface
package com.wzc.boot.dao;

import java.util.List;

import com.wzc.boot.entity.City;
public interface CityMapper{
	List<City> getAll();
}

8、根据接口实现xml配置、sql语句等

  • 请注意namespace的路径一定要是具体接口的路径
  • 请注意resultType的路径一定要是实体类的路径,如果起了别名配置可以首字母小写。
  • id的路径一定要是具体的接口名
<?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.wzc.boot.dao.CityMapper">
	<select id="getAll" resultType="com.wzc.boot.entity.City">
		select * from city
	</select>
</mapper>

9、Service、ServiceImpl业务层编写

Service

package com.wzc.boot.service;

import java.util.List;

import com.wzc.boot.entity.City;

public interface CityService {
	List<City> getAll();
}

ServiceImpl

  • @Service:注入Spring容器
  • @Autowired:引用数据层
package com.wzc.boot.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wzc.boot.dao.CityMapper;
import com.wzc.boot.entity.City;
import com.wzc.boot.service.CityService;

@Service
public class CityServiceImpl implements CityService{

	@Autowired
	private CityMapper cityMapper;
	
	@Override
	public List<City> getAll() {
		// TODO 自动生成的方法存根
		return cityMapper.getAll();
	}
}

10、Controller层

  • @Controller:跟@Service作用一样
  • @RequestMapping:页面访问路径
  • @ResponseBody:返回JSON格式数据
package com.wzc.boot.controller;

import java.util.List;

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 com.wzc.boot.entity.City;
import com.wzc.boot.service.CityService;

@Controller
public class CityController {
	@Autowired
	private CityService cityService;
	
	@RequestMapping("/getAllCity")
	@ResponseBody
	public List<City> getAll() {
		return cityService.getAll();
	}
}

11、最后在浏览器进行访问即可

http://localhost:8080/getAllCity
在这里插入图片描述

12、项目结构图

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值