Springboot from entry to mastery (4)

Springboot integrates mybatis

1. Add the required dependencies to the pom:

<dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>${mybatis.spring.version}</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

2. Configure the relevant information of the data source and mybatis in application.yml

#data source
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/cfcc_pt
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
#mybatis
mybatis:
  typeAliasesPackage: com.cfcc.pojo
  mapperLocations: classpath:mybatis/*.xml

3. Add a mapper scanner to the startup class to scan the referenced packages.

@SpringBootApplication
@RestController
@MapperScan("com.cfcc.mapper")
public class CfccApplicaton {
    public static void main(String[] args) {
        SpringApplication.run(CfccApplicaton.class, args);
    }

}

4. Create a new user table in the database

CREATE TABLE `cf_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `age` int(11) DEFAULT NULL,
  `addr` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

5. Create a new entity class corresponding to the user

package com.cfcc.pojo;

public class CfUser {
    private int id;
    private String name;
    private int age;
    private String addr;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }
}

6. Create a new mybatis directory under resources and create a new userMapper.xml below.

Write some simple methods of adding, deleting, modifying and checking for users in 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.cfcc.mapper.UserMapper" >
	<select id="findUserById" parameterType="int" resultType="cfUser">
		SELECT * from cf_user where id=#{id}
	</select>
	<select id="findUserListByName" resultType="cfUser">
		SELECT * from cf_user
	</select>
	<!-- Insert and return the auto-incrementing primary key-->
	<insert id="insertUser" parameterType="cfUser" useGeneratedKeys="true" keyProperty="id">
		insert INTO cf_user(name, age, addr) VALUES (#{name},#{age},#{addr})
	</insert>
	<update id="updateUser" parameterType="cfUser">
		UPDATE cf_user set name=#{name},age=#{age},addr=#{addr}
		where id=#{id}
	</update>
	<delete id="deleteUserById" parameterType="int">
		delete from cf_user where id=#{id}
	</delete>

</mapper>

7. Create a new UserMapper interface and write the corresponding method.

package com.cfcc.mapper;

import com.cfcc.pojo.CfUser;

import java.util.List;

public interface UserMapper {
    CfUser findUserById (int id);
    List<CfUser> findUserListByName();
    void insertUser(CfUser cfUser);
    void updateUser(CfUser cfUser);
    void deleteUserById(int id);
}

8. Write a method in the controller to call the mapper and use a simple restful style url.

package com.cfcc.controller;

import com.cfcc.mapper.UserMapper;
import com.cfcc.pojo.CfUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
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("/rest")
public class UserRestController {
    @Autowired
    private UserMapper mapper;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public List<CfUser> getUserList(){
        List<CfUser> list = mapper.findUserListByName();
        return list;
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public CfUser getUserById (athPathVariable int id)
        CfUser user = mapper.findUserById(id);
        return user;
    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    public CfUser postUser(){
        CfUser user = new CfUser();
        user.setName("Zhang San");
        user.setAge(23);
        user.setAddr("Beijing");
        mapper.insertUser(user);
        return user;
    }
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String putUser (athPathVariable int id)
        CfUser user = new CfUser();
        user.setName("王五");
        user.setAge(24);
        user.setAddr("Guangzhou");
        user.setId(id);
        mapper.updateUser(user);
        return "put success";
    }
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String deleteUser(@PathVariable int id){
        mapper.deleteUserById(id);
        return "delete success";
    }

}

Start the project, you can access the corresponding url in the browser.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325542200&siteId=291194637