1. Use mapperLocations of sqlSessionFactory to load,
<!-- SessionFactory --> <bean id="sqlSessionFactory" scope="singleton"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> <!-- Mapping file paths can be written in one place, or in one place with dao, supporting multiple paths, supporting wildcard characters --> <property name="mapperLocations" value="classpath:mapper/*.xml,classpath:com/sunny/shop/*/dao/*.xml"></property> </bean>
This method can use wildcard characters, specify locations, and use multiple locations,
2. Use MapperScannerConfigurer to scan
<!-- Scan all interfaces under the specified package to create a proxy class. If the configuration file name of mysql is the same as the interface name, you can do not need to configure it one by one-> <bean> <property name="basePackage" value="com.sunny.shop" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean>
This method can scan the interface under the specified package. If you need to scan the configuration file, the configuration file must be in the same directory as the corresponding DAO interface and the name must be the same.
3. Configure mybatis mapper
<mappers> <!-- You can write map files or corresponding interfaces --> <!--<mapper resource="com/mybatis/student/StudentMapper.xml" /> <mapper resource="com/mybatis/classes/ClassesMapper.xml" /> <mapper /> --> </mappers>
The first two are configured in the spring configuration file, and the <mappers> node is configured in the mybatis configuration file.
PS: Here are two ways to load configuration files in mybatis
package com.atguigu.day03_mybaits.test;import java.io.IOException;import java.io.InputStream;import java.io.Reader;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class Test {public static void test1(){///Load the configuration file of mybatis (it also loads the associated mapping file) String str="conf.xml";InputStream is=Test.class.getClassLoader().getResourceAsStream(str);//Build the factory of sqlSessionSessionFactory factory=new SqlSessionFactoryBuilder().build(is);SqlSession session=factory.openSession();//Mapping the idString of sql is to find the namespace+"+select in the allusion file statement="com.atguigu.day03_mybaits.userMapper.getUser";//Execute the query to return a unique user object sqlUser user=session.selectOne(statement, 1);System.out.println(user);}public static void test2() throws IOException{///Load the configuration file of mybatis (it also loads the associated mapping file) String resource = "conf.xml"; //Load the configuration file of mybatis (it also loads the associated mapping file) Reader reader = Resources.getResourceAsReader(resource); //Build the factory of sqlSessionSqlSessionFactory factory=new SqlSessionFactoryBuilder().build(reader);SqlSession session=factory.openSession();//Mapping the sql identity string, which is to find the idString statement="com.atguigu.day03_mybaits.userMapper.getUser" in the allusion file;//Execute the query to return a unique user object user=session.selectOne(statement, 2);System.out.println(user);}public static void main(String[] args) throws IOException {test1();test2();}}Summarize
The above is the method (two ways) for loading configuration files in mybatis introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!