您的位置 首页 java

Mybatis的xml和java配置方式

在开发过程中, Mybatis 可根据自己的喜好,选择xml配置方式和 Java 配置方式,具体配置方式如下:

xml配置方式

 # xml配置方式
mybatis:
  type-aliases-package: com.example.pojo # 实体类所在的包路径
  mapper-locations: classpath:mapper/*.xml # sql的xml映射文件路径
spring:
  datasource:
    url:  jdbc :mysql://192.168.2.213:3306/ApolloConfigDB?characterEncoding=UTF-8&useSSL=true&serverTimezone=GMT%2B8&useUnicode=true # 数据库URL
    driver-class-name: com.my Sql .cj.jdbc.Driver # Mysql驱动
    username: root # MySQL用户名
    password: jayczx120 # MySQL密码  

Java配置方式

 import com.alibaba.druid.pool.DruidDataSource;
import org. apache .ibatis.session.SqlSessionFactory;
import org.mybatis.spring. sql SessionFactoryBean;
import org.springframework.context. annotation .Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

@Configuration
public class MybatisConfig {

    @Bean
    public Druid dataSource  dataSource() {
        //  Druid 数据库连接池     
        Druid DataSource  dataSource = new DruidDataSource();
        //  数据库URL
        dataSource.setUrl("jdbc:mysql://192.168.2.213:3306/ApolloConfigDB?characterEncoding=UTF-8&useSSL=true&serverTimezone=GMT%2B8&useUnicode=true");
        //  MySQL驱动 
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        //  用户名 
        dataSource.setUsername("root");
        //  密码 
        dataSource.setPassword("jayczx120");
        return dataSource;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws  Exception  {
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource());
        //  实体类包路径 
        bean.setTypeAliasesPackage("com.example.pojo");
        //  sql的xml映射文件路径 
        bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
        return bean.getObject();
    }  

具体的文件路径如下:

Mybatis的xml和java配置方式

文件路径

初次写文档,不足之处请多多见谅!!!!!!

文章来源:智云一二三科技

文章标题:Mybatis的xml和java配置方式

文章地址:https://www.zhihuclub.com/193099.shtml

关于作者: 智云科技

热门文章

网站地图