目录
案例的项目结构
1.导入坐标(pom)
使用lombok插件,需要去下载中心下载
<?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.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>edu.xiao</groupId>
    <artifactId>springboot_mybatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot_mybatis</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--mybatis的spring起步依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <!--添加数据库坐标-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  2.配置数据库以及mybatis(application.properties)
#配置数据库
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql:///ssm?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=y426759813.
#配2,置mybatis的信息
#spring集成Mybatis环境
#pojo别名扫描包
mybatis.type-aliases-package=edu.xiao.springboot_mybatis.pojo
#加载Mybatis映射文件
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
  3.编写mybatis查询(UserMapper.xml)
注意: namespace的值是你接口的位置;使用了别名可以直接使用user
<?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="edu.xiao.springboot_mybatis.mapper.IUserMapper">
    <select id="findAllUser" resultType="user">
        select * from user
    </select>
</mapper>
  4.编写逻辑代码
a.mapper
其中@mapper注解r= @Repository + @MapperScan(basePackages = “{}”)
package edu.xiao.springboot_mybatis.mapper;
import edu.xiao.springboot_mybatis.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface IUserMapper {
   
    List<User> findAllUser();
}
  b.servise类
实现类
package edu.xiao.springboot_mybatis.service.impl;
import edu.xiao.springboot_mybatis.mapper.IUserMapper;
import edu.xiao.springboot_mybatis.pojo.User;
import edu.xiao.springboot_mybatis.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService implements IUserService {
   
    @Autowired
    private IUserMapper mapper;
    @Override
    public List<User> findAllUser() {
   
        return mapper.findAllUser();
    }
}
  IUserService 接口类
package edu.xiao.springboot_mybatis.service;
import edu.xiao.springboot_mybatis.pojo.User;
import java.util.List;
public interface IUserService {
   
    List<User> findAllUser();
}
  c.pojo类
使用了lombok插件
package edu.xiao.springboot_mybatis.pojo;
import lombok.Data;
@Data
public class User {
   
    private Integer id;
    private String username;
    private String password;
    private String name;
    @Override
    public String toString() {
   
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}
  5.controller类(UserController)
package edu.xiao.springboot_mybatis.controller;
import edu.xiao.springboot_mybatis.mapper.IUserMapper;
import edu.xiao.springboot_mybatis.pojo.User;
import edu.xiao.springboot_mybatis.service.IUserService;
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 java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
   
    @Autowired
    private IUserService service;
    @RequestMapping("/findAllUser")
    @ResponseBody
    public List<User> findAllUser(){
   
        List<User> allUser = service.findAllUser();
        System.out.println(allUser);
        return allUser;
    }
}
  6.效果
数据库内容
查询的内容

京公网安备 11010502036488号