步骤
1.导入相关jar包
- junit
- mybatis
- mysql
- spring相关
- aop
- spring-mybatis
- spring-jdbc
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<!--spring操作数据库需要导入springjdbc包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.16</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!--DataSource:使用spring的数据源替代Mybatis的配置c3p0 dbcp druid-->
<!--我们这里使用Spring提供的JDBC:org.springframework.jdbc.datasource.DriverManagerDataSource-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true"/>
<property name="username" value="root"/>
<property name="password" value="qq2420582566"/>
</bean>
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath*:com/yf/mapper/*.xml"/>
</bean>
<!--SqlSessionTemplate:就是我们使用的sqlSession-->
<bean id="SqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能使用构造器注入的方法,因为它没有set方法-->
<constructor-arg ref="sqlSessionFactory"/>
</bean>
public class UserMapperImpl implements UserMapper {
//我们所有的操作,都是使用SQLSession来执行,现在都使用SQLSessionTemplate来执行;
private SqlSessionTemplate sqlSessionTemplate;
public UserMapperImpl(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
@Override
public List<User> selectUser() {
UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
List<User> users = mapper.selectUser();
return users;
}
}
<bean id="userMapperImpl" class="com.yf.mapper.UserMapperImpl">
<constructor-arg ref="SqlSessionTemplate"/>
</bean>
@Test
public void test01() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapperImpl = context.getBean("userMapperImpl", UserMapper.class);
List<User> users = userMapperImpl.selectUser();
for (User user : users) {
System.out.println(user);
}
}
注意
- 不可以在两个地方同时注册mapper
mybatis-config.xml里面和spring-bean.xml里面的 - 我们通常将spring配置文件分来
在spring-dao中编写sqlsession的配置,在applicationContext中配置bean
在applicationContext中导入spring-dao就好了,作为总的文件。
实现方式二使用sqlSessionDaoSupport
- 编写接口实现类
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {
@Override
public List<User> selectUser() {
return getSqlSession().getMapper(UserMapper.class).selectUser();
}
}
- 注册bean
<bean id="userMapperImpl2" class="com.yf.mapper.UserMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>