本文主要是【Mybatis】——Mybatis即Mybatis-Plus使用的文章,如果有什么需要改进的地方还请大佬指出⛺️
作者简介:大家好,我是听风与他
☁️博客首页:CSDN主页听风与他
每日一句:狠狠沉淀,顶峰相见
导入mybatis依赖
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>3.0.2version>
dependency>
在resource下配置和java目录下UserMapper一样的目录结构作接口映射
配置application.yml文件的数据库url及账号密码,采用本地数据库lianxi
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/lianxi
username: root
password: 020911
实体类User
package com.testmybatismp.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private int id;
private String name;
private int age;
private String password;
}
增删改查接口UserMapper
package com.testmybatismp.mapper;
import com.testmybatismp.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
List<User> findAll();
int addUser(User user);
int deleteUser(int id);
int updateUser(User user);
User findUserById(int id);
}
映射文件UserMapper.xml
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.testmybatismp.mapper.UserMapper">
<insert id="addUser">
insert into user(name,age,password) values (#{name},#{age},#{password})
insert>
<update id="updateUser">
update user set name=#{name},age=#{age},password = #{password}
where id = #{id}
update>
<delete id="deleteUser">
delete from user where id = #{id}
delete>
<select id="findAll" resultType="com.testmybatismp.pojo.User">
select * from user;
select>
<select id="findUserById" resultType="com.testmybatismp.pojo.User">
select * from user where id = #{id}
select>
mapper>
测试时注入为JavaBean对象进行测试,如修改
package com.testmybatismp;
import com.testmybatismp.mapper.UserMapper;
import com.testmybatismp.pojo.User;
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 TestMybatisMpApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
void contextLoads() {
User user = new User(0,"王五",18,"123456");
int i = userMapper.addUser(user);
System.out.println("i:"+i);
}
}
引入Mybatis-Plus框架
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.5.3.1version>
dependency>
实体类User1:
package com.testmybatismp.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("user1") //设置数据库的表名
public class User1 {
@TableId(type = IdType.AUTO) //设置主键自增
private int id;
private String username;
private String password;
}
在测试类中测试增删改查
package com.testmybatismp;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.testmybatismp.mapper.User1Mapper;
import com.testmybatismp.mapper.UserMapper;
import com.testmybatismp.pojo.User;
import com.testmybatismp.pojo.User1;
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 TestMybatisMpApplicationTests {
@Autowired
private User1Mapper user1Mapper; //将user1Mapper对象注入,方便调用
@Test
void contextLoads() {
//添加用户
User1 user1 = new User1(0,"王五","123");
user1Mapper.insert(user1);
//删除用户
user1Mapper.deleteById(2);
//修改用户
User1 user2 = new User1(1,"张三","12345");
user1Mapper.updateById(user2);
//查询全部用户
List<User1> list = user1Mapper.selectList(null);
list.forEach(System.out::println);
//查询用户
QueryWrapper<User1> q = new QueryWrapper<>();
q.eq("username","张三");
User1 one = user1Mapper.selectOne(q);
System.out.println(one.toString());
}
}