SpringBoot Mybatis整合(注解版)

SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)

================================

©Copyright 蕃薯耀 2018年4月8日

http://fanshuyao.iteye.com/

 

一、引入Mybatis依赖包:

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>

 数据库连接依赖包:

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <scope>runtime</scope>
</dependency>
		
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
  <version>1.1.9</version>
</dependency>

 

二、建立对应的表和Java实体(过程略)

三、建立实体对应的Mapper(如UserMapper )

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import com.lqy.springboot.bean.User;

@Mapper
public interface UserMapper {

	@Select("select * from user where user_id = #{userId}")
	public User getById(Integer userId);
	
	@Options(useGeneratedKeys=true,keyProperty="userId")
	@Insert("insert into user(user_name,age) values(#{userName},#{age})")
	public Integer save(User user);	
}

 

注意:使用注解版需要在类上加上@Mapper注解,让SpringBoot自动扫描能识别

@Mapper

 问题:如果有很多Mapper接口,能不能一次性扫描呢?

有。

在程序启动入口加入注解:

@MapperScan(basePackages= {"com.lqy.springboot.mapper"})

具体如下:

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

//Mybatis自动扫描包
//@MapperScan(basePackages= {"com.lqy.springboot.mapper"})
@SpringBootApplication
public class SpringbootDatasourceApplication {

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

 这样就能解决多Mapper需要添加注解的麻烦。

 

四、测试Mapper

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

import com.lqy.springboot.bean.User;
import com.lqy.springboot.mapper.UserMapper;

@RestController
public class UserController {

	@Autowired
	private UserMapper userMapper;
	
	@RequestMapping("/getUser")
	public User getUser(Integer userId) {
		if(userId == null) {
			userId = 1;
		}
		return userMapper.getById(userId);
	}
	
	@RequestMapping("/saveUser")
	public User saveUser(User user) {
		userMapper.save(user);
		return user;
	}
	
}

 

五、SpringBoot Mybatis增加驼峰命名规则:

因为是注解版,没有配置文件,所以SpringBoot增加驼峰命名需要增加一个自定义配置类(ConfigurationCustomizer):

import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * mybatis 注解版 
 *
 */
@Configuration
public class MybatisConfig {

	@Bean
	public ConfigurationCustomizer configurationCustomizer() {
		return new ConfigurationCustomizer() {

			@Override
			public void customize(org.apache.ibatis.session.Configuration configuration) {
				configuration.setMapUnderscoreToCamelCase(true);//设置驼峰命名规则
			}
		};
	}
}

 

 

 

================================

©Copyright 蕃薯耀 2018年4月8日

http://fanshuyao.iteye.com/

猜你喜欢

转载自fanshuyao.iteye.com/blog/2415933