SpringBoot Mybatis整合(註解版),SpringBoot集成Mybatis(註解版)java
================================mysql
©Copyright 蕃薯耀 2018年4月8日web
http://www.cnblogs.com/fanshuyao/spring
源代碼下載見:http://fanshuyao.iteye.com/blog/2415933sql
1、引入Mybatis依賴包:數據庫
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>1.3.2</version>
- </dependency>
數據庫鏈接依賴包:apache
- <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>
2、創建對應的表和Java實體(過程略)springboot
3、創建實體對應的Mapper(如UserMapper )session
- 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自動掃描能識別mybatis
問題:若是有不少Mapper接口,能不能一次性掃描呢?
有。
在程序啓動入口加入註解:
- @MapperScan(basePackages= {"com.lqy.springboot.mapper"})
具體以下:
- import org.mybatis.spring.annotation.MapperScan;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication
- public class SpringbootDatasourceApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(SpringbootDatasourceApplication.class, args);
- }
- }
這樣就能解決多Mapper須要添加註解的麻煩。
4、測試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;
- }
-
- }
5、SpringBoot Mybatis增長駝峯命名規則:
由於是註解版,沒有配置文件,因此SpringBoot增長駝峯命名須要增長一個自定義配置類(ConfigurationCustomizer):
- import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- @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://www.cnblogs.com/fanshuyao/