springboot 整合druid和mybatis

Shrio+Mybatis+Druid

1.导入相关依赖包

View Code

2.在配置文件配置数据源

View Code

3.pojo对应实体类和mapper目录下的接口UserMapper

View Code
View Code

(3.使用注解版)

package com.lian.mapper;
import com.lian.pojo.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface UserMapper {

    @Select("select * from user")
    List<User> getUsers();
    //    通过id查询
    @Select("select * from user where id=#{332}")
    User getUserById(@Param("332") int id);
    @Insert("insert into user(id,name,pwd) values(#{id},#{name},#{pwd})")
    int addUser(User user);
    @Update("UPDATE USER set NAME=#{name},PWD=#{pwd} where id=#{id}")
    int updateUser(User user);
    @Delete("delete from user where id=#{id}")
    int delUser(int id);


}
View Code

 

4.在资源目录下新建UserMapper.xml

View Code

 

5.controller测试

View Code

总结:

在springboot整合mybatis(使用注解偷懒版)
1.下载依赖包:mybatis-spring-boot-starter
2.在配置文件properties或者yaml连接数据库
3.com.lian下新建mapper目录,在mapper目录下写接口
并且用注解的方式在接口上方写对应sql,注意点:记得在接口上方写注解@Repository和@Mapper
4.controller目录下写测试类,注意点:记得在mapper上方自动装配@Autowired

 


在springboot整合mybatis(使用xml文件)

1.下载依赖包:mybatis-spring-boot-starter
2.在配置文件properties或者yaml连接数据库
3.com.lian下新建mapper目录,在mapper目录下写接口

4.在资源目录下写xml配置文件

5.controller目录下写测试类,注意点:记得在mapper上方自动装配@Autowired

posted on 2023-05-10 20:34  醒醒起来  阅读(148)  评论(0)    收藏  举报