您好,登录后才能下订单哦!
这篇文章主要介绍“SpringBoot怎么引入mybatis与连接Mysql数据库”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“SpringBoot怎么引入mybatis与连接Mysql数据库”文章能帮助大家解决问题。
其他不赘叙了,引入MyBaties、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
方式一,创建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); } }
效果
关于“SpringBoot怎么引入mybatis与连接Mysql数据库”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。