springboot整合mybatis注解版,mybatis纯注解开发
今天给大家分享纯注解版弹簧与框架的整合
框架包下:有这几个,上面图片没有展开
配置Bean:MyBatisAutoConfiguration
用户
数据层
MybatisProperties
MyBatisAutoConfiguration:
包com。白芝娥都。mybatis导入com。阿里巴巴。德鲁伊。游泳池。德鲁伊数据源;导入org。我的巴蒂斯。春天。sqlsessionfactorybean导入org。我的巴蒂斯。春天。注释。映射器扫描;导入org。spring框架。语境。注释。豆;导入org。spring框架。语境。注释。组件可以;导入org。spring框架。语境。注释。配置;导入org。spring框架。核心。io。类路径资源;导入org。spring框架。核心。io。资源;导入组织。spring框架。核心。io。支持。pathmatchingresourcepatternresolver;导入org。spring框架。核心。io。支持。resourcepatternresolver导入javax。SQL。数据来源;导入Java。io。io异常;@ Configuration @ components可以(基础包= com。白芝娥都。my batis )@ MapperScan(base packages= com。白芝娥都。mybatis )//扫描大刀接口对象公共类MyBatisAutoConfiguration { //连接池对象@ Bean公共数据源data source(){ druid数据源data source=new druid数据源();数据来源。setdriver类名( com。MySQL。JDBC。司机’);数据来源。seturl( JDBC :我的SQL ://localhost :3306/mybatis?use unicode=trueampcharaterecording=utf-8’);数据来源。设置用户名(“root”);数据来源。设置密码( 123456 );返回数据源;} //工厂对象@ Bean public SqlSessionFactoryBean SqlSessionFactoryBean(数据源){ SqlSessionFactoryBean SqlSessionFactoryBean=new SqlSessionFactoryBean();sqlsessionfactorybean。设置数据源(data source);sqlsessionfactorybean。settypealiasespackage( com。白芝娥都。我的batis’);//设置制图人文件的路径,这个只会扫描一个制图人文件,如果需要扫描多个文件,则需要让他扫描包sqlsessionfactorybean。setmapperlocations(新的类路径资源( userdaomapper。XML’));//通配写法让他扫描包下的所有制图人文件资源请尝试{ ResourcePatternResolver resolver=new PathMatchingResourcePatternResolver();资源[]资源
=resolver.getResources("com.baizhiedu.mapper/*Mapper.xml"); sqlSessionFactoryBean.setMapperLocations(resources); } catch (IOException e) { e.printStackTrace(); } return sqlSessionFactoryBean; }}User:
package com.baizhiedu.mybatis; public class User { private Integer id; private String name; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}
UserDAO:
package com.baizhiedu.mybatis; public interface UserDAO { public void save(User user);}
UserDAOMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.baizhiedu.mybatis.UserDAO"> <!--使用insert,update,delete,select标签来写sql语句--> <insert id="save" parameterType="User"> insert into tb_users (name,password) values (#{name},#{password}) </insert> </mapper>
TestMyBatis:
package com.baizhiedu; import com.baizhiedu.mybatis.MyBatisAutoConfiguration;import com.baizhiedu.mybatis.User;import com.baizhiedu.mybatis.UserDAO;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class TestMyBatis { //用于测试Spring+Mybatis注解的整合 @Test public void test1(){ ApplicationContext ctx=new AnnotationConfigApplicationContext(MyBatisAutoConfiguration.class); UserDAO userDAO=(UserDAO)ctx.getBean("userDAO"); User user=new User(); user.setName("annotation1"); user.setPassword("123456"); userDAO.save(user); }}
上面配置Bean:MyBatisAutoConfiguration:存在数据耦合,可以通过配置文件来解耦合:
mybatis.properties:
mybatis.driverClassName=com.mysql.jdbc.Drivermybatis.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&charaterEcoding=utf-8mybatis.username=rootmybatis.password=123456mybatis.typeAliasesPackages=com.baizhiedu.mybatismybatis.mapperLocations=com.baizhiedu.mapper/*Mapper.xml
定义类来封装配置文件中的配置信息:MybatisProperties:
package com.baizhiedu.mybatis; import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component; @Component@PropertySource("classpath:mybatis.properties") //读取配置文件public class MybatisProperties { @Value("${mybatis.driverClassName}") private String driverClassName; @Value("${mybatis.url}") private String url; @Value("${mybatis.username}") private String username; @Value("${mybatis.password}") private String password; @Value("${mybatis.typeAliasesPackages}") private String typeAliasesPackages; @Value("${mybatis.mapperLocations}") private String mapperLocations; public String getDriverClassName() { return driverClassName; } public void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getTypeAliasesPackages() { return typeAliasesPackages; } public void setTypeAliasesPackages(String typeAliasesPackages) { this.typeAliasesPackages = typeAliasesPackages; } public String getMapperLocations() { return mapperLocations; } public void setMapperLocations(String mapperLocations) { this.mapperLocations = mapperLocations; }}
更改配置MyBatisAutoConfiguration:
package com.baizhiedu.mybatis; import com.alibaba.druid.pool.DruidDataSource;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.annotation.MapperScan;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.Resource;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.core.io.support.ResourcePatternResolver; import javax.sql.DataSource;import java.io.IOException; @Configuration@ComponentScan(basePackages = "com.baizhiedu.mybatis")@MapperScan(basePackages = "com.baizhiedu.mybatis") //扫描dao接口对象public class MyBatisAutoConfiguration { @Autowired //注解 自定义类型注入,用来解耦合 private MybatisProperties mybatisProperties; //连接池对象 @Bean public DataSource dataSource(){ DruidDataSource dataSource=new DruidDataSource(); dataSource.setDriverClassName(mybatisProperties.getDriverClassName()); dataSource.setUrl(mybatisProperties.getUrl()); dataSource.setUsername(mybatisProperties.getUsername()); dataSource.setPassword(mybatisProperties.getPassword()); return dataSource; } //工厂对象 @Bean public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){ SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); sqlSessionFactoryBean.setTypeAliasesPackage(mybatisProperties.getTypeAliasesPackages()); //设置Mapper文件的路径,这个只会 扫描一个Mapper文件,如果需要扫描多个文件,则需要让他扫描包 sqlSessionFactoryBean.setMapperLocations(new ClassPathResource("UserDAOMapper.xml")); //通配写法让他扫描包下的所有Mapper文件资源 try { ResourcePatternResolver resolver=new PathMatchingResourcePatternResolver(); Resource[] resources=resolver.getResources(mybatisProperties.getMapperLocations()); sqlSessionFactoryBean.setMapperLocations(resources); } catch (IOException e) { e.printStackTrace(); } return sqlSessionFactoryBean; }}
TestMyBatis:test:
到此这篇关于基于纯注解实现spring与mybatis的整合的文章就介绍到这了,更多相关spring与mybatis整合内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。