springboot整合mybatis(注解)

springboot整合mybatis(注解)

1.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example.mapper</groupId>
    <artifactId>springboot_mybatis</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <name>springboot_mybatis</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <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>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

2.application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

3.sql:

CREATE TABLE `user` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `age` int(10) DEFAULT NULL,
  `name` varchar(100) DEFAULT NULL,
  `role` varchar(100) DEFAULT NULL,
  `email` varchar(100) DEFAULT NULL,
  `phone` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8

4.User:

package com.example.mapper.mybatisMap.entity;

/**
 * @author:
 * @date: 2018/8/13
 * @description:
 */
public class User {
    private int id;
    private int age;
    private String name;
    private String role;
    private String email;
    private String phone;

    public int getId() {
        return id;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

5.UserMapper:

package com.example.mapper.mybatisMap.dao;

import com.example.mapper.mybatisMap.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * @author: 
 * @date: 2018/8/13
 * @description:
 */
@Mapper
public interface  UserMapper {

    @Select("SELECT * FROM USER WHERE NAME = #{name}")
    User findByName(@Param("name") String name);

    @Insert("INSERT INTO USER(NAME, AGE, ID) VALUES(#{name}, #{age}, #{id})")
    int insert(@Param("name") String name, @Param("age") Integer age, @Param("id") Integer id);

}

6.Controller:

package com.example.mapper.mybatisMap.controller;


import com.example.mapper.mybatisMap.dao.UserMapper;
import com.example.mapper.mybatisMap.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * @author: 
 * @date: 2018/8/13
 * @description:
 */
@RestController
public class Controller {

    @Autowired
    UserMapper userMapper;

    private Logger logger = LoggerFactory.getLogger(Controller.class);

    /**
     * 查询数据
     */
    @RequestMapping("/get")
    public User getName(HttpServletRequest request
            , HttpServletResponse response
            , @RequestParam(value="name") String name){
        User user = new User();
        user = userMapper.findByName(name);
        return user;
    }

    /**
     * 插入数据
     */
    @RequestMapping("/insert")
    public String insertName(HttpServletRequest request, HttpServletResponse response){
        String name = request.getParameter("name");
        int age = Integer.valueOf(request.getParameter("age"));
        int id = Integer.valueOf(request.getParameter("id"));
        int number = userMapper.insert(name, age, id);
        return "插入数据:" + number + "条";
    }


}

7.MybatisMapApplication:

package com.example.mapper.mybatisMap;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MybatisMapApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatisMapApplication.class, args);
    }
}

8.访问url:

    http://localhost:8080/get?name=迪丽热巴         →  {"id":6,"age":25,"name":"迪丽热巴","role":"浙江路93号-18-1","email":"88xja7gs@hotmail.com","phone":"15107232275"}

 

    http://localhost:8080/insert?id=22&name=迪丽热巴&age=25   →    插入数据:1条

 

声明:本文为作者原创,转载需声明!

posted @ 2018-08-13 17:20  何其小静  阅读(322)  评论(0编辑  收藏  举报