SpringBoot整合Mybatis框架

SpringBoot整合Mybatis框架

  1. 添加依赖

    pom.xml

    <dependency>  
       <groupId>org.mybatis.spring.boot</groupId>  
       <artifactId>mybatis-spring-boot-starter</artifactId>  
       <version>2.1.1</version>  
    </dependency>  
    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <scope>runtime</scope>
    </dependency>
  2. application.properties配置文件

    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/db_library?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    spring.datasource.username=root
    spring.datasource.password=12
  3. 可以再SpringBoot测试类中测试配置是否正确

     

     

 

 

补充:

在项目中配置多套环境的配置方法。 因为现在一个项目有好多环境,开发环境,测试环境,准生产环境,生产环境,每个环境的参数不同,所以我们就可以把每个环境的参数配置到yml文件中,这样在想用哪个环境的时候只需要在主配置文件中将用的配置文件写上就行如application.yml

笔记:在Spring Boot中多环境配置文件名需要满足application-{profile}.yml的格式,其中{profile}对应你的环境标识,比如:

application-dev.yml:开发环境 application-test.yml:测试环境 application-prod.yml:生产环境 至于哪个具体的配置文件会被加载,需要在application.yml文件中通过spring.profiles.active属性来设置,其值对应{profile}值。

application.yml

spring:
profiles:
  active: dev  #active;application-dev.yml配置文件生效

application-dev.yml

server:
port: 8080

spring:
datasource:
  username: root
  password: 12
  url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
  driver-class-name: com.mysql.cj.jdbc.Driver

#整合mybatis
mybatis:
mapper-locations: classpath:mybatis/mappers/*.xml   #接口文件位置
type-aliases-package: com.example.pojo      #别名的包

#showSql
logging:
level:
  com:
    example:
      mapper : debug

Dao:

//MyBatis的注解,目的是为了让spring能够根据xml和这个接口动态生成这个接口的实现。
//@Repository,就是spring生成一个bean,自动注入service的相关引用中。
@Mapper   //这个注解表示了这是一个mybatis的mapper类;或者可以在启动类里加上注解用于给出需要扫描的mapper文件路径@MapperScan("com.example.mapper")

@Repository  //spring整合
public interface UserMapper {
 User Sel(id);
}

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.mapper.UserMapper">

   <resultMap id="BaseResultMap" type="User">
       <result column="id" jdbcType="INTEGER" property="id" />
       <result column="userName" jdbcType="VARCHAR" property="userName" />
       <result column="passWord" jdbcType="VARCHAR" property="passWord" />
       <result column="realName" jdbcType="VARCHAR" property="realName" />
   </resultMap>

   <select id="Sel" resultType="com.example.entity.User">
      select * from user where id = #{id}
   </select>

</mapper>

UserService.java

@Service
public class UserService {
   @Autowired
   UserMapper userMapper;
   public User Sel(int id){
       return userMapper.Sel(id);
  }

UserController.java

@RestController
@RequestMapping("/testBoot")
public class UserController {

   @Autowired
   private UserService userService;

   @RequestMapping("getUser/{id}")
   public String GetUser(@PathVariable int id){
       return userService.Sel(id).toString();
  }

附一个框架学习Demo,传送门:spring boot + jpa + bootstrap + thymeleaf 简单的增删改查Demo

 

 

posted @ 2020-08-14 12:38  小菜bxb  阅读(160)  评论(0编辑  收藏  举报