IDEA新建mybatis-config.xml配置文件

 

在使用IDEA使用Mybatis时,在把mybatis所依赖的包通过Maven加载后,就要新建mybatis-config.xml配置文件。我以为是可以直接通过右键然后创建mybatis-config.xml文件,事实证明,我找不到(还是真的我找不到)。我们来把Mybatis-config.xml配置文件的模板配置到IDEA中,方便以后使用。

如果使用新建文件的方式

配置mybatis-config.xml到IDEA中

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <properties resource=""></properties>
    <typeAliases>

        <package name=""/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
        <transactionManager type="JDBC"></transactionManager>
        <dataSource type="POOLED">
            <property name="driver" value="${database.driver}"/>
            <property name="url" value="${database.url}"/>
            <property name="username" value="${database.username}"/>
            <property name="password" value="${database.password}"/>
        </dataSource>
    </environment>
    </environments>
    <mappers>
        <mapper resource=""></mapper>
    </mappers>

</configuration>

总结

这样在以后写Mbatis配置文件时就方便很多,直接右键新建就可以了。通过这样的配置,其他我们可以根据我们自己的需求,来配置方便我们编程的许多编程模板。包括映射器的模板也行。

在Spring Boot中配置MyBatis的方式有以下几种: 1. 使用application.properties或application.yml配置文件 在Spring Boot项目的src/main/resources目录下,新建application.properties或application.yml文件,添加MyBatis的配置信息,例如: ``` mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.example.demo.model ``` 2. 使用@MapperScan注解 在Spring Boot的启动类上添加@MapperScan注解,指定Mapper接口所在的包名,例如: ``` @SpringBootApplication @MapperScan("com.example.demo.mapper") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 3. 使用SqlSessionFactoryBean配置 在application.properties或application.yml文件中配置MyBatis的数据源信息,并在@Configuration类中创建SqlSessionFactoryBean对象,例如: 在application.properties或application.yml文件中添加: ``` spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 在@Configuration类中添加: ``` @Configuration public class MyBatisConfig { @Autowired private DataSource dataSource; @Bean public SqlSessionFactoryBean sqlSessionFactoryBean() { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); return factoryBean; } } ``` 以上是在Spring Boot中配置MyBatis的三种方式,可以根据自己的需求选择其中一种方式进行配置。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值