先使用idea创建最基础的spingboot项目,建好如下文件目录。
--src
--main
--java
--com.springboot.merge
--user /* 模块名称 */
--controller /* controller层 */
--dto /* dto实体类层,也可叫做domain */
--mapper /* mappe接口层,也可叫做dao,与/resources/mapper/下的文件映射 */
--service /* service逻辑处理类层 */
--resources
--mapper /* 存放mybatis XML文件 */
--user /* 模块名称 */
mysql
mysql-connector-java
5.1.39
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
整个pom.xml文件
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
com.springboot
merge
0.0.1-SNAPSHOT
war
merge
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-tomcat
provided
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
5.1.39
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
org.springframework.boot
spring-boot-maven-plugin
server:
port: 8081
spring:
# 数据源/数据库配置
datasource:
url: jdbc:mysql://127.0.0.1:3306/sprinngbootmerge?useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
# mybatis配置
mybatis:
type-aliases-package: com.springboot.merge.*.dto # 指向实体包路径
mapper-locations: classpath*:/mapper/**/*.xml # 指向mapper.xml文件的位置:模块名/mapper/*.xml
在com.springboot.merge.user.dto下新建UserTest类
package com.springboot.merge.user.dto;
/**
* @ClassName UserTest
* @description 测试实体类
* @author [email protected]
* @date 19-3-31 下午3:03
* @version 1.0
* @since JDK 1.8
*/
public class UserTest {
private Long userId;
private String userName;
private String userPassword;
public Long getUserId() { return userId; }
public void setUserId(Long userId) { this.userId = userId; }
public String getUserName() { return userName; }
public void setUserName(String userName) { this.userName = userName; }
public String getUserPassword() { return userPassword; }
public void setUserPassword(String userPassword) { this.userPassword = userPassword; }
}
在com.springboot.merge.user.mapper下新建UserTestMapper接口。
接口上加@Mapper注解;
package com.springboot.merge.user.mapper;
import com.springboot.merge.user.dto.UserTest;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* @ClassName UserTestMapper
* @description 测试类接口
* @author [email protected]
* @date 19-4-14 下午4:29
* @version 1.0
* @since JDK 1.8
*/
@Mapper
public interface UserTestMapper {
List userTestInfo();
}
在启动类MergeApplication上加扫描配置mappe接口文件位置后可不用加@Mapper注解,此两种方式选一即可。
在resources/mapper/user下新建UserTestMapper.xml文件用于实现mapper接口中的方法。
在com.springboot.merge.user.service下新建UserTestService类。
package com.springboot.merge.user.service;
import com.springboot.merge.user.dto.UserTest;
import com.springboot.merge.user.mapper.UserTestMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @ClassName UserTestService
* @description 测试Service类
* @author [email protected]
* @date 19-3-31 下午3:31
* @version 1.0
* @since JDK 1.8
*/
@Service
public class UserTestService {
@Autowired
private UserTestMapper mapper;
public List queryUserTestInfo(){
return mapper.userTestInfo();
}
}
在com.springboot.merge.user.controller下新建UserTestController类。
package com.springboot.merge.user.controller;
import com.springboot.merge.user.dto.UserTest;
import com.springboot.merge.user.service.UserTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @ClassName UserTestController
* @description 测试控制类
* @author [email protected]
* @date 19-3-31 下午3:36
* @version 1.0
* @since JDK 1.8
*/
@RestController
@RequestMapping("")
public class UserTestController {
@Autowired
private UserTestService service;
@GetMapping(value = "/user/test/info/query", produces="application/json")
public List queryUserTestInfo(){
return service.queryUserTestInfo();
}
}
后面会在此基础上集成一些基础框架
https://github.com/hellozhaoxudong/springbootMerge