IDEA创建SpringBoot项目并配置Mybatis

参考文章链接
https://www.jianshu.com/p/40efcf767037

创建SpringBoot项目

1.IDEA创建新项目->SpringInitializr->创建 SpringBoot项目,在依赖中选择SpringWeb
2.创建后修改pom.xmL文件代码如下



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.8.RELEASE
         
    
    com.example
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot

    
        1.8
    

    
        

        
            org.springframework.boot
            spring-boot-starter-web
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            org.projectlombok
            lombok
            true
        
        
            com.alibaba
            druid
            1.0.18
        
        
            org.slf4j
            log4j-over-slf4j
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


配置Mybatis

1.在pom文件中添加Mybatis依赖


    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    1.1.1

2.修改application.properties文件,代码如下

#Server Domain-Port
server.address=127.0.0.1
server.port=9998

#spring-datasource

mybatis.type-aliases-package=com.neo.entity
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/jtest?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
spring.datasource.username = root
spring.datasource.password = 123456


#encoding
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8

3.新建一个数据对象类,在其中规定数据库表所对应的对象

package com.example.demo.pojo;

public class DBinformation {
    private String id;
    private String db_name;
    private String db_host;
    private String db_passwd;
    public String getId() {
        return id;
    }

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

    public String getDb_name() {
        return db_name;
    }

    public void setDb_name(String db_name) {
        this.db_name = db_name;
    }

    public String getDb_host() {
        return db_host;
    }

    public void setDb_host(String db_host) {
        this.db_host = db_host;
    }

    public String getDb_passwd() {
        return db_passwd;
    }

    public void setDb_passwd(String db_passwd) {
        this.db_passwd = db_passwd;
    }
}

4.新建一个Mapper包,在其中添加我们要对不同数据表的不同操作,并在启动类添加备注,告诉其要扫描的Mapper包的位置
启动类:

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
@ComponentScan("com.example.demo")
public class DemoApplication {

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

Mapper类:

package com.example.demo.mapper;

import com.example.demo.pojo.DBinformation;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;

@Service
public interface UserMapper {

    @Select("SELECT * FROM user WHERE id = #{id}")
    DBinformation getOne(Long id);
}

5.添加Controller类,用来显示数据库中的信息

package com.example.demo.controller;

import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.DBinformation;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Controller
public class DBcontroller {
    @Autowired
    private UserMapper userMapper;
    @RequestMapping("/db")
    public void show(HttpServletRequest request, HttpServletResponse response) throws IOException {
        DBinformation dBinformation=userMapper.getOne((long) 1);
        ObjectMapper mapper=new ObjectMapper();
        response.getWriter().write(mapper.writeValueAsString(dBinformation));
    }
}

6.启动项目,打开网页localhost:9998即可看到数据库中user表id=1的所有信息

你可能感兴趣的:(SpringCloud)