4.MybatisPlus
1.官网
MyBatis-Plus
MyBatis-Plus 官方文档
https://baomidou.com/
2.加入依赖包
Spring Boot项目如下:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
在配置文件加如下配置mybatis-plus.type-aliases-package=com.beiyou.entitymybatis.mapper-locations = classpath:mapper/*Mapper.xml
Spring MVC 项目如下
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.5.3.1</version>
</dependency>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.baomidou.mybatisplus.samples.quickstart.mapper"/>
</bean>
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
3.配置(必须)
4.Model
@TableName("account")
public class Account {
private Integer id;
@TableField("name")
private String name;
private BigDecimal money;
}
5.使用
@Mapper
public interface AccountMapper extends BaseMapper<Account> {
}
Java
6.使用单元测试测试
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.0</version>
<scope>test</scope>
</dependency>
XML
@Autowired
private UserMapper userMapper;
@Autowired
private TukuMapper tukuMapper;
@Test
void test4() {
Tuku tuku1 = new Tuku();
tuku1.setId(3);
tukuMapper.deleteById(tuku1);
tukuMapper.deleteById(2);
tukuMapper.deleteBatchIds(CollUtil.newArrayList(1,2,3));
UpdateWrapper<Tuku> wapper = Wrappers.update();
wapper.eq("id",3);
tukuMapper.delete(wapper);
}
@Test
void test3() {
Tuku tuku1 = new Tuku();
tuku1.setId(3);
tuku1.setImg("艳杰2.gif");
//int i = tukuMapper.updateById(tuku1);
//UpdateWrapper<Tuku> wapper = new UpdateWrapper<>();
UpdateWrapper<Tuku> wapper = Wrappers.update();
wapper.set("img","艳杰300.gif");
wapper.eq("id",3);
//int update = tukuMapper.update(null, wapper);
LambdaUpdateWrapper<Tuku> wapper3 = new LambdaUpdateWrapper<>();
wapper3.set(Tuku::getImg,"艳杰400.gif");
wapper3.eq(Tuku::getId,3);
int update3 = tukuMapper.update(null, wapper3);
}
@Test
void test2() {
QueryWrapper wapper = new QueryWrapper<Tuku>();
//wapper.eq("img","张北.gif");
Integer minId = null;
//where 后面 加这个 id > xx
wapper.ge(minId != null, "id", minId);
wapper.like("img", "强");
LambdaQueryWrapper<Tuku> wapper2 = new LambdaQueryWrapper();
wapper2.like(Tuku::getImg, "强");
List<Tuku> userList = tukuMapper.selectList(wapper2);
userList.forEach(System.out::println);
}
@Test
void test1() {
List<UserEntity> userList = userMapper.selectList(null);
userList.forEach(System.out::println);
}
结尾