spring + spingMVC + mybatis项目构建

基本环境构建

运行本地数据库

数据库使用oracle 11g。创建test用户新增INFO表新增两组数据。
建库建表插入数据

创建项目

用ideal创建maven项目,结构如下。
项目结构

引入依赖

在pom.xml中引入依赖。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mars</groupId>
    <artifactId>display_backend</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>display_backend</name>
    <description>display_backend</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.oracle.database.nls</groupId>
            <artifactId>orai18n</artifactId>
            <version>19.7.0.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

配置spring和mybatis

修改application.yml。

spring:
  datasource:
    name: localorcl
    driver-class-name: oracle.jdbc.OracleDriver
    url:  jdbc:oracle:thin:@localhost/orcl
    username: test
    password: 1234
mybatis:
  mapper-locations: classpath*:/mapper/*.xml
  type-aliases-package: com.mars.display_backend.entity
  configuration:
    map-underscore-to-camel-case: true

写一写代码

完成代码

Entity

@Data
public class Info {
    private int id;
    private String name;
}

Dao

@Mapper
public interface InfoDao {
     Info getInfoById(int id);
     ArrayList<Info> getAllInfos();
}

Service

@Service
public class InfoService {
    private InfoDao infoDao;
    @Autowired
    public InfoService(InfoDao infoDao){
        this.infoDao = infoDao;
    }
    public Info getInfoById(int id){
        return infoDao.getInfoById(id);
    }
    public ArrayList<Info> getAllInfos(){
        return infoDao.getAllInfos();
    }
}

Controller

@RestController
public class InfoController {
    private InfoService infoService;
    @Autowired
    public InfoController(InfoService infoService){
        this.infoService = infoService;
    }
    @GetMapping("/getAllInfos")
    public ResponseWrapper<?> getAllInfos(){
        ArrayList<Info> infos;
        infos = infoService.getAllInfos();
        return new ResponseWrapper<>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(), ResponseGenerator.listTransForm(infos,new String[]{"id","name"},null));
    }
    @GetMapping("/getInfo")
    public ResponseWrapper<?> getInfo(@RequestParam("id")int id){
        Info info = infoService.getInfoById(id);
        return new ResponseWrapper<>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(), ResponseGenerator.generate(info,new String[]{"id","name"},null));
    }
}

ResponseWrapper是自用的响应处理类,可用其他JSON处理包代替。

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.mars.display_backend.dao.InfoDao">
    <select id="getInfoById" parameterType="int" resultType="Info">
        select * from info where ID = #{id}
    </select>
    <select id="getAllInfos" resultType="Info">
        select * from info
    </select>
</mapper>

运行测试

运行application类,浏览器输入请求,如果没出bug应显示如下结果。
运行测试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值