sprigBoot集成之集成mybatis框架

mac2022-06-30  26

sprigBoot集成之集成mybatis框架


3、集成mybatis框架 官网:http://mybatis.org/spring/zh/ 集成springboot插件: http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/ 3.1 添加依赖

<!--mybatis-springBoot 依赖--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency> <!--mysql依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.48</version> </dependency>

​ 3.2 添加配置 ​ 3.2.1 添加mybatis配置

自动注入数据源创建sqlSessionFactory配置model类的别名解析映射文件 PathMatchingResourcePatterResolver获取Mapper文件的路径 import javax.sql.DataSource; //mybatis的配置 @Configuration @MapperScan("com.kp.myboot.**.dao")//扫描com.kp.myboot包下任意子包的中的dao的包 扫描映射文件和接口 public class MybatisConfig { @Autowired DataSource dataSource; @Bean public SqlSessionFactory sqlSessionFactory () throws Exception{ //使用SqlSessionFactoryBean 管理数据源 SqlSessionFactoryBean sqlSessonFactory = new SqlSessionFactoryBean(); sqlSessonFactory.setDataSource(dataSource); //设置别名包 sqlSessonFactory.setTypeAliasesPackage("com.kp.myboot.**.model");//扫描model PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); // 设置映射文件所在的路径 sqlSessonFactory.setMapperLocations(resolver.getResources("classpath*:**/sqlmap/*.xml")); return sqlSessonFactory.getObject(); } }

​ 3.2.2 添加数据源

#添加数据源 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test1212?characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=root

​ 3.2.3 修改启动类

@SpringBootApplication(scanBasePackages = {"com.kp.myboot"})//默认设置也是扫描该包下子包 public class MybootApplication { public static void main(String[] args) { SpringApplication.run(MybootApplication.class, args); } }

3.3 生成mybatis模块 参考官网: http://mybatis.org/generator/ https://mp.baomidou.com/ 3.3.1 添加自动生成代码的插件依赖

<plugins> <!--mybatis自动代码插件--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.7</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.48</version> </dependency> </dependencies> </plugin> </plugins>

3.3.2 修改自动生成代码的配置和mabatis的配置一致 3.4 编写服务类 3.4.1 在生成的代码中Mapper接口中,添加一个findAll的方法,并且提供sql语句 3.5.1 编写服务接口和实现类

@Service public class AutoCodeServiceImpl implements AutoCodeService { @Autowired private AutoCodeMapper autoCodeMapper; @Override public List<AutoCode> findAll() { return autoCodeMapper.findAll(); } }

3.5.2 编写控制器,在接口文档中测试

@RestController public class AutoCodeController { @Autowired private AutoCodeService autoCodeService; @RequestMapping("/findAll") public Object findAll(){ return autoCodeService.findAll(); } }

3.5 配置打包资源 如果打包没有拷贝资源文件,设置pom文件

<!-- 打包时拷贝MyBatis的映射文件 --> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/sqlmap/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> <filtering>true</filtering> </resource> </resources>

3.6 编译运行测试 http://localhost/findAll 3.7 声明式事务 配置类添加

@SpringBootApplication @EnableTransactionManagement //如果mybatis中service实现类中加入事务注解,需要此处添加该注解service方法添加开启事务 @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
最新回复(0)