首頁 > 軟體

SpringBoot整合資料庫存取層的實戰

2022-03-23 13:01:31

一、springboot整合使用JdbcTemplate

1.pom依賴

        <!--SpringBoot整合jdbc模板框架-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!--SpringBoot整合mysql驅動類-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>

2.application.yml新增設定

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver

3.建表sql

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(32) NOT NULL COMMENT '使用者名稱稱',
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

4.UserService

@RestController
public class UserService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping("/insertUser")
    public String insertUser(String name,Integer age){
        int update = jdbcTemplate.update("insert into users values(null,?,?);", name, age);
        return update > 0 ? "success" : "false";
    }
}

5.瀏覽器存取

http://127.0.0.1:8080/insertUser?name=你好&age=21

二、整合mybatis框架查詢

1.pom依賴

    <!-- springboot 整合mybatis -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.1.1</version>
    </dependency>

2.實體類UserEntity

public class UserEntity {
    private String userName;
    private Integer age;
    private Integer id;
	//GET...SET...省略
}

3.UserMapper介面

public interface UserMapper {
    @Select("select id as id,name as userName,age as age from users where id = #{id};")
    UserEntity selectByUserId(@Param("id") Integer id);
}

4.UserService

@RestController
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @RequestMapping("/mybatisFindById")
    public UserEntity mybatisFindById(Integer id){
        return userMapper.selectByUserId(id);
    }
}

5.app啟動

注意:這裡需要加註解@MapperScan("com.sjyl.mapper")來告訴spring介面mapper的掃包範圍

@SpringBootApplication
@MapperScan("com.sjyl.mapper")
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class);
    }
}

測試連線:http://127.0.0.1:8080/mybatisFindById?id=3

三、整合mybatis框架插入

1.UserMapper新增insertUser

public interface UserMapper {

    @Insert("Insert into users values (null,#{userName},#{age});")
    int insertUser(@Param("userName") String userName,@Param("age") Integer age);

    @Select("select id as id,name as userName,age as age from users where id = #{id};")
    UserEntity selectByUserId(@Param("id") Integer id);
}

2.UserService新增insertUserMybatis

@RestController
public class UserService {
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/mybatisFindById")
    public UserEntity mybatisFindById(Integer id){
        return userMapper.selectByUserId(id);
    }

    @RequestMapping("/insertUserMybatis")
    public String insertUserMybatis(String userName,Integer age){
        int insert = userMapper.insertUser(userName,age);
        return insert > 0 ? "success" : "false";
    }
}

測試連線:http://127.0.0.1:8080/insertUserMybatis?userName=%E5%BC%A0%E4%B8%89&age=21

 到此這篇關於SpringBoot整合資料庫存取層的實戰的文章就介紹到這了,更多相關SpringBoot 資料庫存取層內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com