Spring Boot集成Mybatis非常简单,在之前搭建好的项目中加入Mybatis依赖的jar,在配置文件中加入数据库配置即可,如下图所示:

创建对应的Controller、Service、Dao等,代码如下:

User实体类:

package com.example.demo.entity;

public class User {

    private Long id;

    private String name;

    private int age;

    public Long getId()
{
return id;
} public void setId(Long id)
{
this.id = id;
} public String getName()
{
return name;
} public void setName(String name)
{
this.name = name;
} public int getAge()
{
return age;
} public void setAge(int age)
{
this.age = age;
}
}

Dao:

package com.example.demo.dao;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select; import com.example.demo.entity.User; @Mapper
public interface UserDao { @Results({ @Result(property = "id", column = "id"), @Result(property = "name", column = "name"), @Result(property = "age", column = "age") })
@Select("SELECT * FROM user WHERE age = #{age}")
List<User> get(int age); @Insert("INSERT INTO user(id, name, age) VALUES (#{id}, #{name}, #{age})")
void insert(User user);
}

UserService:

package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.example.demo.dao.UserDao;
import com.example.demo.entity.User; @Service
public class UserService { @Autowired
private UserDao userDao; public String show()
{
return "Hello World!";
} public String insert(String name, int age)
{
User user = new User();
user.setId(System.currentTimeMillis());
user.setName(name);
user.setAge(age);
userDao.insert(user);
return "Insert ( \"" + name + "\", age" + age + ") OK!";
}
}

UserController:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.example.demo.service.UserService; @RestController
public class UserController { @Autowired
private UserService userService; @RequestMapping(value = "/show")
public String show()
{
return userService.show();
} @RequestMapping(value = "/insert")
public String insert(String name, int age)
{
return userService.insert(name, age);
}
}

DemoApplication中加入扫描Mapper的注解,修改后的代码如下所示:

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
@MapperScan(basePackages = { "com.example.demo.dao" })
public class DemoApplication { public static void main(String[] args)
{
SpringApplication.run(DemoApplication.class, args);
}
}

启动,访问:http://localhost:8080/insert?name=yyy&age=20 插入数据成功:

集成Mybatis完成。

最新文章

  1. VMware Tools安装小结
  2. eclipse中遇到的小问题
  3. raw socket
  4. POJ 1182 食物链 (三态种类并查集)
  5. OC基础--OC中的类方法和对象方法
  6. 上传项目的更改 info.plist文件
  7. Git教程(3)命令行使用git简单示例
  8. 升级iOS10后SearchController焦点无法获取的问题
  9. javascript 笔记(1)
  10. Android之sqlite 命令
  11. CentOS 6.2 二进制安装apache2.4.3出现configure: error: APR-util not found. Please read the documentation的解决方
  12. MIPS台OpenWrt在系统内的路由器Rust应用程序开发
  13. 自学html5要花多长时间
  14. 在app中屏蔽第三方键盘
  15. Redis中的执行命令的过程
  16. webpack 模块标识符(Module Identifiers)
  17. Rpgmakermv(38)MOG_Theatrhythm
  18. Quidway S系列交换机
  19. Android_EditText 打勾显示输入的密码 --EditText与setTransformationMethod
  20. Linux下解决高并发socket最大连接数限制,tcp默认1024个连接

热门文章

  1. Java日期格式转换不用发愁
  2. nodejs-CommonJS规范
  3. [php反序列化] CVE-2020-15148(Yii2 反序列化漏洞) 漏洞复现
  4. RAC中常见的高级用法-bind方法
  5. transient关键字和volatile关键字
  6. java.util.Collections.copy()方法注意点
  7. js 时间戳转换为年月日时分秒的格式
  8. 【力扣】剑指 Offer 25. 合并两个排序的链表
  9. 干掉visio,这个画图神器太香了
  10. java多线程4:volatile关键字