Spring07:整合Mybatis
pom.xml导入依赖
junit
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
mybaits
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
mysql-connector-java
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
spring相关
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
aop
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
mybatis-spring整合包
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
配置Maven静态资源过滤问题!
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
dataSource数据库的连接
spring整合mybatis时,数据库的连接交给一个bean对象来进行
<!-- 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/book"/>
<property name="username" value="book"/>
<property name="password" value="123456"/>
</bean>
创建SqlSessionFactory的bean对象
<!-- SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 绑定mmybatis配置文件-->
<property name="configLocation" value="classpath:SqlMybatis.xml"/>
<property name="mapperLocations" value="classpath:com/itheima/dao/*.xml"/>
</bean>
创建SqlSession的bean对象
<!-- SqlSessionTemplate就是我们所说的sqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!-- 只能使用构造器注入sqlSessionFactory-->
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
创建User实例类和UserDao的接口
pojo
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String username;
private String password;
}
dao
public interface UserDao {
List<User> selectUser();
}
接口实现类UserDaoImpl
现在思考这样的问题,我们在之前使用mybatis时的逻辑时先获取到sqlSession的对象,在通过getMapper获取的响应的mapper,再调用方法。
然而使用了spring的ioc容器后,我们的sqlSession已经以bean对象存在容器中,可以直接使用。那么我们就不妨在UserDaoImpl的实现类中,定义一个sqlSession的数据域。将UserDaoImpl注册到ioc容器中,注入sqlSession的bean对象,便可以进行操作了
//需要给接口加实现类
public class UserDaoImpl implements UserDao{
//原来我们的所有的操作都在SqlSession执行,现在都是用SqlSessionTemplate
private SqlSessionTemplate sqlSessionTemplate;
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
@Override
public List<User> selectUser() {
UserDao userDao = sqlSessionTemplate.getMapper(UserDao.class);
return userDao.selectUser();
}
}
注入ioc容器中
<bean id="userDaoImpl" class="com.itheima.dao.UserDaoImpl">
<property name="sqlSessionTemplate" ref="sqlSession"/>
</bean>
Test测试
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserMapper mapper = (UserMapper) context.getBean("userDao");
List<User> user = mapper.selectUser();
System.out.println(user);
}