搭建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,实现简单查询数据库功能功能
项目结构
-
项目结构
-
加入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”)
三、效果
在数据库中增加数据后,效果
(完)
相关文章推荐
- 【个人学习】使用idea搭建SpringBoot,整合Mybatis、Thymeleaf,连接数据库,实现具有前端界面项目:主要功能登录,注册,个人信息查看、更改,不定时更新中...
- 使用idea 搭建一个 SpringBoot + Mybatis + logback 的maven 项目
- 使用SpringBoot搭建小型项目,集成mybatis,redis,swagger2,并部署在外部容器中。
- 使用idea+springboot+Mybatis搭建web项目
- 架构实战项目心得(七):使用SpringBoot+Dubbo+Mybatisplus+Oracle搭建后台项目框架(二)
- Myeclipse下使用Maven搭建spring boot项目采用spring boot devtools实现热部署
- 架构实战项目心得(七):使用SpringBoot+Dubbo+Mybatisplus+Oracle搭建后台项目框架(一)
- 使用IDEA搭建SpringBoot项目整合Mybatis踩的坑。。。。
- 使用HikariCP数据库连接池搭建Spring-Boot+Mybatis的项目
- 使用idea+springboot+Mybatis搭建web项目
- SpringBoot+Mybatis+Mysql项目实现查询功能
- maven项目 spring-boot 整合 mybatis 实现查询功能demo
- 使用idea+springboot+Mybatis搭建web项目
- eclipse搭建简单springboot-mybatis项目实现页面简单跳转
- 使用IDEA创建Spring Boot项目, 整合Mybatis ,连接MySql数据库,实现简单的登录注册功能
- 使用idea+springboot+Mybatis搭建web项目
- 基于SpringBoot + Mybatis实现SpringMVC Web项目
- 使用IDEA和Maven搭建一个Spring Boot项目
- 搭建spring boot集成mybatis项目入门
- SpringBoot+Mybatis项目使用Redis做Mybatis的二级缓存的方法