1.整合Mybatis
导入相关jar包
-
junit
-
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency>
-
-
mybatis
-
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency>
-
-
mysql数据库
-
<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.2.0.RELEASE</version> </dependency> <!-- spring操作数据库,还需要spring-jdbc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.0.RELEASE</version> </dependency>
-
-
aop织入
-
<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>
-
-
偷懒Lombok
-
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency>
-
1.1Mybatis-spring
1.编写数据源配置(和mybatis的配置文件里面的内容一样)
<!-- DataSource:使用spring的数据源替换Mybatis的配置
这里使用Spring提供的jdbc:org.springframework.jdbc.datasource-->
<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"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
2.sqlSessionFactory
<!-- sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 绑定数据源dataSource-->
<property name="dataSource" ref="datasource"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<!-- 绑定mybatis配置文件(非必要),这样mabtis配置文件内容也可以一起使用了-->
<!-- <property name="configLocation" value="classpath:mybatis-config.xml"/>-->
</bean>
3.sqlSessionTemplate
<!-- 获取SqlSession,相当于mybatis中通过工具类获取SqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!-- 只能使用构造器注入sqlSessionFactory因为它没有set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
4.需要给接口加实现类
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.将自己写的实现类注入到Spring
<!-- 将写的接口实现类注入到spring-->
<bean id="userMapper" class="mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
6.测试使用
@Test
public void test02() throws Throwable{
ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
for (User user:
userMapper.selectuser()) {
System.out.println(user);
}
}
1.2继承SqlSessionDaoSupport类实现
1.编写接口的实现类
public class UserMapperImpl02 extends SqlSessionDaoSupport implements UserMapper{
@Override
public List<User> selectuser() {
SqlSession sqlSession = getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.selectuser();
}
}
- 继承SqlSessionDaoSupport类,就可以直接在类里面通过getSqlSession()方法获取sqlsession,然后再获取mapper,再执行方法即可
2.按照第一种方法更改配置文件
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- DataSource:使用spring的数据源替换Mybatis的配置
这里使用Spring提供的jdbc:org.springframework.jdbc.datasource-->
<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"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!-- sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 绑定数据源dataSource-->
<property name="dataSource" ref="datasource"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 方式二-->
<bean id="userMapper2" class="mapper.UserMapperImpl02">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
- 直接将获取sqlsession的步骤用继承来实现。