依赖
<?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 http://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.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.eos</groupId>
<artifactId>seal</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>seal</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>7.2.2.jre8</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
数据源配置
spring.datasource.url=jdbc:sqlserver://10.90.12.203;DatabaseName=Seal
spring.datasource.username=seal
spring.datasource.password=123456
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
mybatis.mapper-locations=classpath:mapping/*Mapper.xml
mybatis.type-aliases-package=com.eos.seal.entity
server.port=8081
映射文件:mybatis.mapper-locations=classpath:mapping/*Mapper.xml
别名(实体)所在包:mybatis.type-aliases-package=com.eos.seal.entity
配置类添加Mapper扫描包
package com.eos.seal;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.eos.seal.mapper")
public class SealApplication {
public static void main(String[] args) {
SpringApplication.run(SealApplication.class, args);
}
}
实体类
@Alias(“Seal”)定义别名,在映射文件中可以使用该别名,而不用使用全类名。
package com.eos.seal.entity;
import org.apache.ibatis.type.Alias;
@Alias("Seal")
public class Seal {
private Integer id;
private String name;
private String accountId;//账户ID
private Integer accountType;//账户类型
private Integer sealType;//类型,0
private String templateType;//印章模板,""
private String color;//颜色
private Integer isDefault;//是否是默认印章
private Integer status;//状态
private String image;//base64图片
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 String getAccountId() {
return accountId;
}
public void setAccountId(String accountId) {
this.accountId = accountId;
}
public Integer getAccountType() {
return accountType;
}
public void setAccountType(Integer accountType) {
this.accountType = accountType;
}
public Integer getSealType() {
return sealType;
}
public void setSealType(Integer sealType) {
this.sealType = sealType;
}
public String getTemplateType() {
return templateType;
}
public void setTemplateType(String templateType) {
this.templateType = templateType;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public Integer getIsDefault() {
return isDefault;
}
public void setIsDefault(Integer isDefault) {
this.isDefault = isDefault;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
}
Mapper接口
package com.eos.seal.mapper;
import com.eos.seal.entity.Seal;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface SealMapper {
// Seal findById(int id);
List<Seal> findAll();
}
映射文件
<?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.eos.seal.mapper.SealMapper">
<resultMap id="BaseResultMap" type="Seal">
<result column="FID" property="id" />
<result column="FName" property="name" />
<result column="FAccountId" property="accountId" />
<result column="FAccountType" property="accountType" />
<result column="FSealType" property="sealType" />
<result column="FSealTemplateType" property="templateType" />
<result column="FColor" property="color" />
<result column="FSealImage" property="image" />
<result column="FIsDefault" property="isDefault" />
<result column="FStatus" property="status" />
</resultMap>
<select id="findAll" resultMap="BaseResultMap">
select * from T_SealInfo
</select>
</mapper>
Service层
package com.eos.seal.service;
import com.eos.seal.entity.Seal;
import com.eos.seal.mapper.SealMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class SealService {
@Autowired
SealMapper sealMapper;
public List<Seal> getAll(){
return sealMapper.findAll();
}
}
Controller层
package com.eos.seal.controller;
import com.eos.seal.entity.Seal;
import com.eos.seal.service.SealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping(value = "/seals")
public class SealController {
@Autowired
private SealService sealService;
@RequestMapping(method = RequestMethod.GET)
public List<Seal> getAll(){
return sealService.getAll();
}
}