springboot框架搭建-访问数据库

2021年11月20日 阅读数:5
这篇文章主要向大家介绍springboot框架搭建-访问数据库,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

springboot框架搭建java

1)访问springboot官网,填好相关信息后,点击左下角generate,生成springboot项目文件,而后直接用idea直接打开该项目,构建好springboot框架就搭建好了。
springboot框架搭建-访问数据库_mysql
2)在pom.xml文件里添加Mybatis的起步依赖数据库驱动坐标mysql

<!--       mybatis依赖-->
      <dependency>
         <groupId>org.mybatis.spring.boot</groupId>
         <artifactId>mybatis-spring-boot-starter</artifactId>
         <version>2.2.0</version>
      </dependency>
<!--      数据库驱动坐标-->
      <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
      </dependency>
      <dependency>
	 <groupId>org.projectlombok</groupId>
	 <artifactId>lombok</artifactId>
	 <version>1.18.20</version>
      </dependency>

3)在application.properties中添加数据库链接信息web

server.server.port=8085
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?userUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456

4)在mysql数据库建立user表spring

CREATE TABLE testuser (
id int(11) NOT NULL COMMENT 'id',
name varchar(255) DEFAULT NULL COMMENT '姓名',
age int(11) DEFAULT NULL COMMENT '年龄',
password varchar(255) NOT NULL COMMENT '密码',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;sql

建立实体Bean数据库

package com.test.demo.bean;
import lombok.Data;
@Data
public class User {
    private int id;
    private String name;
    private int age;
    private String password;

}

5)编写Mapperspringboot

注意:@Mapper标记该类是一个mybatis的mapper接口,能够被spring boot自动扫描到spring上下文中服务器

//注意:@Mapper标记该类是一个mybatis的mapper接口,能够被spring boot自动扫描到spring上下文中
@Mapper
public interface UserMapper {
    public List<User> queryUserList();
}

6)配置Mapper映射文件mybatis

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.test.demo.mapper.UserMapper">
    <select id="queryUserList" resultType="com.test.demo.bean.User">
      select * from user
    </select>
</mapper>

7)在application.properties中添加mybatis的信息app

#添加mybatis的信息**
mybatis.type-aliases-package=com.test.demo.bean
mybatis.mapper-locations=classpath:mapper/*.xml

8)编写测试Controller

注解须要在pom.xml里安装依赖

<dependency>
   <groupId>org.codehaus.jackson</groupId>
   <artifactId>jackson-mapper-asl</artifactId>
   <version>1.9.8</version>
   <type>jar</type>
   <scope>compile</scope>
</dependency>
package com.test.demo.controller;
import com.test.demo.bean.User;
import com.test.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
//@RestController至关于 Controller+ResponseBody
@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;
  //在本地服务器上访问方法 http://localhost:8080/queryUser
    @RequestMapping("/queryUser")
    public List<User> queryUser(){
        List<User> userList = userMapper.queryUserList();
        return userList;
    }
}

9)测试:启动spring boot

访问:http://localhost:8080/queryUser

springboot框架搭建-访问数据库_数据库_02

转载:https://cloud.tencent.com/developer/article/1647670?from=article.detail.1647624