pom.xml中导入相关jar包
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.4</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>com.spring</groupId>
<artifactId>spring_mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
</dependencies>
方式一:
1.新建spring的配置文件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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
2.配置dataSource数据源
<!--配置DataSource数据源:使用spring数据源替换mybatis的配置-->
<!--使用spring提供的jdbc:org.springframework.jdbc.datasource.DriverManagerDataSource-->
<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=false&
useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="sundan"/>
</bean>
3.配置sqlSessionFactory对象
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--可以在这里绑定mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--注册mapper.xml文件-->
<property name="mapperLocations" value="classpath:com/spring/dao/UserMapper.xml"/>
</bean>
4.sqlSession的配置
<!--sqlSession的配置-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能使用构造器注入sqlSessionFactory:源码中没有set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
5.编写UserMapper实现类UserMapperImpl
public class UserMapperImpl implements UserMapper {
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession){
this.sqlSession = sqlSession;
}
@Override
public List<User> queryAllUsers() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.queryAllUsers();
}
}
6.将第五步中的UserMapperImpl 实现类注入到spring中
<!--将UserMapperImpl注入到spring中-->
<bean id="userMapper" class="com.spring.dao.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
7.测试
@Test
public void queryAllUsersTest() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-dao.xml");
UserMapper userMapper = applicationContext.getBean("userMapper", UserMapper.class);
List<User> users = userMapper.queryAllUsers();
for (User user : users) {
System.out.println(user);
}
}
方式二
使用SqlSessionDaoSupport用来提供 SqlSession。调用 getSqlSession() 方法得到一个 SqlSessionTemplate,之后可以用于执行 SQL 方法
省去了我们方式一中的第4步和第6步
需要继承SqlSessionDaoSupport类
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {
@Override
public List<User> queryAllUsers() {
return getSqlSession().getMapper(UserMapper.class).queryAllUsers();
}
}
<!--UserMapperImpl2注入到spring中-->
<bean id="userMapper2" class="com.spring.dao.UserMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>