SpringBoot使用mybatis步骤总结

SpringBoot使用mybatis 1.导入 MyBatis 所需要的依赖

org.mybatis.spring.bootmybatis-spring-boot-starter2.1.4 mysqlmysql-connector-java5.1.47runtimeorg.springframework.bootspring-boot-starter-web

2.配置数据库连接信息(application.properties)
spring.datasource.username=rootspring.datasource.password=123spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8spring.datasource.driver-class-name=com.mysql.jdbc.Driver

测试:
@SpringBootTestclass Springboot05MybatisApplicationTests { @AutowiredDataSource dataSource; @Testvoid contextLoads() throws SQLException { System.out.println(dataSource.getClass()); System.out.println(dataSource.getConnection()); } }

打印出:class com.zaxxer.hikari.HikariDataSource
HikariProxyConnection@1172943149 wrapping com.mysql.jdbc.JDBC4Connection@3dc95b8b
说明配置完成
3.然后进行crud操作:
先编写一个实体类:(注意这里使用了lombok插件)
SpringBoot使用mybatis步骤总结
文章图片

import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; @Data@AllArgsConstructor@NoArgsConstructorpublic class User { private int id; private String name; private String pwd; }

创建mapper目录以及对应的 Mapper 接口
SpringBoot使用mybatis步骤总结
文章图片

import com.chen.pojo.User; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; //这个注解表示了这时一个mybatis的mapper类:Dao@Mapper @Repositorypublic interface UserMapper { List queryUserList(); User queryUserById(int id); int addUSer(User user); int updateUser(User user); int deleteUser(int id); }

对应的Mapper映射文件
SpringBoot使用mybatis步骤总结
文章图片

select * from mybatis.user; select *from mybatis.user where id=#{id}; insert into mybatis.user(name,pwd) values (#{name},#{pwd}); update mybatis.user set name =#{name},pwd=#{pwd} where id=#{id}; delete from mybatis.user where id=#{id};

编写用户的UserController 进行测试!
SpringBoot使用mybatis步骤总结
文章图片

import com.chen.mapper.UserMapper; import com.chen.pojo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestControllerpublic class UserController { @Autowiredprivate UserMapper userMapper; @GetMapping("/queryUserList")public List queryUserList(){List userList=userMapper.queryUserList(); for(User user:userList){System.out.println(user); }returnuserList; } //添加一个用户@GetMapping("/addUser")public String addUser(){int n=userMapper.addUSer(new User(5,"阿毛","4566977")); return String.valueOf(n); } //修改一个用户@GetMapping("/updateUser")public String updateUser(){int n=userMapper.updateUser(new User(5,"阿毛","4566977")); return String.valueOf(n); } //删除一个用户@GetMapping("/deleteUser")public String deleteUser(){int n=userMapper.deleteUser(18); return String.valueOf(n); } }

最后创建该实体类数据库:mybatis
DROP TABLE IF EXISTS `user`; CREATE TABLE `user`(`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(25) CHARACTER SET gbk COLLATE gbk_chinese_ci NULL DEFAULT NULL,`pwd` varchar(25) CHARACTER SET gbk COLLATE gbk_chinese_ci NULL DEFAULT NULL,PRIMARY KEY (`id`) USING BTREE) ENGINE = InnoDB AUTO_INCREMENT = 18 CHARACTER SET = gbk COLLATE = gbk_chinese_ci ROW_FORMAT = Compact; -- ------------------------------ Records of user-- ----------------------------INSERT INTO `user` VALUES (3, '小黑', '789'); INSERT INTO `user` VALUES (4, '可可2', '151'); INSERT INTO `user` VALUES (5, '阿毛', '4566977'); INSERT INTO `user` VALUES (6, '小明', 'cafa1414f'); INSERT INTO `user` VALUES (7, '嘻嘻3', '101112'); INSERT INTO `user` VALUES (8, '很爱很爱你', '101112'); INSERT INTO `user` VALUES (9, '小霞', '159'); INSERT INTO `user` VALUES (10, '嘻嘻5', '101112'); INSERT INTO `user` VALUES (11, '陈锦贤', 'dad45'); INSERT INTO `user` VALUES (12, '陈锦贤2', 'd2ad45'); INSERT INTO `user` VALUES (13, '小明', '123456'); INSERT INTO `user` VALUES (14, '小明2', '456'); INSERT INTO `user` VALUES (17, '阿毛', '4566977'); SET FOREIGN_KEY_CHECKS = 1;

然后就可以启动项目进行测试了:
浏览器打开:
http://localhost:8080/queryUserList

【SpringBoot使用mybatis步骤总结】SpringBoot使用mybatis步骤总结
文章图片

其它的删除,添加,修改也是如此!
到此这篇关于SpringBoot使用mybatis步骤总结的文章就介绍到这了,更多相关SpringBoot使用mybatis内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读