使用MyBatis进行数据库操作,配置简单。主要演示了mybatis可以不用只使用方法名来对应mapper.java和mapper.xml。

目录结构

pom.xml
src/
├── main/
│   ├── java/
│   │   └── com/
│   │       └── springbootjdbcweb/
│   │           └── springbootjdbcweb/
│   │               ├── SpringbootjdbcwebApplication.java
│   │               ├── controller/
│   │               │   └── CertificationAuditController.java
│   │               ├── mapper/
│   │               │   ├── CertificationAuditMapper.java
│   │               │   └── CertificationAuditMapper.xml
│   │               ├── pojo/
│   │               │   └── CertificationAudit.java
│   │               └── service/
│   │                   └── CertificationAuditService.java
│   └── resources/
│       ├── application.yml
│       ├── static/
│       └── templates/
│           └── login.html
└── test/
    └── java/
        └── com/
            └── springbootjdbcweb/
                └── springbootjdbcweb/
                    └── SpringbootjdbcwebApplicationTests.java

表sql


CREATE TABLE certificationaudit (
    serialnumber INT PRIMARY KEY AUTO_INCREMENT COMMENT '序号',
    applicationnumber INT NOT NULL COMMENT '申请编号',
    doctorname VARCHAR(50) NOT NULL COMMENT '医生姓名',
    medicalestablishment VARCHAR(100) NOT NULL COMMENT '医疗机构',
    specificationcenter VARCHAR(100) NOT NULL COMMENT '规范中心',
    phonenumber VARCHAR(20) NOT NULL COMMENT '手机号码',
    controlsystem VARCHAR(255) COMMENT '管理制度',
    coordinatesystem VARCHAR(255) COMMENT '协调体系',
    heartfailurearchitecture DATETIME COMMENT '心衰架构',
    creationtime DATETIME NOT NULL COMMENT '创建时间',
    filewritepeople VARCHAR(50) NOT NULL COMMENT '填表人员',
    auditstatus VARCHAR(20) NOT NULL COMMENT '审核状态',
    operation VARCHAR(255) COMMENT '操作'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='认证审核表';

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.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springbootjdbcweb</groupId>
    <artifactId>springbootjdbcweb</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springbootjdbcweb</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
<!--        <dependency>-->
<!--            <groupId>org.apache.httpcomponents</groupId>-->
<!--            <artifactId>httpcore</artifactId>-->
<!--            <version>4.3.3</version>-->
<!--        </dependency>-->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--通用Mapepr-->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.4</version>
        </dependency>
        <!--分页插件-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
        <!--FastJson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.50</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.15</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.0.0</version>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory><!--所在的目录-->
                <includes><!--包括目录下的.properties,.xml文件都会扫描到-->
                    <include>**/*.yml</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>


</project>

配置文件

server:
  port: 8080
spring:
  datasource:
    username: root
    password: 1234
    url: jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
pagehelper:
  page-size-zero: true
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params: count=countsql
logging:
  level:
    com:
      springbootjdbcweb:
        springbootjdbcweb: debug

mybatis 使用 完全限定名+类名+方法名也可以关联。

实体类映射



import java.util.Date;

/*认证审核*/

public class CertificationAudit{
    /*序号:serial number  int
     申请编号:application number  int
     医生姓名:doctor name
     医疗机构:medical establishment
     规范中心:Specification center
     手机号码:phone number
     管理制度:control system
     协调体系:Coordinate system
     心衰架构:Heart failure architecture
     创建时间:creation time  datetime
     填表人员:Fill out a form personnel--file write people
     审核状态:audit status
     操作:operation
     */

    private Integer serialNumber;
    private Integer applicationNumber;
    private String doctorName;
    private String medicalEstablishment;
    private String specificationCenter;
    private String phoneNumber;
    private String controlSystem;
    private String coordinateSystem;
    private Date heartFailureArchitecture;
    private Date creationTime;
    private String fileWritePeople;
    private String auditStatus;
    private String operation;
    /* 忽略get set 空参构造器,全参构造器 */
}
   

mapper.java


package com.springbootjdbcweb.springbootjdbcweb.mapper;

import com.springbootjdbcweb.springbootjdbcweb.pojo.CertificationAudit;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.common.Mapper;


@Component
public interface CertificationAuditMapper extends Mapper<CertificationAudit> {

    int delete1(@Param("serialnumber") Integer serialnumber);
}


mapper.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.springbootjdbcweb.springbootjdbcweb.mapper.CertificationAuditMapper">
    <!--通用查询映射结果-->
    <resultMap id="sel" type="com.springbootjdbcweb.springbootjdbcweb.pojo.CertificationAudit">
        <!--column数据库      property实体类对应-->
        <id column="serialnumber" property="serialNumber"/>
        <result column="applicationnumber" property="applicationNumber"/>
        <result column="doctorname" property="doctorName"/>
        <result column="medicalestablishment" property="medicalEstablishment"/>
        <result column="specificationcenter" property="specificationCenter"/>
        <result column="phonenumber" property="phoneNumber"/>
        <result column="controlsystem" property="controlSystem"/>
        <result column="coordinatesystem" property="coordinateSystem"/>
        <result column="heartfailurearchitecture" property="heartFailureArchitecture"/>
        <result column="creationtime" property="creationTime"/>
        <result column="filewritepeople" property="fileWritePeople"/>
        <result column="auditstatus" property="auditStatus"/>
        <result column="operation" property="operation"/>
    </resultMap>
    <!--通用查询结果列-->
    <sql id="aaa">
serialnumber,applicationnumber,doctorname,medicalestablishment,specificationcenter,phonenumber,controlsystem,coordinatesystem,heartfailurearchitecture,creationtime,filewritepeople,auditstatus,operation
    </sql>
    <select id="selCerList" resultMap="sel">
        select * from CertificationAudit
    </select>
    <delete id="com.springbootjdbcweb.springbootjdbcweb.mapper.CertificationAuditMapper.delete1" parameterType="int">
        delete from certificationaudit where serialnumber=#{serialnumber}
    </delete>
</mapper>

service

import com.springbootjdbcweb.springbootjdbcweb.mapper.CertificationAuditMapper;
import com.springbootjdbcweb.springbootjdbcweb.pojo.CertificationAudit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CertificationAuditService {
    @Autowired
    private CertificationAuditMapper certificationAuditMapper;


    public List<CertificationAudit> selCerList() {
        return certificationAuditMapper.selectAll();
    }
    public Integer delCertificationAudit(Integer serialnumber){
        return certificationAuditMapper.deleteByPrimaryKey(serialnumber);
    }
    public Integer delete1(Integer serialnumber){
      return certificationAuditMapper.delete1(serialnumber);
    }
}

controller

import com.springbootjdbcweb.springbootjdbcweb.service.CertificationAuditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@RestController
//@ResponseBody
//@RequestMapping
public class CertificationAuditController {
    @Autowired
    private CertificationAuditService certificationAuditService;

    @RequestMapping(value = "/cer")
    public String selCer(  Integer serialnumber) {
        Integer integer = certificationAuditService.delete1(serialnumber);
        System.out.println(integer);
        return "login";
    }
    @GetMapping("/k")
    public String s(){
        System.out.println("232232");
        return "login";
    }
}

启动类



import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
@MapperScan("com.springbootjdbcweb.springbootjdbcweb")
public class SpringbootjdbcwebApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootjdbcwebApplication.class, args);
    }

}
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐