spring boot集成mybatis
Posted pu20065226
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot集成mybatis相关的知识,希望对你有一定的参考价值。
1.新建一个spring boot web工程
idea: file-->new-->project-->Spring Initializr一路next下去。
2.最终目录
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.0.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>mybtest</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mybtest</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Mybatis通用Mapper --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.yml
spring: # application: # name: service-mybatistest2 datasource: # 数据库配置 url: jdbc:mysql://192.168.1.1:3306/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&maxReconnects=10 username: test password: test driverClassName: com.mysql.jdbc.Driver #server: # port: 8081 mybatis: configuration: # map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl mapper-locations: classpath:mappers/*.xml #eureka: # client: # serviceUrl: # #指向注册中心 # defaultZone: http://192.168.111.133:8888/eureka/ # instance: # # 每间隔1s,向服务端发送一次心跳,证明自己依然”存活“ # lease-renewal-interval-in-seconds: 1 # # 告诉服务端,如果我2s之内没有给你发心跳,就代表我“死”了,将我踢出掉。 # lease-expiration-duration-in-seconds: 2
3.创建mapper
接口ISelectIdMapper.java
package com.example.mybtest.mapper; import org.springframework.stereotype.Repository; //@Mapper @Repository public interface ISelectIdMapper String selectId(String name);
resource下userMapper.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.example.mybtest.mapper.ISelectIdMapper"> <select id="selectId" resultType="String"> select id from t_user where name=#name </select> </mapper>
4.创建service
接口IQueryIdByName.java
package com.example.mybtest; /** * @Title: * @Auther: test * @Date: 2019/6/24 17:26 * @Version: 1.0 * @Description: */ public interface IQueryIdByName String queryId(String Name);
实现类QueryIdByNameImpl.java
package com.example.mybtest; import com.example.mybtest.mapper.ISelectIdMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @Title: * @Auther: test * @Date: 2019/6/24 17:25 * @Version: 1.0 * @Description: */ @Service public class QueryIdByNameImpl implements IQueryIdByName @Autowired private ISelectIdMapper selectIdMapper; @Override public String queryId(String name) return selectIdMapper.selectId(name);
5.contoller类helloController.java
package com.example.mybtest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @Title: * @Auther: test * @Date: 2019/6/24 18:37 * @Version: 1.0 * @Description: */ @RestController public class helloController @Autowired QueryIdByNameImpl queryIdByNameImplService; @RequestMapping(value="/queryIdByName") public String queryId(String name) return queryIdByNameImplService.queryId(name); @RequestMapping(value="/selectId") public String say() String str="hello world mybatis"; System.out.println(str); return str;
6.启动类MybtestApplication
添加扫mapper
package com.example.mybtest; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @MapperScan("com.example.mybtest.mapper") @SpringBootApplication public class MybtestApplication public static void main(String[] args) SpringApplication.run(MybtestApplication.class, args);
7.启动测试
踩坑记:Invalid bound statement (not found)--spring boot集成mybatis
以上是关于spring boot集成mybatis的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot(十八):使用Spring Boot集成FastDFS