1.pom.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>mybatisdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatisdemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<!--spring+mybatis主要jar包-->
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<!--lombok包,用于简化代码,注解省略setter,getter,默认构造方法等-->
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.配置数据源,直接在配置文件中配置数据源,可以使用properties文件或者yml文件
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=1234567890
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#MySQL5用的驱动url是com.mysql.jdbc.Driver,MySQL6以后用的是com.mysql.cj.jdbc.Driver,
连接MySQL6(com.mysql.cj.jdbc.Driver),需要指定时区serverTimezone
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC
password: root
username: ******
driver-class-name: com.mysql.cj.jdbc.Driver
3.实体类User
package com.example.mybatisdemo.entity;
import lombok.Data;
@Data//这里使用注解生成setter,getter,默认构造器等
public class User {
private int id;
private String username;
private String name;
private int age;
private int balance;
}
4.MyBatis Mapper接口
package com.example.mybatisdemo.dao;
import com.example.mybatisdemo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper//注明这是一个mapper类,用于MapperScan扫描
public interface UserMapper {
@Select("select * from user")
List<User> queryAll();
}
5.Service层UserServeice
package com.example.mybatisdemo.service;
import com.example.mybatisdemo.dao.UserMapper;
import com.example.mybatisdemo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> getAllUsers() {
return userMapper.queryAll();
}
}
6.Controller层UserController
package com.example.mybatisdemo.controller;
import com.example.mybatisdemo.entity.User;
import com.example.mybatisdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RequestMapping("/user")
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/getallusers")
public List<User> getAllUsers() {
return userService.getAllUsers();
}
}
7.入口类,Main方法
package com.example.mybatisdemo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.mybatisdemo.dao")//扫描特定包下的所有mapper
public class MybatisdemoApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisdemoApplication.class, args);
}
}
8.SQL文件
/*
Navicat Premium Data Transfer
Source Server : localhostMysql
Source Server Type : MySQL
Source Server Version : 50726
Source Host : localhost:3306
Source Schema : test
Target Server Type : MySQL
Target Server Version : 50726
File Encoding : 65001
Date: 07/10/2019 20:01:36
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`username` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '',
`name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`age` int(11) NOT NULL,
`balance` int(11) UNSIGNED ZEROFILL NOT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, 'accout1', 'zhangsan', 20, 00000000100);
INSERT INTO `user` VALUES (2, 'accout2', 'lisi', 28, 00000000180);
INSERT INTO `user` VALUES (3, 'accout3', 'wangwu', 32, 00000000250);
INSERT INTO `user` VALUES (4, 'accout4', 'saozhu', 18, 00000000200);
INSERT INTO `user` VALUES (5, 'accout5', 'zhangtao', 22, 00000000540);
SET FOREIGN_KEY_CHECKS = 1;