This article is just a record of JAVA getting started with implementing the first interface via SpringBoot+ Mybatis

1. Environment construction

Environment construction is not the focus of this article, do not do a specific narrative

  1. Installing the JAVA Environment
  2. Install MySQL and run it
  3. Download the IntelliJ IDEA
  4. Configure maven
  • Download maven
  • The editorconf/settings.xmlconfigurationmavenLocal warehouse path
  • Set maven paths in IntelliJ IDEA

2. New projects

  • chooseSpring Initializr, select the corresponding Java version

  • Adding dependencies can also be skipped in thepom.xmlAdd to file

  • pom.xmlAdd dependencies to files

In the POM.xml file, Dependencies, add dependencies (the dependencies from the previous step are also here)

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.0</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</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-web</artifactId>
</dependency>
Copy the code

3. Create database data

On the database and data table related operations, not the focus of this paper, this papert_userThe following table is used as an example

4, CODE

Create the following files

The application.yml file is the changed file suffix

Configure database and Mybatis

  • inapplication.ymlConfigure database and Mybatis in
server:
  prot:8080
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: JDBC: mysql: / / 127.0.0.1:3306 / smart_feeder? useUnicode=true&characterEncoding=UTF-8
    username: root
    password: Test1234

mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.orangeal.demo.entity
  configuration:
    map-underscore-to-camel-case: true 
Copy the code
  • Start the classDemoApplicationThe use of@MapperScan

@mapperscan Can scan all mapper in the corresponding directory

@MapperScan("com.orangeal.demo.mapper")
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}Copy the code

entity

Add the required fields to TUserInfo

@Data
public class TUserInfo {
    Long id;
    String name;
    String email;
}
Copy the code

mapper

  • TUserMapperDefines the interface
public interface TUserMapper {
    List<TUserInfo> getUserList(a);
}
Copy the code
  • TUserMapper.xmlImplementation interface in

Namespace corresponds to a mapper interface, and ID corresponds to the method name resultType corresponds to the return type


      

<! DOCTYPEmapper
        PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.orangeal.demo.mapper.TUserMapper">
    <select id="getUserList" resultType="com.orangeal.demo.entity.TUserInfo">
        select id,email,name from t_user
    </select>
</mapper>
Copy the code

service

  • TUserServiceDefine the interface in
public interface TUserService {
    List<TUserInfo> getUserList(a);
}
Copy the code
  • TUserServceImplImplementing an interface
@Service
public class TUserServceImpl implements TUserService {

    @Resource
    private TUserMapper mapper;

    @Override
    public List<TUserInfo> getUserList(a) {
        returnmapper.getUserList(); }}Copy the code

controller

The TUserController defines the interface address and request mode, and finally calls the Service implementation

@RestController
@RequestMapping("/api/userList")
public class TUserController {

    @Autowired
    private TUserService service;

    @GetMapping
    public List<TUserInfo> getUserList(a) {
        returnservice.getUserList(); }}Copy the code

debugging

After starting the project, passPostmanOr browser debugging