Basic introduction
In fact, the so-called Spring integration of Mybatis is to entrust the loading process of Mybatis to Spring hosting, without loading configuration tools and other operations, and the specific DAO layer operation is still to use Mybatis to operate the database.
Mybatis application steps:
-
Mybatis -config. XML core configuration file, configure the basic environment support: data source, driver, URL, username, password…
-
Then write the mybatisUtil utility class, load the mybatis-config. XML Resource as an IO stream, and create an SqlSessionFactory with the SqlSessionFactoryBuilder. And the SqlSessionFactory factory is encapsulated as a singleton factory. Finally, only a SqlSession session access interface is thrown!
-
Finally DAO layer through SqlSession to obtain Mapper for SQL execution!
Here is a personal collation of some information, the need of friends can be directly click to receive.
Java Basics
22 Java Architect Core books
Learning routes and materials from 0 to 1Java
1000+ questions from 2021
Spring integration mybatis steps:
-
Mybatis -config. XML environment configuration: data source, driver, URL, username, password… These basic configurations are handed over to Spring’s core configuration file application.xml! Create the bean object dataSource instead of Resource.
-
SqlSessionFactory = SqlSessionFactory = SqlSessionFactory = SqlSessionFactory (It is recommended to configure it as a singleton according to the custom of Mybatis)
-
Then inject the Configration configuration from the mybatis-config. XML configuration file into the SqlSessionFactory as dependency injection, and everything in mybatis-config. XML can take effect in Spring.
-
Finally, the SqlSession retrieval will also be given to Spring hosting, in the form of constructor injection SqlSessionFactory into SqlSession dependency!
-
All subsequent DAO layer operations obtain SqlSession in the form of getBean, and then execute SQL; Compared to the myBatis steps above, integration basically leaves myBatis loading in Spring’s hands, and that’s it.
Mybatis core configuration
<? The XML version = "1.0" encoding = "UTF8"? > <! DOCTYPE configuration PUBLIC "- / / mybatis.org//DTD Config / 3.0 / EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" > <configuration> <settings> <! Setting name="logImpl" value="STDOUT_LOGGING"/> <! Setting name="cacheEnabled" value="true"/> <! <setting name="mapUnderscoreToCamelCase" value="true"/> </ Settings > <! -- aliases --> <typeAliases> <typeAlias type="com.pojo.User" alias="User"/> </typeAliases> <! <mappers> <mapper class=" com.dao.usermapper "/> </mappers> </configuration>Copy the code
Spring Core Configuration
<? The XML version = "1.0" encoding = "UTF8"? > <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" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="com.dao"/> <context:annotation-config/> <! -- 1. Integrate mybatis configuration environment into Spring, To the Spring managed - > < bean id = "dataSource" class = "com. Alibaba. Druid. Pool. DruidDataSource" > < property name = "driverClassName." value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis? useSSL=true& useUnicode=true& characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <! - 2. SqlSessionFactory to Spring managed - > < bean id = "SqlSessionFactory" class = "org. Mybatis. Spring. SqlSessionFactoryBean" scope="singleton"> <property name="dataSource" ref="dataSource"/> <! Load the data environment --> <! <property name="configLocation" value=" clasSPath :mybatis-config.xml"/> <! -- Almost all things can be matched in this, completely do not need mybatis core configuration according to personal preference, personal feeling separate point maintenance is easier --> </bean> <! - 3. The load of SqlSession objects to Spring managed - > < bean id = "SqlSession" class = "org. Mybatis. Spring. SqlSessionTemplate" > <! -- According to the custom of Mybatis, <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>Copy the code
Database and corresponding POJO entity classes
package com.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class User {
private int id;
private String username;
private String password;
}
Copy the code
Dao layer
UserMapper interface
package com.dao;
import com.pojo.User;
import java.util.List;
public interface UserMapper {
public List<User> getUserList();
}
Copy the code
UserMapper. XML configuration
<? The XML version = "1.0" encoding = "UTF8"? > <! DOCTYPE mapper PUBLIC "- / / mybatis.org//DTD mapper / 3.0 / EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > < mapper namespace="com.dao.UserMapper"> <! <id column="id" property="id"/> <id column="name" property="username"/> <id column="pwd" property="password"/> </resultMap> <select id="getUserList" resultMap="map"> select *from user; </select> </mapper>Copy the code
Interface Implementation Class (1)
This implementation class is not needed when using Mybatis alone, and the impL implementation class does the work of manipulating the database to the Service layer. Now you need the implementation class to internally aggregate the SqlSessionTemplate object (equivalent to SqlSession), then get the Mapper in the implementation method by reflection, and then execute SQL!
import com.pojo.User; import org.mybatis.spring.SqlSessionTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import java.util.List; @Repository("userMapperImpl") public class UserMapperImpl implements UserMapper{ @Autowired private SqlSessionTemplate sqlSession; @Override public List<User> getUserList() { UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> list = mapper.getUserList(); return list; }}Copy the code
Interface Implementation Class (2)
In the latest spring-Mybatis integration, the SqlSessionDaoSupport abstract class is provided to replace the internal aggregate SqlSessionTemplate object, and you only need to inherit this class. Although dependency injection is not explicitly required, the parent class does require dependency injection into an SqlSessionFactory factory!
import com.pojo.User; import org.apache.ibatis.session.SqlSession; import org.mybatis.spring.support.SqlSessionDaoSupport; import java.util.List; public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{ @Override public List<User> getUserList() { SqlSession sqlSession = getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> list = mapper.getUserList(); return list; }}Copy the code
test
@Test public void test1(){ ApplicationContext context = new ClassPathXmlApplicationContext("application.xml"); UserMapper userMapperImpl = context.getBean("userMapperImpl", UserMapper.class); List<User> list = userMapperImpl.getUserList(); for (User user : list) { System.out.println(user.toString()); }}Copy the code
The last
Now that you’ve read this, please give a thumbs up if you think this article is helpful!