Springboot 整合Mybatis

一、导入相关依赖

工程目录如下:

Springboot 整合Mybatis_第1张图片

  
    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.1.RELEASE
    
    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-devtools
        

        

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        
        
        
            mysql
            mysql-connector-java
        
    

二、yml 相关配置和mybatis配置文件

# 配置服务器端口和项目的路径
server:
  port: 8080
  servlet:
    context-path: /demo

person:
  name: hs
  age: 12


#DB Configuration:
spring:
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/20190822_mybatis
    username: root
    password: root

useUnicode: true&characterEncoding=utf8

mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml

userMapper.xml




    

数据库信息

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(50) DEFAULT NULL,
`password` VARCHAR(50) DEFAULT NULL,
`name` VARCHAR(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'zhangsan', '123', '张三');
INSERT INTO `user` VALUES ('2', 'lisi', '123', '李四');

三、相关代码

启动类App

package hs;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @Author: huangshun
 * @Date: 2019/8/22 9:18
 * @Version 1.0
 */
@SpringBootApplication
@MapperScan("hs")
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class);
    }
}

实体类User

package hs.domain;

/**
 * @Author: huangshun
 * @Date: 2019/8/22 10:57
 * @Version 1.0
 */
public class User {
    // 主键
    private Long id;
    // 用户名
    private String username;
    // 密码
    private String password;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", name='" + name + '\'' +
                '}';
    }

    public Long getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

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

    // 姓名
    private String name;
}

接口UserMapper

package hs.mappper;
import hs.domain.User;
import org.springframework.stereotype.Repository;
import java.util.List;

/**
 * @Author: huangshun
 * @Date: 2019/8/22 10:59
 * @Version 1.0
 */
@Repository
//@Mapper
public interface UserMapper {

    List findAll();

}

控制层

package hs.controller;

import hs.domain.User;
import hs.mappper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author: huangshun
 * @Date: 2019/8/22 9:19
 * @Version 1.0
 */
@Controller
@RequestMapping("test")
public class TestController {
    @Autowired
    private UserMapper mapper;



    @Value("${person.name}")
    private String name;

    @RequestMapping("hello")
    @ResponseBody
    public String testHelloWorld(){
        System.out.println("hello  world");
        return "success";
    }

    //获取配置文件中的值
    @RequestMapping("name")
    @ResponseBody
    public String getProperties(){
        System.out.println("配置文件中name--"+name);
        return "success";
    }

    @RequestMapping("findAll")
    @ResponseBody
    public String findAll(){
        List users = mapper.findAll();
        System.out.println(users);
        return "aa";
    }
}

启动,访问http://localhost:8080/demo/test/findAll

结束!

你可能感兴趣的:(数据库)