boot整合mybatis

0.圖解

圖片

一.pom依賴【重點】

1.代碼塊

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- mybatis起步依賴 -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.2</version>
    </dependency>
    <!-- mysql數據庫驅動 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!-- spring測試的啓動器 -->
    <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>

2.範例

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-ozxV3DyO-1590489295544)(https://uploader.shimo.im/f/ucBz5PanI58jZbj7.png!thumbnail)]

圖片

二.啓動類【重點】

1.代碼塊

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("cn.mapper")//開啓mapper掃描器
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

2.範例

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-tZ81S7Lm-1590489295547)(https://uploader.shimo.im/f/aPWKyd3sAqEmrQM8.png!thumbnail)]

三.yml配置類【重點】

1.代碼塊

spring:
  #數據庫連接配置
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/db2?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
    username: root
    password: root
#mybatis的相關配置
mybatis:
  #mapper配置文件
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: cn.pojo
  #開啓駝峯命名
  configuration:
    map-underscore-to-camel-case: true

2.範例

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-ZjJmlXlm-1590489295548)(https://uploader.shimo.im/f/eOAKLcxMONarEK9B.png!thumbnail)]

四.controller層

1.代碼塊

import cn.mapper.UserMapper;
import cn.pojo.User;
import cn.service.UserService;
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;
/**
 * 集成mybatis
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/findAll.do")
    @ResponseBody
    public List<User> findAll(){
        List<User> users = userService.findAll();
        System.out.println(users);
        return users;
    }
}

2.範例

圖片

五.pojo類

1.代碼塊

import java.io.Serializable;
public class User implements Serializable {
    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 + '\'' +
                '}';
    }
    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;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

2.範例

圖片

六.dao層【重點】

1.代碼塊

import cn.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
    public List<User> findAll();
}

2.範例

圖片

七.mapper的配置文件

1.代碼塊

<?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="demo.com.mapper.UserMapper">
    <select id="findAll" resultType="demo.com.pojo.User">
        select * from user
    </select>
</mapper>

2.範例

圖片

八.service層

1.代碼塊

import cn.mapper.UserMapper;
import cn.pojo.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> findAll(){
        List<User> users = userMapper.findAll();
        return users;
    }
}

2.範例

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-lrmt9WqF-1590489295554)(https://uploader.shimo.im/f/VGImSlcLbROnJ8aR.png!thumbnail)]

九.源碼

day02.rar

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章