首页 > 其他分享 >spring boot——spring boot的基本配置——spring boot整合mybatis——教程

spring boot——spring boot的基本配置——spring boot整合mybatis——教程

时间:2023-01-02 10:33:45浏览次数:42  
标签:spring boot ch6 mybatis import com public

 

 

 

 

 

 

 

 

 

 

 

 

 

 

第二步——修改pom.xml文件:

 

 

 

 

pom文件:

<?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.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ch</groupId>
    <artifactId>ch6_6</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ch6_6</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 添加MySQL依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.45</version>
            <!-- MySQL8.x时,请使用8.x的连接器 -->
        </dependency>
        
        <!-- MyBatis-Spring,Spring Boot应用整合MyBatis框架的核心依赖配置-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

 

 

 

 

 

 

 

 

第三步——设置数据源信息——application.properties:

 

 

 

 

server.servlet.context-path=/ch6_6
###
##数据源信息配置
###
#数据库地址
spring.datasource.url=jdbc:mysql://localhost:3306/springbootjpa?characterEncoding=utf8
#数据库MySQL为8.x时,url为jdbc:mysql://localhost:3306/springbootjpa?useSSL=false&serverTimezone=Asia/Beijing&characterEncoding=utf-8
#数据库用户名
spring.datasource.username=root
#数据库密码
spring.datasource.password=root
#数据库驱动
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#数据库MySQL为8.x时,驱动类为com.mysql.cj.jdbc.Driver
#设置包别名(在Mapper映射文件中直接使用实体类名)
mybatis.type-aliases-package=com.ch.ch6_6.entity
#告诉系统在哪里去找mapper.xml文件(映射文件)
mybatis.mapperLocations=classpath:mappers/*.xml
#在控制台输出SQL语句日志
logging.level.com.ch.ch6_6.repository=debug
#让控制器输出的JSON字符串格式更美观
spring.jackson.serialization.indent-output=true

 

 

 

 

 

 

 

第四步——创建java实体类:

 

 

package com.ch.ch6_6.entity;
public class MyUser {
    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;
    }
}

 

 

 

 

 

 

 

 

 

第五步——创建数据访问接口:

 

 

package com.ch.ch6_6.repository;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.ch.ch6_6.entity.MyUser;
/**
 * MyBatis的Mapper映射接口
 */
@Mapper
public interface MyUserRepository {
    public List<MyUser> findAll();
}

 

 

 

 

 

 

 

 

 

 

第六步——创建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="com.ch.ch6_6.repository.MyUserRepository">
    <select id="findAll"  resultType="MyUser">
        select * from user 
    </select>
</mapper>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

第七步——创建业务层:

 

 

 

 

 

package com.ch.ch6_6.service;
import java.util.List;
import com.ch.ch6_6.entity.MyUser;
public interface MyUserService {
    public List<MyUser> findAll();
}

 

package com.ch.ch6_6.service;
import java.util.List;

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

import com.ch.ch6_6.entity.MyUser;
import com.ch.ch6_6.repository.MyUserRepository;
@Service
public class MyUserServiceImpl implements MyUserService{
    @Autowired
    private MyUserRepository myUserRepository;
    @Override
    public List<MyUser> findAll() {
        return myUserRepository.findAll();
    }
}

 

 

 

 

 

 

 

 

第八步——设置控制类:

 

 

package com.ch.ch6_6.controller;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ch.ch6_6.entity.MyUser;
import com.ch.ch6_6.service.MyUserService;
@RestController
public class MyUserController {
    @Autowired
    private MyUserService myUserService;
    @RequestMapping("/findAll")
    public List<MyUser> findAll(){
        return myUserService.findAll();
    }
}

 

 

 

 

 

 

 

 

 

 

 

第九步——设置扫描接口:

 

 

package com.ch.ch6_6;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//配置扫描MyBatis接口的包路径
@MapperScan(basePackages={"com.ch.ch6_6.repository"})
public class Ch66Application {
    public static void main(String[] args) {
        SpringApplication.run(Ch66Application.class, args);
    }
}

 

 

 

 

 

 

 

 

 

第十步——浏览器访问:

 

 

 

 

 

 

 

 

 

 

 

 

注意:一般注解不推荐使用:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


========================================================================================

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

标签:spring,boot,ch6,mybatis,import,com,public
From: https://www.cnblogs.com/xiaobaibailongma/p/17019484.html

相关文章