zoukankan      html  css  js  c++  java
  • 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

     

     

  • 相关阅读:
    easyui的dataGrid生成的日期时间,总是不能很好的兼容ie8和谷歌,终于摸索出一个合适的办法
    DELPHI使用TClientDataSet时不携带MIDAS.DLL的方法
    你又重新年轻了一次,这一次你打算怎么活?
    c#网站项目的发布:项目方式、webSite网站模式(未能获得项目引用XXX的依赖项的解决)
    当取不到raisError的错误信息只能取到return的错误代码时,可以取connection.errors[0].description
    layer iframe大致使用
    全选
    下拉选
    checkbox
    js判断值对否为空
  • 原文地址:https://www.cnblogs.com/bxbo/p/13501534.html
Copyright © 2011-2022 走看看