Spring Series 11
11. Integrate Mybatis
step:
1. Import related jar packages
- junit
- my shoe
- mysql database
- spring-related
- aop weaving
- mybatis-spring integration package
2. Write the configuration file
3. Test
11.1, recall mybatis
- Write entity class
- Write a core configuration file
- write interface
- Write Mapper.xml
- test
11.2、Mybatis-Spring
Official document : http://mybatis.org/spring/zh/getting-started.html
1. Write data source configuration
### spring-dao.xml
<!-- DataSource:使用Spring的数据源替换Mybatis的配置
我们这里使用Spring提供的JDBC
-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
2. Write sqlSessionFactory configuration
### spring-dao.xml
<!-- sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 绑定Mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis_config.xml"/>
<property name="mapperLocations" value="classpath:com/only/mapper/*.xml"/>
</bean>
Define the configuration file of mybatis
### mybatis_config.xml
<configuration>
<typeAliases>
<package name="com.only.pojo"/>
</typeAliases>
</configuration>
3、sqlSessionTemplate
### spring-dao.xml
<!-- sqlSessionTemplate: 就是我们使用的SqlSession -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!-- 只能使用构造器注入sqlSessionFactory,因为它没有set方法 -->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
4. It is necessary to add an implementation class to the interface
### UserMapperImpl.java
public class UserMapperImpl implements UserMapper {
//在原来,我们所有的操作都使用sqlSession来执行,现在都是用SqlSessionTemplate
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
public List<User> selectUser() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.selectUser();
}
}
## User.java
@Data
public class User {
private int id;
private String name;
private String pwd;
}
## UserMapper.xml
<mapper namespace="com.only.mapper.UserMapper">
<select id="selectUser" resultType="user">
select * from mybatis.user;
</select>
</mapper>
5. Inject the implementation class written by yourself into Spring
### applicationContext.xml
<import resource="spring-dao.xml"/>
<bean id="userMapper" class="com.only.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
6. Test
@Test
public void test() throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper mapper = context.getBean("userMapper", UserMapper.class);
List<User> userList = mapper.selectUser();
for (User user : userList) {
System.out.println(user);
}
}
7. Test results
Method 2: Instead of using sqlSessionTemplate directly, use SqlSessionDaoSupport to obtain SqlSession, and getSqlSession()
you will get a SqlSessionTemplate by calling the method.
The steps are still as above, without configuring the sqlSessionTemplate in step 3, directly modify the implementation class for the interface in step 4 as follows:
### UserMapperImpl2.java
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {
public List<User> selectUser() {
return getSqlSession().getMapper(UserMapper.class).selectUser();
}
}
5. Inject the implementation class written by yourself into Spring
### applicationContext.xml
<import resource="spring-dao.xml"/>
<bean id="userMapper" class="com.only.mapper.UserMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>