码迷,mamicode.com
首页 > 编程语言 > 详细

spring boot 与 Mybatis整合(*)

时间:2019-03-11 01:03:08      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:prope   lis   int   数据库   pen   ack   rgs   enc   gap   

  1. 在pom.xml文件中加入数据库、spring-mybatis整合
    <!-- spring boot 整合mybatis -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.3.0</version>
            </dependency>
            <!-- MYSQL -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
  2. 在springboot的启动类中添加注解
    package cn.java.controller;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /*
     * spring boot 启动程序
     */
    @SpringBootApplication(scanBasePackages= {"cn.java.controller","cn.java.service.impl"})
    @EnableAutoConfiguration
    //配置mapper层扫描
    @MapperScan(basePackages="cn.java.mapper")
    public class StartApplication {
        public static void main(String[] args) {
            SpringApplication.run(StartApplication.class, args);
        }
    
    }

     

 

控制层GoodController 

package cn.java.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.java.service.GoodService;

@Controller
@RequestMapping("/goods/")
public class GoodController {
    @Autowired
    private GoodService goodService;
    //获取goods表中所有数据
    @RequestMapping("/selectAllGoods.do")
    @ResponseBody
    public List<Map<String,Object>> selectAllGoods(){
        return goodService.getAll();
    }
}

 

 业务层

package cn.java.service;

import java.util.List;
import java.util.Map;

public interface GoodService {

    List<Map<String, Object>> getAll();

}
package cn.java.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.java.mapper.GoodMapper;
import cn.java.service.GoodService;
@Service
public class GoodServiceImpl implements GoodService {
    /*
     * 获取springboot库下goods表中所有的数据 
     */
    @Autowired
    private GoodMapper goodMapper;
    
    /*
     * 查询所有商品
     */
    
    /* (non-Javadoc)
     * @see cn.java.service.impl.GoodService#getAll()
     */
    @Override
    public List<Map<String,Object>> getAll(){
        return goodMapper.getAllGoods();
    }
}

DAO层

package cn.java.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Select;

public interface GoodMapper {
    @Select("SELECT * FROM goods")
    public List<Map<String,Object>> getAllGoods();
}

application.properties

#update tomcat port
server.port=8888

#config datasource(mysql)
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql:///springboot
spring.datasource.username=root
spring.datasource.password=78978r

数据库springboot

CREATE TABLE `springboot`.`goods`(  
  `id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT ‘主键‘,
  `good_name` VARCHAR(40) COMMENT ‘商品名称‘,
  `good_price` FLOAT(10,2) COMMENT ‘商品单价‘,
  `good_phone` VARCHAR(20) COMMENT ‘商品联系电话‘,
  `good_address` VARCHAR(40) COMMENT ‘商品地址‘,
  `good_num` INT COMMENT ‘商品库存‘,
  PRIMARY KEY (`id`)
);

 

 

 控制层与业务层  业务层与DAO层 通过接口连接

 

技术图片

 

spring boot 与 Mybatis整合(*)

标签:prope   lis   int   数据库   pen   ack   rgs   enc   gap   

原文地址:https://www.cnblogs.com/mzdljgz/p/10508148.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!