Java-03-Mybatis

Mybatis

ORM:为了解决面向对象与关系数据库存在的互不匹配现象的一种技术。建立了关系型数据库和对象之间的映射。

Mybatis:是一款优秀的ORM框架。

配置

用到的时候搜吧qa

1.pom.xml添加依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
</dependency>

<!--这是日志的东西,可以不加-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.20</version>
</dependency>

2.添加properties

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://10.1.1.58:6632/study?stringtype=unspecified&ApplicationName=tutorial
spring.datasource.username=tom
spring.datasource.password=123456

3.添加 @MapperScan(#包名) 扫描器。注意包名要填全包名,所以最好不要手动写,而是:右键->复制路径/引用->复制引用

注意 SpringbootMybatis版本适配问题qaaa。Springboot 3.x不能和Mybatis 2.x适配。

报错大概是 Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

Mybatis

数据库操作的java类不需要创建类,创建接口就行。

@Mapper
public interface UserMapper {

}

Mybatis的数据库查询语句写在注释里面。

增删改查范例

@Mapper
public interface UserMapper {

    // 查询所有用户
    @Select("select * from user_test")
    List<User> find();

    // 增加用户,返回值表示插入用户的数量
    // 如果不传递id 的话,记得将数据库id列改成自增模式。因为int默认为0,所以如果不修改,就很容易出现500错误
    // 我没搞懂怎么修改PGSQL的自增orz
    @Insert("INSERT INTO user_test values(#{id},#{name},#{pwd})")
    int insert(User user);

     // 删除
    @Delete("Delete from user_test where id=#{id}")
    int delete(int id);

    // 修改
    @Update("UPDATE user_test set name=#{name},pwd=#{pwd} where id=#{id}")
    int update(User user);
}

controller 中不能实例化,直接使用就行。

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

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

    @PostMapping("/user")
    public String save(User user){
        int i = userMapper.insert(user);

        if(i>0) {
            return "插入成功";
        }else {
            return "插入失败";
        }
    }

    @RequestMapping(value = "/user",method = RequestMethod.PUT)
    public String update(User user){
        int i = userMapper.update(user);

        if(i>0) {
            return "修改成功";
        }else {
            return "修改失败";
        }
    }

    @RequestMapping(value = "/user/{id}",method = RequestMethod.DELETE)
    public String delete(@PathVariable int id){
        int i = userMapper.delete(id);

        if(i>0) {
            return "删除成功";
        }else {
            return "删除失败";
        }
    }

}

以上内容,Mybatis-plus可以通过继承 BaseMapper完成。

//前提是数据库中的表名和类名相同,即都叫 user
public interface UserMapper extends BaseMapper<User>{

}

Mybatis-plus还提供了一些注解

// 类和表名不一样时,做对应
@TableName("user_test")
public class User {
    private int id;

    // 列名和属性名不一样时,做对应
    @TableField("nickname")
    private String name;
    private String pwd;

    // 不想映射
    @TableField(exist = False)
    private int age;
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇