整体的工程结构 pom.xml文件 需要添加的 1.这里UserMapper,不需要在配置繁琐的xml文件 2.pojo层里的User,与数据库对应 3.这里注意MybatisPlusApplication这里需要添加扫描mapper文件 4.application.properties的相关配置,与之前没有什么不同 5.测试一下在MybatisPlusApplicationTests 更新操作
数据库文件(mybatis_plus 数据库名字)
DROP TABLE IF EXISTS user; CREATE TABLE user ( id BIGINT(20) NOT NULL COMMENT '主键ID', name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名', age INT(11) NULL DEFAULT NULL COMMENT '年龄', email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱', PRIMARY KEY (id) );INSERT INTO user (id, name, age, email) VALUES (1, 'Jone', 18, 'test1@baomidou.com'), (2, 'Jack', 20, 'test2@baomidou.com'), (3, 'Tom', 28, 'test3@baomidou.com'), (4, 'Sandy', 21, 'test4@baomidou.com'), (5, 'Billie', 24, 'test5@baomidou.com');
<!-- 数据库驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!-- mybatis-plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.5</version> </dependency>
package com.xyj.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.xyj.pojo.User; import org.springframework.stereotype.Repository; /** * @author xyj * @date 2020/4/24 -10:20 */ @Repository public interface UserMapper extends BaseMapper<User>{ }
package com.xyj.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; /** * @author xyj * @date 2020/4/24 -10:17 */ @Data @AllArgsConstructor @NoArgsConstructor public class User { private Long id; private String name; private Integer age; private String email; }
@MapperScan(“com.xyj.mapper”)对应你的包位置package com.xyj; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; //扫描mapper文件夹 @MapperScan("com.xyj.mapper") @SpringBootApplication public class MybatisPlusApplication { public static void main(String[] args) { SpringApplication.run(MybatisPlusApplication.class, args); } }
spring.datasource.username=root spring.datasource.password=123456 spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
package com.xyj; import com.xyj.mapper.UserMapper; import com.xyj.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 MybatisPlusApplicationTests { //继承basemapper,所有方法来自父类 @Autowired private UserMapper userMapper; @Test void contextLoads() { //查询全部 List<User> users = userMapper.selectList(null); users.forEach(System.out::println); } }
只需几步就可以完成CRUD操作
添加操作 @Test public void testInsert(){ User user = new User(); user.setName("xyj"); user.setAge(18); user.setEmail("1550213743@qq.com"); int result = userMapper.insert(user);//帮我们自动生成id System.out.println(result); System.out.println(user); System.out.println(user.getId()); }
//通过条件自动拼接动态sql @Test public void testUpdate(){ User user = new User(); user.setId( 5L); user.setAge(60); user.setName("青青草原"); user.setEmail("qingqingcaoyuan@qq.com"); int result = userMapper.updateById(user); System.out.println(result); }
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算