Use Spring to integrate MyBatis to create the Mapper objects you need

Spring integration MyBatis official documentation: mybatis.org/spring/zh/f…

Mybatis create Mapper object

  1. sqlSessionFactory
  2. sqlSession
  3. UserMapper

Create Mapper with Spring

Since Spring is used to create Mybatis Mapper proxy object; So the above steps are necessary, only in Spring to help us, but we still need to write the necessary configuration files:

1. Guide package:

  • Import mybatis package

  • Mysql driver package

  • Mybatis – Spring integrated package

  • Spring JDBC/ORM support package

  • Other packages (log4j)

    [Note] The version of mybatis- Spring must match the version of Mybatis. Otherwise, an error is reported

General engineering: To use the MyBatis -spring module: just include the MyBatis -spring-2.0.6.jar file and related dependencies in the classpath. Download address: repo. Spring. IO/release/org… Maven project: If you use Maven as a build tool, simply add the following code to pom.xml:

< the dependency > < groupId > org. Mybatis < / groupId > < artifactId > mybatis - spring < / artifactId > < version > 2.0.6 < / version > </dependency> 12345Copy the code

2. Configure the XML file:

  • Beans-datasource. XML (Spring generated mybatis object XML file)

The Properties file is configured as follows:

url=jdbc:mysql://localhost:3306/easybuy? useUnicode=true&amp&characterEncoding=utf-8 driver=com.mysql.jdbc.Driver username2=root password=123456 1234Copy the code

The beans-datasource. XML file is configured as follows:

<? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <! --> <context:property-placeholder location="database.properties"/> <! - 2, the datasource data source - > < bean id = "datasource" class = "HTTP: / / org.apache.com mons. DBCP. BasicDataSource" > < property name="driverClassName" value="${driver}" /> <property name="url" value="${url}"/> <property name="username" value="${username2}"/> <property name="password" value="${password}"/> </bean> <! - 3, sqlSessionFactory - > < bean id = "sqlSessionFactory" class = "org. Mybatis. Spring. SqlSessionFactoryBean" > <! -- Alias --> <property name="typeAliasesPackage" value="com.cc.model"></property> <! <property name=" classpath*:mapper/* mapper.xml "></property> <! DataSource ="dataSource"></property> </bean> <! - mapper package location - > < bean class = "org. Mybatis. Spring. Mapper. MapperScannerConfigurer" > < property name = "basePackage" value="mapper"></property> </bean> <! - 5, transaction management -- -- > < bean id = "txManager" class = ". Org. Springframework. JDBC datasource. DataSourceTransactionManager "> < property name="dataSource" ref="dataSource"/> </bean> </beans> 12345678910111213141516171819202122232425262728293031323334353637383940Copy the code
  • Mybatis -config.xml (mybatis own configuration file)

    123456789101112131415161718192021222324252627282930313233

  • Xxxmapper.xml (mybatis interface file)

3, write test class:

/** *@auther Nical */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring-datasource.xml") public class TestMyBatis { @Autowired UserMapper userMapper; @test public void testUser() { List<User> users = userMapper.selectById(1); System.out.println("users==>"+users); }} 1234567891011121314Copy the code

Conclusion:

  1. ${username} in spring-database. XML is the username of the Windows operating system by default
  2. Mybatis 1.2.2 can only integrate mybatis version 3.2, but cannot inherit the latest 3.5.5 requires maven repository to obtain mybatis version 2.x JAR

Also welcome to pay attention to my public number: Java programmers gathering place, Maidong will share Java related technical articles or industry information every day, welcome to pay attention to and forward the article!