2024.09.27

今天学习了如何使用Mybatis实现增删改查 为此做一个梳理发表一篇博客,也是为了总结一下 首先,要使用Mybatis需要添加依赖 从建立项目的时候选择java8+Springboot2这样的方式,避免高版本存在一些兼容性的问题 然后添加Mybatis-plus依赖,老师讲的是添加了plus会自动添加Mybatis的依赖,但是很遗憾我后续出现了报错因此还是手动添加了
复制代码
复制代码
 
  <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.4.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.0.7</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.5</version>
        </dependency>
复制代码
复制代码

四个依赖 从上到下分别是 Mybatis-plus 、mysql、Mybatis、德鲁伊连接池

单独解释一下德鲁伊,说白了就是对数据库进行多次链接,提高连接的效率,然后报错的时候你就会发现,它会不断的报错,就像写了一个循环不断执行连接一样

然后配置properties

复制代码
 
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis??useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=****
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
复制代码

这里第三行如果写userUnicode=fasle会出现报错 因此我对编码进行了修改

然后需要对主类写一个@MapperScan的注解,注解的内容是要用到的接口所在的包

建议使用全名,这个是用于扫描mapper实现mybatis的操作的,因此路径不能错

 然后创建mapper和controller包,还是先写controller类

controller类中 首先添加@RestController注解,然后就可以等待实现一些mybatis的方法了

然后建议先创建entity包,用以把数据封装成对象,这样使用起来比较方便

建议创建的名称与数据库中一致,值得注意的是 Springboot中存在驼峰命名的规则

因此如果你的数据库中名称为day_id

在实体类中需要写dayId。

然后创建Mapper类

对Mapper类添加一个@Mapper的注解,意味着这是一个Mapper主键,上面在主类中所添加的@MapperScan注解就是为了在这里扫描到这个Mapper注解

然后就可以创建方法了

这里创建方法基于Mybatis就变得十分容易,只需要创建方法名称,然后添加对应接口就可以

例如

实现查询操作

 
@Select("select * from user")
public List<User> find();

这样就完成了接口的创建,去到Controller类中就可以实现对它的使用了

复制代码
复制代码
 
@GetMapping("/user")
        public List query()
        {
            List<User> list = userMapper.find();
            System.out.println(list);
            return list;
        }
复制代码
复制代码

这里单独说一下,接口类是无法被实例化的,它是由spring自动实例化的,因此只需要在Controller类中添加一个

 
@Autowired
    private UserMapper userMapper;

就可以调用userMapper这个对象中的方法了

其他的三个方法也是类似的,单独说一下的是,如果你使用的是对象,在sql语句中需要写成#{}的格式,这样mybatis会自动从你所传入的参数中寻找和它一致的进行注入,但是如果不一致则需要进行注解和映射的配置,建议还是保持一致避免麻烦

复制代码
 
 @Insert("insert into user values(#{id},#{username},#{password},#{birthday})")
        public int insert(User user);
    @Delete("delete from user where id = #{id}")
        public int delete(String id);
    @Update("update user set username = #{username},password = #{password},birthday = #{birthday} where id = #{id}")
        public int update (User user);
复制代码

 

复制代码
复制代码
 
@PostMapping("/user")
        public String insert(User user)
        {
            int flag = 0;
            flag = userMapper.insert(user);
            if(flag>0)
            {
                return "插入成功";
            }else
            {
                return "插入失败";
            }

        }
        @PostMapping("/delete")
        public String delete(String id)
        {
            int flag = 0;
            flag = userMapper.delete(id);
            if(flag>0)
            {
                return "删除成功";
            }else
            {
                return "删除失败";
            }
        }
        @PostMapping("/update")
        public String update(User user)
        {
            int flag = 0;
            flag = userMapper.update(user);
            if(flag>0)
            {
                return "更新成功";
            }else
            {
                return "更新失败";
            }

        }
复制代码
复制代码

然后需要传入参数的基本上都是post方法,当然写RequestMapping(value = "/",method =RequestMethod.POST)也是一样的

通过Apipost软件可以发送post请求,省去了创建form表单的过程。

posted @   new菜鸟  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示