springboot集成sqlite、mybatis

1.创建sqlite数据库文件

D:/work2020/sqlite/cyq.sqlite

2.创建表Company

CREATE TABLE company(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL
);

3.插入测试数据

INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Paul', 32, 'California', 20000.00 );

INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (2, 'Allen', 25, 'Texas', 15000.00 );

INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );

INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );

INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (5, 'David', 27, 'Texas', 85000.00 );

INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (6, 'Kim', 22, 'South-Hall', 45000.00 );

4.新建SpringBoot工程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.2.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.wisea</groupId>
    <artifactId>test-project-2020-1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>test-project-2020-1</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.21.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

5.配置application.yml

mybatis:
  mapper-locations: classpath:mapping/*Mapper.xml
  type-aliases-package: com.wisea.tp.entity

spring:
  datasource:
    url: jdbc:sqlite:D:/work2020/sqlite/cyq.sqlite
    driver-class-name: org.sqlite.JDBC

6.新建entity、mapper、controller如下

entity.java --> company.java

package com.wisea.tp.entity;

import org.apache.ibatis.type.Alias;

@Alias("company_alias")
public class Company {
    private Integer id;
    private String name;
    private Integer age;
    private String address;
    private Double salary;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }
}

mapper.java --> CompanyMapper.java

package com.wisea.tp.mapper;

import com.wisea.tp.entity.Company;

import java.util.List;

public interface CompanyMapper {

    Company findById(Integer id);

    List<Company> findList();

    List<Company> findListByCondition(Company company);

    int insert(Company company);

    int update(Company company);

    int delete(Integer id);
}

mapper.xml --> CompanyMapper.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.wisea.tp.mapper.CompanyMapper">

    <resultMap id="BaseResultMap" type="com.wisea.tp.entity.Company">
        <result column="id" jdbcType="INTEGER" property="id"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="age" jdbcType="INTEGER" property="age"/>
        <result column="address" jdbcType="VARCHAR" property="address"/>
        <result column="salary" jdbcType="DOUBLE" property="salary"/>
    </resultMap>

    <select id="findById" resultType="com.wisea.tp.entity.Company">
        select * from company where id = #{id}
    </select>
    <select id="findList" resultType="com.wisea.tp.entity.Company">
        select * from company
    </select>
    <select id="findListByCondition" parameterType="company_alias" resultType="company_alias">
        select * from company
        where 1 = 1
        <if test="id != null">
            and id = #{id}
        </if>
        <if test="name != null">
            and name = #{name}
        </if>
        <if test="age != null">
            and age = #{age}
        </if>
        <if test="address != null">
            and address = #{address}
        </if>
        <if test="salary != null">
            and salary = #{salary}
        </if>
    </select>
    <insert id="insert" parameterType="com.wisea.tp.entity.Company">
        insert into company(ID,NAME,AGE,ADDRESS,SALARY)
        values (#{id},#{name},#{age},#{address},#{salary})
    </insert>
    <update id="update" parameterType="com.wisea.tp.entity.Company">
        update company
        <set>
            <if test="name != null">
                name = #{name}
            </if>
            <if test="age != null">
                age = #{age}
            </if>
            <if test="address != null">
                address = #{address}
            </if>
            <if test="salary != null">
                salary = #{salary}
            </if>
        </set>
        where id = #{id}
    </update>
    <delete id="delete">
        delete from company where id = #{id}
    </delete>
</mapper>

controller.java --> CompanyController.java

package com.wisea.tp.controller;

import com.wisea.tp.entity.Company;
import com.wisea.tp.mapper.CompanyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/com")
public class CompanyController {

    @Autowired
    private CompanyMapper companyMapper;

    @RequestMapping("/get/{id}")
    public Company get(@PathVariable("id") Integer id){
        return companyMapper.findById(id);
    }

    @RequestMapping("/list")
    public List<Company> list(){
        return companyMapper.findList();
    }

    @RequestMapping("/listBy")
    public List<Company> listBy(Company company){
        return companyMapper.findListByCondition(company);
    }

    @RequestMapping("/add")
    public Integer add(@RequestBody(required = false) Company company, Company company1){
        if (company1 != null) {
            companyMapper.insert(company1);
        }
        return companyMapper.insert(company);
    }

    @RequestMapping("/modify")
    public Integer modify(Company company){
        return companyMapper.update(company);
    }

    @RequestMapping("/delete/{id}")
    public Integer delete(@PathVariable("id") Integer id){
        return companyMapper.delete(id);
    }

}

6.启动类上加入如下配置

@MapperScan("com.wisea.tp.mapper")

通过以上步骤,一个完整的springboot、sqlite、mybaits增删改查就完成了。

你可能感兴趣的:(javaEE,数据,SpringBoot)