您的位置:首页 > 编程语言 > Java开发

搭建spring-boot项目,并使用mybatis实现查询

2020-06-02 05:46 344 查看

一、搭建spring-boot项目

1、打开itellij,点击new–>Project–>Spring Initralizr(如果没有Spring Initralizr,请到setting–>plugin下搜索spring-boot安装插件)
填写好项目名字,选择devTool,spring-Web依赖,生成spring项目

等待安装完依赖包,启动sping-boot项目

2、集成mybatis,实现简单查询数据库功能功能
项目结构

  1. 项目结构

  2. 加入mybatis和mysql依赖
    pom.xml

<!--mybatis-->
<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>
<version>5.1.37</version>
</dependency>

3增加配置项, 加入mybatis和mysql依赖
application.properties

# server.servlet.context-path=/business
# redis配置
# spring.redis.host=47.100.55.40
# spring.redis.port=6379
# spring.redis.password=Redis000
#端口
server.port=3111
# 增加数据库连接
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF8&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#mybatis的mapper路径
mybatis.mapper-locations=classpath:/mapper/*.xml

4、在resource增加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.example.demo1.mapper.testMapper">
<select id="list"  resultType="com.example.demo1.domain.Test">
select `id`,`name` from `test`
</select>
</mapper>

5、domain增加dto

package com.example.demo1.domain;

public class Test {
private String id;
private String name;

public String getId( ) {
return id;
}

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

public String getName( ) {
return name;
}

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

6、mapper文件夹下增加mapper接口

package com.example.demo1.mapper;

import com.example.demo1.domain.Test;

import java.util.List;

public interface testMapper {
public List<Test> list();
}

7、mapper文件夹下增加mapper接口

package com.example.demo1.mapper;

import com.example.demo1.domain.Test;

import java.util.List;

public interface testMapper {
public List<Test> list();
}

8、增加service层

package com.example.demo1.service;

import com.example.demo1.domain.Test;
import com.example.demo1.mapper.testMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TestService {
@Resource
testMapper testMapper;
public List<Test> list() {
List<Test> list = testMapper.list();
return  list;
}
}

9、增加controller层

package com.example.demo1.controller;

import com.example.demo1.domain.Test;
import com.example.demo1.service.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class test {
private static final Logger LOG = LoggerFactory.getLogger(test.class);
@Resource
TestService testService;
@GetMapping("/list")
public List<Test> list() {
return testService.list();
}
}

10、在application文件中加入mapper扫描
@MapperScan(“com.example.demo1.mapper”)

三、效果


在数据库中增加数据后,效果

(完)

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐