springboot 整合mybatis
-
首先手动引入起步依赖(因为springboot没有内嵌的起步依赖)
<!--mybatis起步依赖--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--mybatis起步依赖结束 --> </dependencies>
-
编写实体类(根据表的自定义)
package com.example.springboot_mybatis.domain; public class User { private Integer id; private String username; private String password; private String address; private String sex; private Integer age; 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 getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", address='" + address + '\'' + ", sex='" + sex + '\'' + ", age=" + age + '}'; } }
-
创建dao(个人习惯把sql语句写在此处),在resources下创建mapper.xml用于扫描dao
package com.example.springboot_mybatis.mapper; import com.example.springboot_mybatis.domain.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import java.util.List; @Mapper public interface UserMapper { @Select("select * from user ") public List<User> getUser(); }
<?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 namespace="com.example.springboot_mybatis.mapper"> </mapper>
-
配置application.yml扫描resourcesmapper包下的mapper.xml和数据库信息
#数据库连接信息 spring: datasource: username: root password: root url: jdbc:mysql://localhost:3306/php?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT driver-class-name: com.mysql.cj.jdbc.Driver #配置mybatis信息 mybatis: type-aliases-package: com.example.springboot_mybatis.domain mapper-locations: classpath:mapper/*Mapper.xml
-
编写控制器测试是否获取到数据数据
package com.example.springboot_mybatis.controller; import com.example.springboot_mybatis.domain.User; import com.example.springboot_mybatis.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class MybatisController { @Autowired private UserMapper userMapper; @RequestMapping("query") public List<User> quertUserList() { return userMapper.getUser(); } }
spring boot整合junit
-
起步依赖在用idea创建时已经自动导入了
-
直接编写controller测试是否可以使用
package com.example.springboot_mybatis; import com.example.springboot_mybatis.mapper.UserMapper; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes = SpringbootMybatisApplication.class) public class MybatisTest { @Autowired private UserMapper userMapper; @Test public void test() { System.out.println(userMapper.getUser()); } }