查漏补缺:mybatis_plus之一
Mybatis是一个优秀的Mybatis插件。
- 首先实现mybatis完成CRUD
//mapper
public interface UserMapper {
List<User> findAll();
}
<mapper namespace="com.boerk.mapper.UserMapper">
<select id="findAll" resultType="com.boerk.pojo.User">
select * from tb_user
</select>
</mapper>
- 在mybatis实现的基础上,让
mapper
继承BaseMapper
public interface UserMapper extends BaseMapper<User> {
List<User> findAll();
}
- 使用BaseMapper的方法来代替mapper接口的方法
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
//这一行需要修改
//mybatis原生
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//mybatisplus
SqlSessionFactory sqlSessionFactory = new MybatisSqlSessionFactoryBuilder().build(inputStream);
//
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//这一行需要修改
//mybatis原生
List<User> all = mapper.findAll();
//mybatisplus,后面应填写查询的条件。
List<User> users = mapper.selectList(null);
//使用@TableName,将实体类名与表名进行映射
基于这种方式,mapper接口是实际上不需要任何方法,可以完全依赖basemapper中的方法。映射文件也不需要。
- 使用spring与mybatisplus整合时,将数sqlSessionFactoryBean的class路径修改
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
当需要使用spring进行测试的时候,可以使用注解。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class TestSpring {
}
- springboot整合mybatisplus时…
- 引入依赖
- 编写配置文件(主要是数据源信息)
- 测试
springboot测试时,需要从容器中拿组件的时候需要加上@SpringBootTest
注解
@RunWith(SpringRunner.class)//测试结果表明这个可以不写。
@SpringBootTest
class Demo2ApplicationTests {
@Autowired
UserMapper userMapper;
@Test
void contextLoads() {
List<User> users = this.userMapper.selectList(null);
for (User user : users) {
System.out.println(user);
}
}
}
//主程序需要加上@MapperScan注解,来指定扫描的mapper接口配置。
@MapperScan("com.boerk.mapper")
@SpringBootApplication
public class Demo2Application {
public static void main(String[] args) {
SpringApplication.run(Demo2Application.class, args);
}
}
使用@TableId注解然后指定主键策略,默认为none即不设置主键。使用auto为自增长。
使用@TableField注解可以解决表中字段名和类中属性不一致的问题。
@TableField(value = "email")
private String mail;
也可以解决特定字段不存在于表中的问题
@TableField(exist = false)
private String address;
查询时不希望返回的数据使用@TableField(select=false)
根据条件进行更新数据时
- 根据id进行更新时,将id和需要更新的数据封装成一个对象。
void contextLoads() {
User user = new User();
user.setId(1L);
user.setAge(20);
int i = userMapper.updateById(user);
System.out.println("result=>"+i);
}
- 根据其他属性进行更新时
void contextLoads() {
//将名字叫张三的人的密码改为666666
User user = new User();
user.setPassword("666666");
QueryWrapper<User> userQueryWrapper = new QueryWrapper<User>();
//这个是字段名而非属性名。
userQueryWrapper.eq("user_name","zhangsan");
userMapper.update(user,userQueryWrapper);
}
或者也可以使用UpdateWrapper
void contextLoads() {
//将名字叫张三的人的密码改为111111
UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
userUpdateWrapper.set("password","111111").eq("user_name","zhangsan");
userMapper.update(null,userUpdateWrapper);
}
删除数据
- 通过主键删除
void contextLoads() {
int result = userMapper.deleteById(5L);
System.out.println("result"+result);
}
- 多条件删除,通过一个map进行封装,多个条件之间是and关系。
void contextLoads() {
//删除密码为123456且年龄为20岁的人
Map<String, Object> map = new HashMap<>();
map.put("password","123456");
map.put("age",20);
int i = userMapper.deleteByMap(map);
System.out.println("受影响的行数:"+i);
}
也可以替换成QueryWrapper并使用delete()方法。
使用count()可以查询有几条数据,并使用QueryWrapper指定条件(GT大于,GE大于或等于,NE是不等于,EQ是等于,LT小于,LE小于或等于)
void contextLoads() {
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
//查询年龄大于或等于20次的人
QueryWrapper<User> age = userQueryWrapper.ge("age", 20);
Integer integer = userMapper.selectCount(userQueryWrapper);
System.out.println(integer);
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库