由于公司项目微服务采用的springBoot框架,感觉这玩意在了解之后着实好用。本文将分为三个步骤构建一个SpringBoot整合Mybatis的小项目。
1.配置依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mysql连接 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.18</version>
</dependency>
<!--Json Support-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.43</version>
</dependency>
<!--Swagger support-->
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>0.9.5</version>
</dependency>
</dependencies>
本文采用的阿里的druid数据源,大家如果不想引用可以使用默认的数据源org.apache.tomcat.jdbc.pool.DataSource。因为mybatis-spring-boot-starter已经集成了mybatis,所以不需要额外引入mybatis的包。
2.数据源配置
package com.example.myproject.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration
/**
* 这种注解方式也是可行的
* @MapperScan(value="com.example.myproject.mapper",basePackages="com.example.myproject.mapper",sqlSessionFactoryRef = "sqlSessionFactory")
* 扫描mapper包,并引入对应的sqlSessionFactory
*/
@MapperScan("com.example.myproject.mapper")
public class RepositoryConfig {
/**
* 读取application.properties里面的数据源信息,并赋值给数据源配置
* */
@Bean
@ConfigurationProperties(prefix="jdbc")
public DruidDataSource dataSource() {
String driverClassName = null;
String url = null;
String userName = null;
String password = null;
final DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(userName);
dataSource.setPassword(password);
return dataSource;
}
/**
* 设置SqlSessionFactory配置
* */
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
ssfb.setDataSource(dataSource());
ssfb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/**/*.xml"));
return ssfb.getObject();
}
/**
* 事务配置
* */
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
}
application.properties配置
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=utf-8
jdbc.userName=root
jdbc.password=admin
其实到这步就已经大功告成了,其他的配置如同我们用xml配置一样.
3.其他代码
Controller代码块
@RestController
public class UserController {
@Autowired
private UserService UserService;
@RequestMapping("/getUser")
public User getUser(Integer id){
return UserService.getUser(id);
}
}
Service代码块
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getUser(Integer id) {
return userMapper.getUser(id);
}
}
mapper代码块
<mapper namespace="com.example.myproject.mapper.UserMapper">
<select id="getUser" resultType="com.example.myproject.model.User" parameterType="int">
select
id,
name,
age
from user
where id = #{id}
</select>
</mapper>
4.总结
在使用springBoot整合mybatis的时候,大多时候采用注解的方式,当然你也可以用xml配置的方式,然后用@importSource引入.当然采用注解的方式可以更直接的OOP也去除了繁杂的xml配置更加契合了springBoot的设计初衷.在配置的时候可以多联想当初Spring整合mybatis的配置,其实一切就会都变得非常简单了!