参考链接:https://www.cnblogs.com/gavincoder/p/10105762.html
项目结构:
数据库中的测试表(sys_user_role):
1.在springboot项目的pom.xml文件:
<?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 http://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.4.RELEASE</version> <relativePath/> </parent> <groupId>com.shengquan</groupId> <artifactId>meeting</artifactId> <version>0.0.1-SNAPSHOT</version> <name>meeting</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!--Spring Boot的核心启动器,包含了自动配置、日志和YAML--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--springboot程序测试依赖,如果是自动创建项目默认添加--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--springboot web模块支持,自动帮我们引入了web模块开发需要的相关jar包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--MySQL 连接驱动依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- SpringBoot Mybatis 依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.0</version> </dependency> </dependencies> <!--maven构建--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.yml文件
server: port: 9002 servlet: context-path: /meeting spring: #数据源 datasource: url: jdbc:mysql://localhost:3306/water?useUnicode=true&characterEncoding=utf8&useSSL=true username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver #mybatis配置 mybatis: typeAliasesPackage: com.shengquan.meeting.entity mapperLocations: classpath:mapper/*.xml
3.entity目录下实体类:
import lombok.Data; /** * @description:测试实体类 * @author: Administrator * @date: 2019-04-25 10:06 */ @Data public class Department { private long id; private long user_id; private long role_id; }
4.mapper目录下:
import com.shengquan.meeting.entity.Department; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; /** * @description: * @author: Administrator * @date: 2019-04-25 10:41 */ @Mapper @Repository public interface TestMapper { List<Department> findall(); }
5.resources目录下新建mapper目录,创建TestMapper.xml文件:
<?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.shengquan.meeting.mapper.TestMapper"> <select id="findall" resultType="com.shengquan.meeting.entity.Department"> SELECT * FROM sys_user_role </select> </mapper>
6.service目录中:
import com.shengquan.meeting.entity.Department; import com.shengquan.meeting.mapper.TestMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * @description: * @author: Administrator * @date: 2019-04-25 10:45 */ @Service public class TestService { @Autowired private TestMapper testMapper; public List<Department> findaa(){ return testMapper.findall(); } }
7.controller目录中:
import com.shengquan.meeting.service.TestService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @description: * @author: Administrator * @date: 2019-04-25 09:02 */ @RestController public class TestController { @Autowired private TestService testService; @GetMapping("test") public String test(){ System.out.println("查询到数据=="+testService.findaa().toString()); return "测试"+testService.findaa().toString(); } }
8.测试:
http://localhost:9002/meeting/test
demo源码:https://gitee.com/xusea/springbootmybatis.git