SpringBoot+Mybatis

作者: Taoyongpan | 来源:发表于2018-10-06 15:22 被阅读32次

SSM---->SpringBoot+Mybatis

从Spring+SpringMvc+Mybatis到SpringBoot+Mybatis,具体的优缺点和特性,百度到处都是,这篇文章就不再重复的叙述,主要讲一下用法(真实原因是lz也没有细看呢,折腾两个小时主要做的是从认识到应用),在接下来的文章中会进行讲解;

IDEA创建SpringBoot项目

1、New--->project--->Spring Initializr


image.png

这里面我遇到了一个小坑,就是选择默认的时候,一直连接不上这个网址,百度大神的指导是把https变成http,果然可行;当连接不上的时候,两种之间相互切换即可;
2、next,自主配置文件名


image.png

3、next,选择web


image.png

4、文件目录


image.png

配置Mybatis及连接数据库

1、文件目录


image.png

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.tao</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/>
        <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>
            <version>5.1.45</version>
        </dependency>
        <!--配置mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2、SpringBoot支持yml编程,这里我使用的是yml(推荐),结构比较清晰明了;

//数据库连接配置
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/db_login
    username: root
    password: root
//此项目的server配置
//我的demo的访问路径:http://localhost:8081/springboot/list
server:
  port: 8081
  servlet:
    path: /springboot
//mybatis的xml地址配置
mybatis:
  mapper-locations: classpath:mapper/*.xml

中间遇到一个问题:mapper的bean找不到,在mapper接口类上面加@Repository注解即可解决问题:


image.png

下面直接贴出代码:
entity/User.java:

package com.tao.springboot.entity;

/**
 * Author: Taoyongpan
 * Date: Created in 13:53 2018/10/6
 */
public class User {

    private Integer id;

    private String username;

    private String password;

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

mapper/UserMapper.java:

package com.tao.springboot.mapper;

import com.tao.springboot.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Author: Taoyongpan
 * Date: Created in 13:57 2018/10/6
 */
@Mapper
@Repository
public interface UserMapper {
    public List<User> listUser();
}

resources/mapper/UserMapper.xml:

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE mapper PUBLIC
        "-//mybatis.org//DTD com.example.Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tao.springboot.mapper.UserMapper">
    <resultMap id="result" type="com.tao.springboot.entity.User">
        <result property="username" column="username" />
        <result property="password" column="password" />
    </resultMap>

    <select id="listUser" resultMap="result">
      SELECT * FROM user
   </select>

</mapper>

service/UserService.java:

package com.tao.springboot.service;

import com.tao.springboot.entity.User;

import java.util.List;

/**
 * Author: Taoyongpan
 * Date: Created in 14:01 2018/10/6
 */
public interface UserService {

    public List<User> listUser();
}

service/impl/UserServiceImpl.java:

package com.tao.springboot.service.impl;

import com.tao.springboot.entity.User;
import com.tao.springboot.mapper.UserMapper;
import com.tao.springboot.service.UserService;

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

import java.util.List;

/**
 * Author: Taoyongpan
 * Date: Created in 14:01 2018/10/6
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    @Override
    public List<User> listUser() {
        return userMapper.listUser();
    }
}

controller/UserController.java:

package com.tao.springboot.controller;

import com.tao.springboot.entity.User;
import com.tao.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Author: Taoyongpan
 * Date: Created in 9:03 2018/10/6
 */
@RestController
@RequestMapping("/")
public class UserController {

    @Autowired
    UserService userService;
    @RequestMapping(value = "list" ,method = RequestMethod.GET)
    public List<User> listUser(){
        return userService.listUser();
    }
}

启动:
直接运行SpringBootApplication里面的main方法即可;这里和国产框架Jfinal的原理有些类似;
效果图:


image.png

总结

在我看来SpringBoot就是一个Spring体系的的各种配置进行了封装,Java这个封装的思想一层层的像上堆叠,不知道是好还是不好;把原来乱糟糟的配置,封装起来,提供给我们最简洁的接口,真正使用起来和SSM框架并无太大的区别,在各个用法的时候也是对以前一些相对啰嗦的语法结构进行封装或重构。大概就是这样,会逐渐的深入这个框架,用起来的确比SSM爽太多了。

相关文章

网友评论

    本文标题:SpringBoot+Mybatis

    本文链接:https://www.haomeiwen.com/subject/tvdhaftx.html