亲宝软件园·资讯

展开

SpringBoot引入mybatis

游坦之 人气:0

创建一个SpringBoot项目

其他不赘叙了,引入MyBaties、MySql依赖

创建mysql表

CREATE TABLE sp_users(
	`id` INT PRIMARY KEY,
	`username` VARCHAR(30),
	`age` INT
);

刚开始一直出现这个错误,弄的我怀疑人生,结果是最后一行不能加',' ,物是人非。

INSERT INTO sp_users(id,`username`,`age`) VALUES(1,"张三",11);
INSERT INTO sp_users(id,`username`,`age`) VALUES(2,"李四",21);
INSERT INTO sp_users(id,`username`,`age`) VALUES(3,"游坦之",800); 

编写实体类

因为引入了Lombok,所以直接用了

package com.you.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class user {
    private int id;
    private String username;
    private int age;
}

配置application.yaml

#datasource
spring:
  datasource:
    url: jdbc:mysql:///springboot?serverTimezone=UTC
    username: root
    password: 你的密码
    driver-class-name: com.mysql.cj.jdbc.Driver

配置Mapper

方式一,创建UserMapper接口

package com.you.mapper;
import com.you.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserMapper {
    @Select("select * from sp_users")
    public List<User> findAll();
}

配置测试类

package com.you.boot;
import com.you.boot.domain.User;
import com.you.boot.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class BootMybatis03ApplicationTests {
    @Autowired
    private UserMapper userMapper;
    @Test
    public void testFindAll()
    {
        List<User> list = userMapper.findAll();
        System.out.println(list);
    }
}

效果图

方法二 XML文件配置

编写xml文件,namespace一定要复制全路径,Copy/Copy Reference

<?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.you.boot.mapper.UserXmlMapper">
    <select id="findAll" resultType="user">
        select * from sp_users
    </select>
</mapper>

编写UserXmlMapper接口,@Repository的作用是解决在测试类内爆红,虽然爆红不影响程序的运行。

package com.you.boot.mapper;
import com.you.boot.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserXmlMapper {
    public List<User> findAll();
}

配置yaml,*Mapper值得是所有后缀是Mapper的xml文件

mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml
  type-aliases-package: com.you.boot.domain

编写测试类

package com.you.boot;
import com.you.boot.domain.User;
import com.you.boot.mapper.UserMapper;
import com.you.boot.mapper.UserXmlMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class BootMybatis03ApplicationTests {
    @Autowired
    private UserXmlMapper userXmlMapper;
    @Test
    public void testFindAll2()
    {
        List<User> list = userXmlMapper.findAll();
        System.out.println(list);
    }
}

效果

感叹

记得当初学MySQL的时候,编写sql语句非常的熟练,现在非常僵硬,认真写博客真的不失为一种复习的好方法。还有当初学SSM的时候,没好好学,没有扎扎实实的学,现在也挺后悔的。快速和详细真的不能兼得。别人的建议可以听听,但一定要坚守自己的内心。倚天照海花无数,流水高山心自知。

加载全部内容

相关教程
猜你喜欢
用户评论