006.MyBatis访问PostgreSQL-常规方式

1.引入MyBatis和PostgreSQL依赖

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

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

2.配置数据库链接和使用的驱动

spring.datasource.url=jdbc:postgresql://localhost:5432/javacore
spring.datasource.username=postgres
spring.datasource.password=asdf-1234
spring.datasource.driver-class-name=org.postgresql.Driver

3.创建表+创建自增长ID

-- Table: public.User

-- DROP TABLE IF EXISTS public."User";

CREATE TABLE IF NOT EXISTS public."User"
(
    id bigint NOT NULL,
    name text COLLATE pg_catalog."default",
    age bigint,
    CONSTRAINT "User_pkey" PRIMARY KEY (id)
)

TABLESPACE pg_default;

ALTER TABLE IF EXISTS public."User"
OWNER to postgres;


创建序列
create sequence table_name_id_seq start with 1 increment by 1 no minvalue no maxvalue cache 1;

设置表的ID字段的默认值为nextval(table_name_id_seq)
alter table public."User" alter column id set default nextval('table_name_id_seq');

4.创建实体类

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class User {
    private Long id;

    private String name;
    private Integer age;

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
}

5.创建Mapper

package com.java.core;

import org.apache.ibatis.annotations.*;

@Mapper
public interface UserMapper {

    @Select("SELECT * FROM \"User\" WHERE name = #{name}")
    User findByName(@Param("name") String name);

    @Insert("INSERT INTO \"User\"(name, age) VALUES(#{name}, #{age})")
    int insert(@Param("name") String name, @Param("age") Integer age);

    @Update("UPDATE \"User\" SET age=#{age} WHERE name=#{name}")
    void update(User user);

    @Delete("DELETE FROM \"User\" WHERE id =#{id}")
    void delete(Long id);
}

6.测试调用


@SpringBootTest
class CoreApplicationTests {

    @Autowired
    private UserMapper userMapper;


    @Test
    public void testUserController() throws Exception {

        // insert一条数据,并select出来验证
        userMapper.insert("AAA", 20);
        User u = userMapper.findByName("AAA");

        // update一条数据,并select出来验证
        u.setAge(30);
        userMapper.update(u);
        u = userMapper.findByName("AAA");

        // 删除这条数据,并select验证
        userMapper.delete(u.getId());
        u = userMapper.findByName("AAA");


    }

}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
[INFO] Scanning for projects... [ERROR] [ERROR] Some problems were encountered while processing the POMs: [ERROR] 'dependencies.dependency.version' for org.mybatis.spring.boot:mybatis-spring-boot-starter:jar is missing. @ line 73, column 21 [ERROR] 'dependencies.dependency.version' for org.mybatis.spring.boot:mybatis-spring-boot-starter:jar is missing. @ line 85, column 21 [ERROR] 'dependencies.dependency.version' for org.mybatis.spring.boot:mybatis-spring-boot-starter:jar is missing. @ line 82, column 21 @ [ERROR] The build could not read 3 projects -> [Help 1] [ERROR] [ERROR] The project org.pw:user-service:1.0-SNAPSHOT (C:\workspace\tools\idea\myproject\mysb\user-service\pom.xml) has 1 error [ERROR] 'dependencies.dependency.version' for org.mybatis.spring.boot:mybatis-spring-boot-starter:jar is missing. @ line 73, column 21 [ERROR] [ERROR] The project org.pw:order-service:1.0-SNAPSHOT (C:\workspace\tools\idea\myproject\mysb\order-service\pom.xml) has 1 error [ERROR] 'dependencies.dependency.version' for org.mybatis.spring.boot:mybatis-spring-boot-starter:jar is missing. @ line 85, column 21 [ERROR] [ERROR] The project org.pw:login-service:1.0-SNAPSHOT (C:\workspace\tools\idea\myproject\mysb\login-service\pom.xml) has 1 error [ERROR] 'dependencies.dependency.version' for org.mybatis.spring.boot:mybatis-spring-boot-starter:jar is missing. @ line 82, column 21 [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles:.
06-01

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值