赞
踩
觉得有帮助或需要源码请点赞关注收藏后评论区留言或者私信
MyBatis本来是Apache的一个开源项目iBatis 2010年这个项目由Apache Software Foundation迁移到了Google Code 并且改名为MyBatis
MyBatis是一个基于Java的持久层框架,MyBatis提供的持久层框架包括SQL Maps和Data Access Objects(DAO) 它消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索,MyBatis使用简单的XML或注解用于配置和原始映射,将接口和Java的POJOs映射成数据库中的记录
下面实战讲解如何在Spring Boot应用中使用MyBatis框架操作数据库
创建步骤可以参考这篇博客快速创建Spring Boot应用
- <?xml version="1.0" encoding="UTF-8"?>
-
- -<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
-
- <modelVersion>4.0.0</modelVersion>
-
-
- -<parent>
-
- <groupId>org.springframework.boot</groupId>
-
- <artifactId>spring-boot-starter-parent</artifactId>
-
- <version>2.1.7.RELEASE</version>
-
- <relativePath/>
-
- <!-- lookup parent from repository -->
-
-
- </parent>
-
- <groupId>com.ch</groupId>
-
- <artifactId>ch6_6</artifactId>
-
- <version>0.0.1-SNAPSHOT</version>
-
- <name>ch6_6</name>
-
- <description>Demo project for Spring Boot</description>
-
-
- -<properties>
-
- <java.version>11</java.version>
-
- </properties>
-
-
- -<dependencies>
-
-
- -<dependency>
-
- <groupId>org.springframework.boot</groupId>
-
- <artifactId>spring-boot-starter-web</artifactId>
-
- </dependency>
-
- <!-- 添加MySQL依赖 -->
-
-
-
- -<dependency>
-
- <groupId>mysql</groupId>
-
- <artifactId>mysql-connector-java</artifactId>
-
- <version>5.1.45</version>
-
- <!-- MySQL8.x时,请使用8.x的连接器 -->
-
-
- </dependency>
-
- <!-- MyBatis-Spring,Spring Boot应用整合MyBatis框架的核心依赖配置-->
-
-
-
- -<dependency>
-
- <groupId>org.mybatis.spring.boot</groupId>
-
- <artifactId>mybatis-spring-boot-starter</artifactId>
-
- <version>2.1.0</version>
-
- </dependency>
-
-
- -<dependency>
-
- <groupId>org.springframework.boot</groupId>
-
- <artifactId>spring-boot-starter-test</artifactId>
-
- <scope>test</scope>
-
- </dependency>
-
- </dependencies>
-
-
- -<build>
-
-
- -<plugins>
-
-
- -<plugin>
-
- <groupId>org.springframework.boot</groupId>
-
- <artifactId>spring-boot-maven-plugin</artifactId>
-
- </plugin>
-
- </plugins>
-
- </build>
-
- </project>
在application.properties配置如下内容
- server.servlet.context-path=/ch6_6
- ###
- ##数据源信息配置
- ###
- #数据库地址
- spring.datasource.url=jdbc:mysql://localhost:3306/springbootjpa?characterEncoding=utf8
- #数据库MySQL为8.x时,url为jdbc:mysql://localhost:3306/springbootjpa?useSSL=false&serverTimezone=Asia/Beijing&characterEncoding=utf-8
- #数据库用户名
- spring.datasource.username=root
- #数据库密码
- spring.datasource.password=root
- #数据库驱动
- spring.datasource.driver-class-name=com.mysql.jdbc.Driver
- #数据库MySQL为8.x时,驱动类为com.mysql.cj.jdbc.Driver
- #设置包别名(在Mapper映射文件中直接使用实体类名)
- mybatis.type-aliases-package=com.ch.ch6_6.entity
- #告诉系统在哪里去找mapper.xml文件(映射文件)
- mybatis.mapperLocations=classpath:mappers/*.xml
- #在控制台输出SQL语句日志
- logging.level.com.ch.ch6_6.repository=debug
- #让控制器输出的JSON字符串格式更美观
- spring.jackson.serialization.indent-output=true
此处不再赘述 可以参考这篇文章创建实体类
- package com.ch.ch6_6.repository;
- import java.util.List;
- import org.apache.ibatis.annotations.Mapper;
- import com.ch.ch6_6.entity.MyUser;
- /**
- * MyBatis的Mapper映射接口
- */
- @Mapper
- public interface MyUserRepository {
- public List<MyUser> findAll();
- }
在src/main/recources目录下 创建名为mappers的包,并在该包中创建SQL映射文件MyUserMapper.xml 代码如下
- <?xml version="1.0" encoding="UTF-8"?>
-
- <!DOCTYPE mapper SYSTEM "http://mybatis.org/dtd/mybatis-3-mapper.dtd" PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN">
-
- -<mapper namespace="com.ch.ch6_6.repository.MyUserRepository">
-
- <select resultType="MyUser" id="findAll">select * from user </select>
-
- </mapper>
接口代码如下
- package com.ch.ch6_6.service;
- import java.util.List;
- import com.ch.ch6_6.entity.MyUser;
- public interface MyUserService {
- public List<MyUser> findAll();
- }
实现类代码如下
- package com.ch.ch6_6.service;
- import java.util.List;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import com.ch.ch6_6.entity.MyUser;
- import com.ch.ch6_6.repository.MyUserRepository;
- @Service
- public class MyUserServiceImpl implements MyUserService{
- @Autowired
- private MyUserRepository myUserRepository;
- @Override
- public List<MyUser> findAll() {
- return myUserRepository.findAll();
- }
- }
- package com.ch.ch6_6.controller;
- import java.util.List;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import com.ch.ch6_6.entity.MyUser;
- import com.ch.ch6_6.service.MyUserService;
- @RestController
- public class MyUserController {
- @Autowired
- private MyUserService myUserService;
- @RequestMapping("/findAll")
- public List<MyUser> findAll(){
- return myUserService.findAll();
- }
- }
- package com.ch.ch6_6;
- import org.mybatis.spring.annotation.MapperScan;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- @SpringBootApplication
- //配置扫描MyBatis接口的包路径
- @MapperScan(basePackages={"com.ch.ch6_6.repository"})
- public class Ch66Application {
- public static void main(String[] args) {
- SpringApplication.run(Ch66Application.class, args);
- }
- }
然后运行主类,接着访问http://localhost:8080/ch6_6/findAll即可
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。