【springboot】整合 MyBatis

 转自:https://blog.csdn.net/cp026la/article/details/86493503

1. 简介:

  目前,国内大部分公司都使用 MyBatis作为持久层框架。本章整合MyBatis,在上一章的基础上进行扩展。废话少说,直接上代码。

2. pom 主要依赖如下:

<dependencies>
    <!-- Spring Boot Web 依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Spring Boot Test 依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- spring-boot整合mybatis -->
    <dependency>
       <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.2</version>
    </dependency>
    <!-- mysql驱动 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <!-- alibaba的druid数据库连接池 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.1.0</version>
    </dependency>
</dependencies>

3. 配置文件:

application.properties( 或 .yml 文件)

 server.context-path=/
 server.port=8080
 # 数据库连接信息
 spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8
 spring.datasource.username=root
 spring.datasource.password=root
 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
 # 使用druid数据源
 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

 ## Mybatis 配置
 mybatis.typeAliasesPackage=com.coolron.*.domain
 mybatis.mapperLocations=classpath:mapping/*/*.xml

4. 入口类:

// mapper 接口类扫描包配置
@MapperScan("com.coolron.*.dao")
@SpringBootApplication
public class SpringbootApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }
}

@MapperScan 注解:指定 mapper(dao) 接口对应的包位置。若未配置会出现如下异常:

***************************
APPLICATION FAILED TO START
***************************
Description:
Field mapper in com.coolron.user.service.impl.UserServiceImpl required a bean of type 'com.coolron.user.dao.UserMapper' that could not be found.

此处应特别注意入口类 SpringbootApplication 所在包的位置。因为包扫描需要自定义,否则扫描到的只是入口类所在的包,或者入口类所在包的下一级。访问资源报错 404。

方案一:
入口类注解指定包扫面位置:

@SpringBootApplication(scanBasePackages = "com.coolron")

方案二 :
模块入口类放到所有需要扫描到的类的上级包中。

5. controller测试:

 @RestController
 @RequestMapping(value = "user")
 public class UserController {
 
     @Autowired
     private UserService userService;
 
     /**
      * 通过用户 id 获取用户信息
     * @param id  用户id
     * @return
     */
    @GetMapping(value = "getInfo/{id}")
    public User getInfo(@PathVariable("id") Integer id) {
        return userService.getInfo(id);
    }
}

6. service(service接口省略):

 @Service
 public class UserServiceImpl implements UserService {
 
     @Autowired
     private UserMapper mapper;
 
     @Override
     public User getInfo(Integer id) {
         return mapper.selectByPrimaryKey(id);
    }
}

7. mapper 以及mapper.xml :

public interface UserMapper {
    User selectByPrimaryKey(Integer id);
}
 <?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.coolron.user.dao.UserMapper">
   <resultMap id="BaseResultMap" type="com.coolron.user.domain.User">
     <id column="id" jdbcType="INTEGER" property="id" />
     <result column="age" jdbcType="INTEGER" property="age" />
     <result column="name" jdbcType="VARCHAR" property="name" />
     <result column="password" jdbcType="VARCHAR" property="password" />
     <result column="description" jdbcType="VARCHAR" property="description" />
     <result column="cityId" jdbcType="INTEGER" property="cityId" />
   </resultMap>
   <sql id="Base_Column_List">
     id, age, name, password, description, cityId
   </sql>
   <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
     select 
     <include refid="Base_Column_List" />
     from user
     where id = #{id,jdbcType=INTEGER}
   </select>
 </mapper>

8. 项目启动、测试:

同第一章运行入口类 main 函数即可启动。

示例代码可参考:greenwich-sr4-user_auth项目

  https://github.com/wjqhuaxia/springcloud-greenwich-sr4

 

  

  

posted @ 2019-12-28 22:07  天际星痕  阅读(1110)  评论(0编辑  收藏  举报