前言:
此时小伙伴们对“springmybatis怎么读”大约比较重视,各位老铁们都想要知道一些“springmybatis怎么读”的相关资讯。那么小编同时在网摘上网罗了一些对于“springmybatis怎么读””的相关文章,希望姐妹们能喜欢,大家快快来学习一下吧!1.官网
MyBatis-Plus
可查看官方文档
官网地址:
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.xmlXML
Spring MVC 项目如下
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>3.5.3.1</version> </dependency>XML
<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>XML3.配置(必须)
4.Model
@TableName("account")public class Account { private Integer id; @TableField("name") private String name; private BigDecimal money;}Java5.使用
@Mapperpublic interface AccountMapper extends BaseMapper<Account> {}Java6.使用单元测试测试
<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); }Java结尾
MyBatis-Plus 官方文档:
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.使用
@Mapperpublic 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); }
结尾
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。
标签: #springmybatis怎么读