- 下载安装IDEA
- 详情见百度
- 配置maven
- 详情见百度
- 创建springboot-web项目
- 创建project,选择Spring Initalizr, 选择default
-
-
输入组织、项目名称以及基本信息
-
-
- 选择spring Web 以及数据库连接
-
- 继续下一步Next,完成创建finish。
- 启动springApplication,提示。
修改项目jdk版本信息为1.8
1、项目当前jdk信息
2、当前模型的jdk版本信息
3、点击设置,查看当前java编译版本
4、打开pom.xml,修改springboot版本信息,以及java.version信息
5、清除原配置信息,执行启动类
6、继续启动,再次报错,缺少数据源配置信息,修改application.properties文件,填写Oracle驱动信息。
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@//XXX.XX.X.XX:1521/XXXX
spring.datasource.username=XXXXXXX
spring.datasource.password=XXXXXXX
提示:当前项目的依赖如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
7、运行spring启动类,成功启动。
8、编写controller类
MainController.Java
package com.tohi.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class MainController {
@RequestMapping({"/selectUser"})
public String selectUser() {
return "hello-selectUser";
}
}
SpringbootMybatisApplication.java
package com.tohi.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
9.运行启动类,访问 127.0.0.1:8080/selectUser 运行成功
- 集成mybatis(待更新)
- 导入mybatis 依赖
-
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.4</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.5</version> </dependency>
- 编写mapper.xml
-
UserMapper.xml
<?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.tohi.mapper.UserMapper"> <select id="selectAll" resultType="com.tohi.pojo.User"> select * from maxuser </select> </mapper>
- 编写实体类、mapper接口
-
User.java
package com.tohi.pojo; import lombok.Data; @Data public class User { private String personid; private String password; private String status; private String statusdate; }
UserMapper.java
package com.tohi.mapper; import com.tohi.pojo.User; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; @Mapper @Repository public interface UserMapper { public List<User> selectAll(); }
- 编写MainController
-
package com.tohi.controller; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.tohi.mapper.UserMapper; import com.tohi.pojo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class MainController { @Autowired private UserMapper mapper; @RequestMapping("/selectUser") public String selectUser() { List<User> users = mapper.selectAll(); JSONArray array= (JSONArray) JSON.toJSON(users); String str=array.toJSONString(); return str; } }
5、编写SpringbootMybatisApplication.Java
package com.tohi.controller; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @MapperScan("com.tohi.mapper") public class SpringbootMybatisApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SpringbootMybatisApplication.class); } public static void main(String[] args) { SpringApplication.run(SpringbootMybatisApplication.class, args); } }
调用,127.0.0.1:8080/selectUser 会输出当前数据库查询的所有记录信息
- 集成myBatis-plus
- 在pojo包下面新建Maxuser.java
-
package com.tohi.pojo; import lombok.Data; @Data public class Maxuser { private String personid; private String password; private String status; private String statusdate; }
- 编写UserMapperPlus.java文件,写上mapper注解
-
package com.tohi.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.tohi.pojo.Maxuser; import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserMapperPlus extends BaseMapper<Maxuser> { }
3、MainController.java中添加方法
@RequestMapping("/selectUser1") public String selectUser1() { List<Maxuser> users = userMapperPlus.selectList(null); JSONArray array= (JSONArray) JSON.toJSON(users); String str=array.toJSONString(); return str; }
4.启动Application,进行访问127.0.0.1:8080/selectUser1 访问成功!!