1. 程式人生 > 實用技巧 >EF Core知識筆記三

EF Core知識筆記三

1、建立springboot工程

2、新增mybatis依賴和mysql依賴

3、建立實體類

4、建立資料庫表

5、建立application.properties檔案,配置資料庫連線

6、建立mapper介面

7、建立mapper對映檔案

8、在application.properties中配置mybatis資訊

1、新增依賴,注意springboot版本2.0.2

<?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.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>cn.lgy</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.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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>

關鍵程式碼

     <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>

2、建立實體類

package cn.lgy.domain;

public class User {

    private Integer id;
    private String name;
    private String sex;
    private String address;

    public Integer getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getSex() {
        return sex;
    }

    public String getAddress() {
        return address;
    }

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

    public void setName(String name) {
        this.name = name;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

3、建立資料庫表

4、建立application.properties檔案,配置資料庫連線

#資料庫配置
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot-9501?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root

#配置mybatis配置資訊
#pojo別名配置
mybatis.type-aliases-package=cn.lgy.domain
#載入mybatis對映檔案
mybatis.mapper-locations=classpath:mapper/*Mapper.xml

5、建立mapper介面

package cn.lgy.mapper;

import cn.lgy.domain.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserMapper {

    public List<User> queryUserList();
}

6、建立mapper對映檔案

<?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="cn.lgy.mapper.UserMapper">
    <select id="queryUserList" resultType="cn.lgy.domain.User">
        select * from user
    </select>
</mapper>

7、將mybatis資訊註冊到springboot中,

application.properties中新增以下內容

#配置mybatis配置資訊
#pojo別名配置
mybatis.type-aliases-package=cn.lgy.domain
#載入mybatis對映檔案
mybatis.mapper-locations=classpath:mapper/*Mapper.xml

8、在啟動類上添加註解、

@MapperScan("cn/lgy/mapper")//用於掃描mapper包下的介面
@ComponentScan("cn.lgy.controller")//用於掃描controller或service包下的類
package cn.lgy.springbootmybatis;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@MapperScan("cn/lgy/mapper")//用於掃描mapper包下的介面
@ComponentScan("cn.lgy.controller")//用於掃描controller或service包下的類
public class SpringbootMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisApplication.class, args);
    }

}

9、編寫controller測試

package cn.lgy.controller;

import cn.lgy.domain.User;
import cn.lgy.mapper.UserMapper;
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
public class UserController {

    @Autowired
    UserMapper userMapper;

    @RequestMapping("/query")
    @ResponseBody
    public List<User> queryUserList(){
        List<User> users = userMapper.queryUserList();
        return users;
    }

}

10、因為版本不同所以注意測試類上@Test註解的包匯入

package cn.lgy.springbootmybatis;

import org.junit.Test;

import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringbootMybatisApplicationTests {

    @Test
    void contextLoads() {
    }

}