目录创建一个SpringBoot项目创建Mysql表编写实体类配置Mapper感叹创建一个springBoot项目 其他不赘叙了,引入MyBaties、mysql依赖 创建mysq
其他不赘叙了,引入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);
}
}
效果
记得当初学MySQL的时候,编写sql语句非常的熟练,现在非常僵硬,认真写博客真的不失为一种复习的好方法。还有当初学SSM的时候,没好好学,没有扎扎实实的学,现在也挺后悔的。快速和详细真的不能兼得。别人的建议可以听听,但一定要坚守自己的内心。倚天照海花无数,流水高山心自知。
到此这篇关于SpringBoot图文并茂详解如何引入mybatis与连接Mysql数据库的文章就介绍到这了,更多相关SpringBoot引入mybatis内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: SpringBoot图文并茂详解如何引入mybatis与连接Mysql数据库
本文链接: https://lsjlt.com/news/164528.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0