spring boot之整合MyBatis08
程序员文章站
2022-05-23 15:05:43
...
spring boot之整合MyBatis08
通过快速创建spring boot 项目选中 MySQL,jdbc,mybatis选项
mybaits依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
步骤:
1)、配置数据源相关属性(见上一节Druid)
2)、给数据库建表
3)、创建JavaBean实体类
package com.atguigu.domian;
import java.io.Serializable;
public class User implements Serializable {
private Integer id;
private String username;
private String password;
private String name;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", name='" + name + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
创建dao接口(注解版)
package com.atguigu.dao;
import com.atguigu.domian.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserDao {
@Select("select * from user")
public List<User> findAll();
@Select("select * from user where id=#{id}")
public User findById(Integer id);
}
创建控制器类来进行操作返回响应到页面上
package com.atguigu.controller;
import com.atguigu.dao.UserDao;
import com.atguigu.domian.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 MybatisController {
@Autowired
UserDao userDao;
@GetMapping("qu")
public List<User> findAll(){
return userDao.findAll();
}
@GetMapping("/id")
public User findAllById(Integer id){
return userDao.findById(1);
}
}
通过get请求将数据库的数据查询返回到页面上
下一篇: php绘制一条弧线的方法,php绘制弧线
推荐阅读