SpringBoot 简单使用mybatis
Posted 冷血~多好
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot 简单使用mybatis相关的知识,希望对你有一定的参考价值。
1.导入 MyBatis 所需要的依赖
<!--mybatis-spring-boot-starter:整合-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
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<User> queryUserList();
User queryUserById(int id);
int addUSer(User user);
int updateUser(User user);
int deleteUser(int id);
}
对应的Mapper映射文件
<?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.chen.mapper.UserMapper">
<!--select查询语句-->
<!--id为方法名,resultType为结果返回类型-->
<select id="queryUserList" resultType="User">
select * from mybatis.user;
</select>
<!--通过id进行select查询语句,parameterType为传参类型,resultType为结果返回类型-->
<select id="queryUserById" parameterType="int" resultType="User">
select *from mybatis.user where id=#{id};
</select>
<!--插入语句,对象中的属性,可以直接取出来-->
<insert id="addUSer" parameterType="User" >
insert into mybatis.user(name,pwd) values (#{name},#{pwd});
</insert>
<!--更改用户语句-->
<insert id="updateUser" parameterType="User" >
update mybatis.user set name =#{name},pwd=#{pwd} where id=#{id};
</insert>
<!--删除用户-->
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id=#{id};
</delete>
</mapper>
编写用户的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<User> queryUserList(){
List<User> 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
其它的删除,添加,修改也是如此!
以上是关于SpringBoot 简单使用mybatis的主要内容,如果未能解决你的问题,请参考以下文章
springboot.springboot用最简单的方式整合mybatis
推荐一个基于SpringBoot + MyBatis + Vue 的代码生成器