springboot集成mybatis(简单明了)

 

以测试为例,需要以下几步

 

1.准备数据库环境(MySQL)

  在MySQL中创建test数据库,并建立users数据表,然后插入几条数据以备测试

 

create database test;

use test;

CREATE TABLE `users` (
   `id` varchar(11) NOT NULL,
   `name` varchar(12) DEFAULT NULL,
   `password` varchar(12) DEFAULT NULL,
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8

insert into users VALUES('1','t1','123456');
insert into users VALUES('2','t2','123123');
insert into users VALUES('3','t3','111222');

    2.创建springboot项目,此处省略创建过程

在pom.xml中添加相应依赖

        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>  
        </dependency>
        <!--Mybatis启动器-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>

3.编写配置文件

主要为配置数据库连接

#配置Mysql
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root

4.创建实体类,在(src.main.java下创建example.model.User)    

public class User {
    private Integer id;
    private String name;
    private String password;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

}

5.编写Mapper ,在(src.main.java下创建example.mapper.UserMapper)

import com.example.model.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserMapper {

    //查询所有用户
    @Select("select * from users")
    List<User> getAllUser();

    //删除用户
    @Delete("DELETE FROM USERS WHERE id=#{id}")
    void delete(Integer id);
}

6.编写Service层 ,在 (src.main.java下创建example.service.UserService)

import com.example.model.User;

import java.util.List;

public interface UserService {
    List<User> getAllUser();
    void delete(Integer id);
}

7.编写Service的实现类  ,在 (src.main.java下创建example.service.Impl.UserServiceImpl)


import com.example.mapper.UserMapper;
import com.example.model.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class UserServiceImp  implements UserService {

    //注入UserMapper
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> getAllUser() {
        return userMapper.getAllUser();
    }

    @Override
    public void delete(Integer id) {
        System.out.println("删除了id为"+id+"的用户");
        userMapper.delete(id);
    }
}

8.编写Controller ,在 (src.main.java下创建example.controller.UserController)


import com.example.model.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UsersController {

    //注入UserService
    @Autowired
    private UserService userService;

    @RequestMapping("/getAll")
    //添加此注解表明返回json字符串
    @ResponseBody
    public String getAll(){
        List<User> users = userService.getAllUser();
        //控制台可见
        System.out.println(users);

        return users.toString();
    }
}

9.启动项目,在浏览器中输入http://localhost:8080/user/getAll

显示效果如下

 

至此,springboot集成Mybatis成功.

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值