SpringBoot使用mybatis步骤总结

网友投稿 245 2023-01-06

SpringBoot使用mybatis步骤总结

SpringBoot使用mybatis

1.导入 MyBatis 所需要的依赖

org.mybatis.spring.boot

mybatis-spring-boot-starter

2.1.4

mysql

mysql-connector-java

5.1.47

runtime

org.springframework.boot

spring-boot-starter-web

2.配置数据库连接信息(application.properties)

spring.datasource.username=root

spring.datasource.password=123

spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

测试:

@SpringBootTest

class Springboot05MybatisApplicationTests {

@Autowired

DataSource dataSource;

@Test

void 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插件)

import lombok.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

import org.springframework.beans.factory.annotation.Autowired;

@Data

@AllArgsConstructor

@NoArgsConstructor

public class User {

private int id;

private String name;

private String pwd;

}

创建mapper目录以及对应的 Mapper 接口

import com.chen.pojo.User;

import org.apache.ibatis.annotations.Mapper;

import org.springframework.stereotype.Repository;

import java.util.List;

//这个注解表示了这时一个mybatis的mapper类:Dao

@Mapper

@Repository

public interface UserMapper {

List queryUserList();

User queryUserById(int id);

int addUSer(User user);

int updateUser(User user);

int deleteUser(int id);

}

对应的Mapper映射文件

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

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 进行测试!

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;

@RestController

public class UserController {

@Autowired

private UserMapper userMapper;

@GetMapping("/queryUserList")

public List queryUserList(){

List userList=userMapper.queryUserList();

for(User user:userList){

System.out.println(user);

}

return userList;

}

//添加一个用户

@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

其它的删除,添加,修改也是如此!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:解决java字符串转换成时间Unparseable date出错的问题
下一篇:电影网站api接口(电影网站api接口json)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~