springboot框架搭建-访问数据库

springboot框架搭建

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

<!--       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中添加数据库连接信息

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表

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;

创建实体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)编写Mapper

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

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

6)配置Mapper映射文件

<?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的信息

#添加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框架搭建-访问数据库

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

springboot框架搭建-访问数据库

上一篇:使用minikube安装单机版Kubernetes


下一篇:写1行代码影响1000000000人,这是个什么项目?