构建项目时选好mybatis的依赖
使用映射文件的访问方法
配置连接源信息包括mybatis的配置信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
#指定mapper文件位置
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
#指定各映射数据类的包位置
mybatis.type-aliases-package=com.cakecn.domain
SpringBoot启动类 要注解mybatis的注解扫描包路径
package com.cakecn.springboot_test04;
import com.cakecn.mapper.UserMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = "com.cakecn") //Web控制类的扫描路径
@MapperScan("com.cakecn.mapper") //mybatis映射类的扫描位置
public class SpringbootTest04Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootTest04Application.class, args);
}
}
数据实体类
package com.cakecn.domain;
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int 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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
编写接口类作为mybatis调用SQL的途径
package com.cakecn.mapper;
import com.cakecn.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper //标记是mybatis的映射接口类
@Repository //加上 取消错误误报
public interface UserMapper {
//@Select("select * from t_user") 这里是使用mybatis注解时需要的代码
public List<User> findAll();
}
Mapper.xml文件,配置映射方法对应的SQL语句
namespace
中为映射接口类,表映射文件和映射接口的对应关系
其它内容与springmvc
中的运用一致
<?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.cakecn.mapper.UserMapper">
<select id="findAll" resultType="User">
select * from t_user
</select>
</mapper>
示例控制类
package com.cakecn.controller;
import com.cakecn.domain.User;
import com.cakecn.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 MainController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/save")
public String save(){
List<User> list = userMapper.findAll();
System.out.println(list);
return "success";
}
}
运行访问/save
总的来说,Mybatis在SpringBoot中的应用原理,应该是通过我们去编写映射文件、映射接口、安排他们的对应关系,然后由框架编写“执行SQL和数据映射”的实体类并产生放在框架中供调用
使用注解方式
映射接口类中的那句Select注解
取消注释,后可以取消掉application.prooerties
中的Mybatis配置信息
然后直接运行即可,方便很多