使用idea整合springboot和mybatis

可以再pom中加一个数据库连接池

		
		
			com.alibaba
			fastjson
			1.2.47
		

1.新建项目,选择spring initializr->next 设置group和artifact->next选择web中的web和sql中的jpa,mysql,mybatis->设置项目名生成项目
2.在application中配置mybatis和数据源(此处使用的是yml格式的配置文件)

#mybatis配置
mybatis:
  typeAliasesPackage: mybatis
  mapperLocations: classpath:*.xml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/tutf-8?characterEncoding=utf-8
    username: root
    password: 107156
    driver-class-name: com.mysql.jdbc.Driver

3.配置sql语句映射文件






    
        
        
        
    

        

4.编写实体类user

package ztc.springboot_rocketmq_mybatis.mybatis;
public class User {
    private Integer id;
    private String name;
    private String realName;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getRealName() {
        return realName;
    }
    public void setRealName(String realName) {
        this.realName = realName;
    }

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

5.编写sql语句接口

package ztc.springboot_rocketmq_mybatis.mybatis.mapper;

import org.springframework.stereotype.Repository;
import ztc.springboot_rocketmq_mybatis.mybatis.User;
import java.util.List;

@Repository
public interface UserMapper {
        public List getUserList();
}

6.编写service接口层

package ztc.springboot_rocketmq_mybatis.mybatis.service;

import ztc.springboot_rocketmq_mybatis.mybatis.User;
import java.util.List;

public interface UserService {
    public List getUserList();
}

7.编写service接口实现层

package ztc.springboot_rocketmq_mybatis.mybatis.service.serviceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ztc.springboot_rocketmq_mybatis.mybatis.User;
import ztc.springboot_rocketmq_mybatis.mybatis.mapper.UserMapper;
import ztc.springboot_rocketmq_mybatis.mybatis.service.UserService;

import java.util.List;
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public List getUserList() {
        return userMapper.getUserList();
    }
}

8.编写controller层进行测试

package ztc.springboot_rocketmq_mybatis.rocketmq;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ztc.springboot_rocketmq_mybatis.mybatis.User;
import ztc.springboot_rocketmq_mybatis.mybatis.service.UserService;
import java.util.List;

@RestController
public class TestController {
    @Autowired
    UserService userService;
    @RequestMapping("/testMQ")
    public String testMq() {
        List users=userService.getUserList();
        String str= JSON.toJSONString(users);
        return str;
    }
}

你可能感兴趣的:(使用idea整合springboot和mybatis)