SpringBoot整合Mybatis

先附上此次整合的github地址链接:(方便以后看)

https://github.com/201705010201/SpringBoot-Mybatis

1. 在新建项目时,要注意一下勾选的。

web这时候也可以勾选上,或者在pom.xml文件中引入web的依赖。

创建项目的时候要特别注意上述勾选的。要不然会连接不上数据库。

2. 导入mybatis所需要的依赖

  <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
  </dependency>

3. 配置数据库连接信息,IDEA连接数据库

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

IDEA连接数据库,点击右侧的Database,填写数据库的User,和Password,这时候要是报错如

Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' property manually

请参考https://blog.csdn.net/hello_cmy/article/details/104296899

选择你要连接的数据库。Apply,OK

再回到General栏,有一个Test Connection,点击,可以测试一下。

连接之后你可以再次点击Database,就可以你连接的数据库信息,如图

4,创建实体类

package com.cc.pojo;

import lombok.*;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
public class User {

    private Integer id;
    private String username;
    private String password;

}

5.配置Mapper接口类

package com.cc.mapper;

import com.cc.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

//@Mapper : 表示本类是一个 MyBatis 的 Mapper,等价于以前 Spring 整合 MyBatis 时的 Mapper 接口
@Mapper
@Repository
public interface UserMapper {
    //选择全部用户
    List<User> selectAllUser();

    //根据id选择用户
    User selectUserById(int id);

    //增加用户
    int addUser(User user);

    //修改用户
    int updateUser(User user);

    //删除用户
    int deleteUser(int id);

}

6. Mapper映射文件

<?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映射文件-->
<mapper namespace="com.cc.mapper.UserMapper">
    <select id="selectAllUser"  resultType="User">
        select * from mybatis.user
    </select>

    <select id="selectUserById" resultType="User">
        select * from mybatis.user where id = #{id}
    </select>

    <insert id="addUser" parameterType="User">
        insert into mybatis.user(id, username, password)
        values (#{id}, #{username}, #{password})
    </insert>

    <update id="updateUser" parameterType="User">
        update mybatis.user set username=#{username},password=#{password} where id=#{id}
    </update>

    <delete id="deleteUser" parameterType="int">
        delete from mybatis.user where id=#{id}
    </delete>
</mapper>

7.SpringBoot 整合!

既然已经提供了 myBatis 的映射配置文件,自然要告诉 spring boot 这些文件的位置

#整合mybatis
#注意:对应实体类的路径,类型别名
mybatis.type-aliases-package=com.cc.pojo

#指定mybatis的核心配置文件与mapper映射文件
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

 spring boot 官方并没有提供 myBaits 的启动器,是 myBatis 官方提供的开发包来适配的 spring boot,从 pom.xml 文件中的依赖包名也能看出来,并非是以 spring-boot 开头的;

同理上面全局配置文件中的这两行配置也是以 mybatis 开头 而非 spring 开头也充分说明这些都是 myBatis 官方提供的

可以从 org.mybatis.spring.boot.autoconfigure.MybatisProperties 中查看所有配置项。

也可以直接去查看官方文档

8.编写controller

package com.cc.controller;

import com.cc.mapper.UserMapper;
import com.cc.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    UserMapper userMapper;

    //选择全部用户
    @GetMapping("/selectUser")
    public String selectAllUser(){
        List<User> users = userMapper.selectAllUser();

        for (User user : users) {
            System.out.println(user);
        }
        return "users";
    }

    //根据id选择用户
    @GetMapping("/selectUserById")
    public String selectUserById(){
        User user = userMapper.selectUserById(1);
        System.out.println(user);
        return "ok";
    }

    //添加一个用户
    @GetMapping("/addUser")
    public String addUser(){
        userMapper.addUser(new User(4,"moon","123"));
        return "ok";
    }

    //修改一个用户
    @GetMapping("/updateUser")
    public String updateUser(){
        userMapper.updateUser(new User(4,"小花","123"));
        return "ok";
    }

    //根据id删除用户
    @GetMapping("/deleteUser")
    public String deleteUser(){
        userMapper.deleteUser(3);
        return "ok";
    }
}

运行测试。

这里就不展示测试的结果了。

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值