2.springboot连接数据库获取数据并输出

pom.xml

 <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.1.0version>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>commons-fileuploadgroupId>
            <artifactId>commons-fileuploadartifactId>
            <version>1.3.2version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>
    dependencies>

1.新建mapper、service、包,并且创建mapper接口和service类

2.springboot连接数据库获取数据并输出_第1张图片
mapper接口内容

package com.example.second.mapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface TestMapper {
        String selectAll();
}

service

package com.example.second.service;
import com.example.second.mapper.TestMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class TestService {
    @Autowired
    private TestMapper mapper;
    public String selectAll(){
        return mapper.selectAll();
    }
}

之后application.properties下内容

spring.datasource.url=jdbc:mysql://localhost:3306/spider?characterEncoding=utf8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.tomcat.driver-class-name=com.mysql.jdbc.Driver
mybatis.configuration.map-underscore-to-camel-case=false
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.demo.model
spring.thymeleaf.prefix=classpath:/templates/
spring.resources.static-locations=classpath:/static/,classpath:/templates/
mybatis.configuration.mapUnderscoreToCamelCase=false

spring.datasource.url设置数据库地址
spring.datasource.username设置数据库用户名
spring.datasource.password设置密码
spring.datasource.tomcat.driver-class-name设置数据库驱动
在resources下新建mapper文件夹,新建xml文件
2.springboot连接数据库获取数据并输出_第2张图片
2.springboot连接数据库获取数据并输出_第3张图片



<mapper namespace="com.example.second.mapper.TestMapper">


    <select id="selectAll" resultType="String">
        select * from test where test2='asdc'
    select>
mapper>

在controller调用

package com.example.second.controller;

import com.example.second.service.TestService;
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;

@Controller
@RequestMapping("/home")
@ResponseBody
public class TestController {
    @Autowired
    private TestService service;


    @RequestMapping("/getdata")
    public String getData(){
        return "hello world";
    }

    @RequestMapping("/selectall")
    public String SelectAll(){
        System.out.println("数据库内容"+service.selectAll());
        return "hello world";
    }

}

2.springboot连接数据库获取数据并输出_第4张图片
2.springboot连接数据库获取数据并输出_第5张图片
完成

你可能感兴趣的:(spring,boot教程,spring,boot,mysql,数据库)