一、通过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>3.2.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>doris</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>doris</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</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>3.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</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>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>3.0.3</version>
<scope>test</scope>
</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>
二、项目结构
三、配置 (application.properties)
spring.datasource.url=jdbc:mysql://yourip:9030/dxh_test
spring.datasource.username=admin
spring.datasource.password=wsf@123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.type-aliases-package=com.example.doris.model
mybatis.mapper-locations=classpath:mybatis/*.xml
mybatis.configuration.map-underscore-to-camel-case=true
server.port=8081
四、实体(数据表对应的实体类:model/UserInfo.java)
@Data
public class UserInfo {
private BigInteger id;
private BigInteger userId;
private String userName;
}
五、mapper类(UserInfoMapper.java)
@Mapper
public interface UserInfoMapper {
@org.apache.ibatis.annotations.Select("select `id`,`user_id` AS userId,`user_name` AS userName from user_info_v6")
public List<com.example.doris.model.UserInfo> selectAll();
List<UserInfo> getUserInfoList();
}
六、项目入口(DorisApplication.java)添加MapperScan(mapper类对应的包)
@SpringBootApplication
@MapperScan("com.example.doris.dao")
public class DorisApplication {
public static void main(String[] args) {
SpringApplication.run(DorisApplication.class, args);
}
}
七、mybatis对应的表的XML配置(UserInfoMapper.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.example.doris.dao.UserInfoMapper">
<select id="getUserInfoList" resultType="UserInfo">
select * from user_info_v6
</select>
</mapper>
八、服务层(service)
8.1 服务接口层(interface => UserInfoService)
public interface UserInfoService {
List<UserInfo> getAllUserInfo() throws Exception;
List<DorisTestModel> getAllDorisTestModel() throws Exception;
}
8.2 服务实现层(impl=> UserInfoImplService)
@Service
public class UserInfoImplService implements UserInfoService {
@Autowired
UserInfoMapper userInfoMapper;
@Autowired
DorisTestMapper dorisTestMapper;
@Override
public List<UserInfo> getAllUserInfo() throws Exception{
//List<UserInfo> userInfoList = userInfoMapper.selectAll();
List<UserInfo> userInfoList = userInfoMapper.getUserInfoList();
System.out.println(userInfoList);
return userInfoList;
}
@Override
public List<DorisTestModel> getAllDorisTestModel() throws Exception {
return dorisTestMapper.selectAll();
}
}
九、控制层
@RestController
@RequestMapping("/userinfo")
public class UserInfoController {
@Autowired
UserInfoService userInfoService;
@RequestMapping("/list")
public List<UserInfo> getUserInfoList() {
try {
return userInfoService.getAllUserInfo();
} catch (Exception ex) {
System.out.println(ex.getMessage());
return null;
}
}
}
十、数据库的创表:(本实例是使用的doris,doris支持mysql的协议)
### 创建表SQL
CREATE TABLE `user_info_v6` (
`id` int NULL COMMENT "id",
`user_id` int NULL COMMENT "user_id",
`user_name` varchar(40) NULL COMMENT "名称"
) ENGINE=OLAP
UNIQUE KEY(`id`)
COMMENT "OLAP"
DISTRIBUTED BY HASH(`id`) BUCKETS 1
PROPERTIES (
"replication_num" = "1",
"in_memory" = "false",
"storage_format" = "V2"
);
### 插入数据SQL
insert into user_info_v6(`id`,`user_id`,`user_name`)
values (1,333,'doris-name-333'),
(2,444,'doris-name-444');
### 查询数据SQL
select * from user_info_v6;
十一:结果
**
**
标签:springboot,spring,boot,id,user,mybtais,mysql,org,public From: https://blog.51cto.com/u_6404598/9369627