SpringBoot(三):整合MyBatis

/ SpringBoot / 2 条评论 / 377人围观

SpringBoot整合MyBatis简直不要太简单,仅需三步!

引入jar包

首先必须引入相关jar包,包括数据库连接,查看对应版本

        <!--mysql连接驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.45</version>
        </dependency>

        <!-- 引入mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        
        <!-- 使用德鲁伊连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.18</version>
        </dependency>

修改配置文件

配置类

在application.properties中加入:

// 指定xml和dao接口的位置
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.demo.mapper

// 数据库
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/story?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

启动类加扫描注解:

@MapperScan("com.example.demo.mapper")

编写类测试

项目的目录结构:

请输入图片描述

controller

/**
 * description
 *
 * @author 70KG
 * @date 2018/10/24
 */
@RestController
public class TestMyBatisController {

    @Autowired
    private TestMyBatisService testMyBatisService;

    @RequestMapping("/testmybatis")
    public User getUserById() {
        String id = "1";
        User user = testMyBatisService.getUserById(id);
        return user;
    }

}

service

/**
 * description
 *
 * @author 70KG
 * @date 2018/10/24
 */
public interface TestMyBatisService {
    User getUserById(String id);
}

/**
 * description
 *
 * @author 70KG
 * @date 2018/10/24
 */
@Service
public class TestMyBatisServiceImpl implements TestMyBatisService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserById(String id) {
        User user = userMapper.getUserById(id);
        return user;
    }

}

dao

/**
 * description
 *
 * @author 70KG
 * @date 2018/10/24
 */
public interface UserMapper {
    User getUserById(@Param("id") String id);
}

mapper

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
    <resultMap id="BaseResultMap" type="com.example.demo.Entity.pojo.User">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="username" jdbcType="VARCHAR" property="username" />
        <result column="password" jdbcType="VARCHAR" property="password" />
        <result column="email" jdbcType="VARCHAR" property="email" />
        <result column="home_url" jdbcType="VARCHAR" property="home_url" />
        <result column="screen_name" jdbcType="VARCHAR" property="screen_name" />
        <result column="created" jdbcType="INTEGER" property="created" />
        <result column="activated" jdbcType="INTEGER" property="activated" />
        <result column="logged" jdbcType="INTEGER" property="logged" />
        <result column="group_name" jdbcType="VARCHAR" property="group_name" />
        <result column="status" jdbcType="VARCHAR" property="status" />
    </resultMap>

    <select id="getUserById" resultMap="BaseResultMap">
        SELECT * FROM t_users WHERE id = #{id}
    </select>

</mapper>

地址栏访问 localhost:8080/testmybatis ,ok成功!

  1. Springboot2.0不需要在配置sqlsessionfactory了么

    回复

    @中国-上海网友 默认的配置文件中已经为我们配置好了,不需要再配置了

    回复