学习记录笔记,狂神yyds!
相关笔记:
Mybatis配置过程的基本步骤
将mybatis-config.xml的部分内容整合到了spring的配置文件中,便于管理的分工!
方式一:
一、理解
个人对其流程的理解:
1.在spring-dao.xml文件中,通过bean配置DataSource,实现对所需连接的数据库信息进行配置,进而代替了原db.properties的作用,或者说是替代了mybatis-config文件中dataSource标签的作用!
2.再通过bean配置sqlSessionFactory,实现sqlSession的创建。
- 在此bean中配置property,dataSource ---->所依赖的数据库信息。
- 在此bean中配置property,configLocation ---->所需要绑定的mybatis的xml文件。
- 在此bean中配置property,apperLocations ---->所管理的mapper.xml的路径。
- 等等。。。
3.通过bean创建sqlSession。
- 此时只能使用构造器注入SQLSessionFactory,因为它没有set方法
4.最后再通过bean将我们所需要的实现类UserMapperImpl注入到xml文件中,这样我们就将其托管在spring上了。
- 定义一个名字,这里是userMapper(可随意),class指定其锁对应的类,通过property配置sqlSession,并指定ref为上面所定义的sqlSession。这里是通过UserMapperImpl文件中的set方法注入的。
5 .通过测试类调用进行测试
- ApplicationContext Context = new ClassPathXmlApplicationContext(“spring-dao.xml”);获取配置文件信息。
- UserMapper userMapper = Context.getBean(“userMapper”, UserMapper.class);获取userMapper这个名字的bean配置,返回类型为UserMapper。
- 这样就得到了userMapper这个对象了,我们就可以通过userMapper来调用接口UserMapper中的方法了。
参考下面的文件~
二、具体配置
1.配置pom.xml文件中的依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-mybatis</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.7</version>
</dependency>
<!--spring操作数据库的话,还需要一个spring-jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.7</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
</dependencies>
<!--为了便于程序找到xml文件-->
<build>
<resources>
<resource>
<directory>src/main/resource</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
2.编写测试类及接口
目录如下:
数据库信息:
User类:
package com.wen.pojo;
import lombok.Data;
@Data
public class User {
private int id;
private String name;
private String pwd;
}
UserMapper接口:
package com.wen.mapper;
import com.wen.pojo.User;
import java.util.List;
public interface UserMapper {
public List<User> selectUser();
}
UserMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wen.mapper.UserMapper">
<select id="selectUser" resultType="user">
select *from mybatis.user;
</select>
</mapper>
3.原mybatis-config.xml文件和现spring-dao.xm(applicationContext。xml)文件的对比
mybatis-config.xml:
<?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>
<typeAliases>
<package name="com.wen.pojo"/>
</typeAliases>
<!--当整合后,下面的都删掉,我注释以示区别-->
<!-- <environments default="development">-->
<!-- <environment id="development">-->
<!-- <transactionManager type="JDBC"/>-->
<!-- <dataSource type="POOLED">-->
<!-- <property name="driver" value="com.mysql.cj.jdbc.Driver"/>-->
<!-- <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC"/>-->
<!-- <property name="username" value="root"/>-->
<!-- <property name="password" value="root"/>-->
<!-- </dataSource>-->
<!-- </environment>-->
<!-- </environments>-->
<!-- <mappers>-->
<!-- <mapper resource="com/wen/mapper/UserMapper.xml"/>-->
<!-- </mappers>-->
</configuration>
整合后
spring-dao.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--DataSource:使用Spring的数据源替换mybatis的配置 c3p0 dbcp druid
我们这里使用Spring提供的jdbc-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--绑定mybatis配置文件-->
<property name="configLocation" value="mybatis-config.xml"/>
<property name="mapperLocations" value="com/wen/mapper/UserMapper.xml"/>
</bean>
<!--sqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能使用构造器注入SQLSessionFactory,因为它没有set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<bean id="userMapper" class="com.wen.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
4.实现类UserMapperImpl:
package com.wen.mapper;
import com.wen.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
public class UserMapperImpl implements UserMapper{
//我们的所有操作都使用sqlSession来执行,在原来,现在都使用SqlSessionTemplate
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public List<User> selectUser() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.selectUser();
}
}
5.测试类MyTest:
import com.wen.mapper.UserMapper;
import com.wen.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class MyTest {
@Test
public void test(){
ApplicationContext Context = new ClassPathXmlApplicationContext("spring-dao.xml");
UserMapper userMapper = Context.getBean("userMapper", UserMapper.class);
List<User> users = userMapper.selectUser();
for (User user : users) {
System.out.println(user);
}
}
}
6.实验结果:
方式二:
UserMapperImpl2
不同点,继承了SqlSessionDaoSupport
package com.wen.mapper;
import com.wen.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> selectUser() {
// SqlSession sqlSession = getSqlSession();
// UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return getSqlSession().getMapper(UserMapper.class).selectUser();
}
}
<bean id="userMapper2" class="com.wen.mapper.UserMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
Talk is cheap,show me the code! —— 薪火工作室!