SpringBoot整合mybatis实现mapper.xml方式访问数据库

       SpringBoot整合mybatis实现mapper.xml方式访问数据库

一、项目架构

SpringBoot整合mybatis实现mapper.xml方式访问数据库_第1张图片

二、代码

## pom文件


  4.0.0
  com.yuyou
  springboot-mybatis-demo
  0.0.1-SNAPSHOT
  
  
        org.springframework.boot
        spring-boot-starter-parent
        2.0.1.RELEASE
    
    
    
        UTF-8
        UTF-8
        1.8
    
    
        
            commons-codec
            commons-codec
            1.11
        
        
            org.springframework
            spring-context
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        
        
            mysql
            mysql-connector-java
        
        
            org.projectlombok
            lombok
        
    

##mapper层

---------------------mapper接口-----------------------
package com.yuyou.demo.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import com.yuyou.demo.po.User;

public interface UserMapper {
	
	List getUserList(User user);

}
---------------------mapper接口sql------------------------




    

##service层

---------------------service接口-----------
import java.util.List;

import com.yuyou.demo.po.User;

public interface UserService {

	List selectUser(); 
}
---------------------service接口实现-----------
package com.yuyou.demo.service.impl;

import java.util.List;

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

import com.yuyou.demo.mapper.UserMapper;
import com.yuyou.demo.po.User;
import com.yuyou.demo.service.UserService;

@Service
public class UserServiceImpl implements UserService{

	@Autowired
	private UserMapper userMapper;
	
	@Override
	public List selectUser() {
		User user = new User();
		user.setType(1);
		user.setAge(16);
		return userMapper.getUserList(user);
	}

}

##controller层

package com.yuyou.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.demo.po.User;
import com.yuyou.demo.service.UserService;

@RestController
public class IndexController {

	@Autowired
	private UserService userService;
	
	@GetMapping("/getUser")
	public List getUser(){
		return userService.selectUser();
	}
}

## 配置文件application.yml

server: 
  port: 8080
spring:
  datasource: 
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://ip_address:port/db_name?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true
    username: username
    password: password
mybatis:
    mapper-locations: classpath:mapper/**/*.xml
    type-aliases-package: com.yuyou.demo.po

##启动类

package com.yuyou.demo;

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

@SpringBootApplication
@MapperScan("com.yuyou.demo.mapper")
public class StartUp {

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

三、注意事项

一、可以在每个mapper接口上添加@Mapper注解,这样启动类就不需要@MapperScan

二、mapper.xml文件中写sql语句注意点

1、where 1==1  and col = '#' 和 and col = '#'

2、再进行数值比较时,>可以直接使用,<必须转义<,否者编译异常

三、异常org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

1、配置文件mybatis.mapper-locations配置有误

2、检查xml文件所在package名称是否和Mapper interface所在的包名一一对应

3、检查xml的namespace是否和xml文件的package名称一一对应

4、检查方法名称是否对应

5、去除xml文件中的中文注释

 

你可能感兴趣的:(java)