(六)Maven+SpringBoot整合Mybatis+SQL Server

1、在上文提到的整合项目中,添加依赖


    
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        2.0.0
    
	
    
        com.microsoft.sqlserver
        mssql-jdbc
        runtime
    

    	channel
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.2
                
                    true
                    true
                
            
            
                maven-compiler-plugin
                
                    1.8
                    1.8
                
            
        
    

2、在application.yml中配置mybatis和数据源等信息

#公共配置与profiles选择无关
mybatis:
  typeAliasesPackage: com.ljb.channel.entity #实体类所在包
  mapperLocations: classpath:mapper/*.xml #配置文件所在包
  
#开发配置
spring:
  datasource:
    url: jdbc:sqlserver://localhost:1433;databaseName=数据库名称
    username: 用户名
    password: 密码
    driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
  ...

 

3、创建关键目录如下

(六)Maven+SpringBoot整合Mybatis+SQL Server_第1张图片

4、在APP.java类中添加注解

@EnableTransactionManagement
@SpringBootApplication
@MapperScan("com.ljb.channel.dao")
public class ChannelApp
{
    public static void main( String[] args )
    {
    	SpringApplication.run(ChannelApp.class, args);
    }
}

5、至此配置文件,创建好数据库写好controller即可访问,下面是核心代码




  
    
    
    
  
  
    Id, CATEGORYNAME, CATEGORYDESC
  
  
  
  
  
  
  
  
  
  

  ...
package com.ljb.channel.dao;

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

import com.ljb.channel.entity.Category;

public interface CategoryMapper {
	
	List selectAll();
	
	Category selectByPrimaryKey(String id);
	
	Category selectByName(String categoryname);
	
	List selectCategoryList(Map map);

}
package com.ljb.channel.controller;

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

import javax.servlet.http.HttpServletRequest;

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.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ljb.channel.entity.Category;
import com.ljb.channel.service.CategoryService;
import com.ljb.channel.util.Result;
import com.ljb.channel.util.UUIDTool;

@Controller
@RequestMapping("/categoryController")
public class CategoryController {

	@Autowired
	CategoryService categoryService;
	
	@RequestMapping(value = "/queryCategory", method = RequestMethod.POST)
	@ResponseBody
    public Result queryCategory() {
		Result result = new Result();
		String msg = "";
		Map map = new HashMap(); 
		try {
			Map fileps = new HashMap();//用于查询列表数据
			List list = categoryService.getCategoryList(fileps);
			map.put("data", list);//数据集合
		}catch(Exception e) {
			e.printStackTrace();
			msg = "数据获取失败!";
		}
        result.setExtend(map);
		result.setSuccess(true);
		result.setMsg(msg);
		return result;
    }	
	
}
package com.ljb.channel.service.impl;

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

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ljb.channel.dao.CategoryMapper;
import com.ljb.channel.entity.Category;
import com.ljb.channel.service.CategoryService;

@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {

	@Resource
	CategoryMapper categoryMapper;
	

	@Override
	public List getCategoryList(Map map) {
		List list = categoryMapper.selectCategoryList(map);
		return list;
	}

}

SpringBoot系列文章请查看,更多内容正在更新中

(一)Myeclipse2018创建Maven项目

(二)Maven整合SpringBoot

(三)Maven+SpringBoot整合thymeleaf

(四)Maven+SpringBoot+thymeleaf 配置热部署devtools

(五)Maven+SpringBoot整合Bootstrap

(六)Maven+SpringBoot整合Mybatis+SQL Server

(七)SprintBoot项目打包成jar并做成后台运行的服务

 

你可能感兴趣的:(Maven,SQL,Server,SpringBoot,Mybatis)