服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - SpringBoot整合数据库访问层的实战

SpringBoot整合数据库访问层的实战

2022-10-18 15:03无休止符 Java教程

本文主要介绍了SpringBoot整合数据库访问层的实战,主要包含JdbcTemplate和mybatis框架的整合应用,具有一定的参考价值,感兴趣的可以了解一下

一、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

SpringBoot整合数据库访问层的实战

SpringBoot整合数据库访问层的实战

 

二、整合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

SpringBoot整合数据库访问层的实战

 

三、整合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整合数据库访问层的实战的文章就介绍到这了,更多相关SpringBoot 数据库访问层内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq23001186/article/details/123662605

延伸 · 阅读

精彩推荐