12.整合Mybatis
12.1.步骤:
1.导入相关jar包
- junit
- mybatis
- mysql数据库
- spring相关的
- aop织入
- mybatis-spring【new】
2.编写配置文件
3.测试
12.2.回忆Mybatis
- 编写实体类
- 编写核心配置文件
- 编写接口
- 编写Mapper.xml
- 编写utils
- 测试类
12.3.Mybatis-spring
1.编写数据源配置
ApplicationContext-Mybatis.xml
<?xml version="1.0" encoding="UTF8"?>
<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的配置 c3p0 dbcp druid
我们这里使用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&userUnicode=ture&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--绑定mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/itxiaofei/mapper/*.xml"/>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
</beans>
ApplicationContext.xml
<?xml version="1.0" encoding="UTF8"?>
<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>
">
<!--导入Mybatis-spring配置文件-->
<import resource="ApplicationContext-Mybatis.xml"/>
<!--注入UserMapperImpl-->
<bean id="UserMapper" class="com.itxiaofei.mapper.UserMapperImpl">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
</bean>
</beans>
2.sqlSessionFactory
3.sqlSessionTemplate
4.需要给接口加实现类【***MappeImpl】
public class UserMapperImpl implements UserMapper{
//我们的所有操作,都使用sqlSession来执行,
// 现在都是用sqlSessionTemplate来执行
private SqlSessionTemplate sqlSessionTemplate;
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
public List<User> selectUser() {
UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
List<User> user = mapper.selectUser();
return user;
}
}
5.将自己写的实现类注入到Spring中
<bean id="UserMapper" class="com.itxiaofei.mapper.UserMapperImpl">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
</bean>
6.测试即可
@Test
public void selectUser(){
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
UserMapper userMapper = context.getBean("UserMapper", UserMapper.class);
for (User user : userMapper.selectUser()) {
System.out.println(user);
}
}
SqlSessionDaoSupport
SqlSessionDaoSupport是一个抽象的支持类,用来为你提供 SqlSession。调用 getSqlSession()方法你会得到一个 SqlSessionTemplate,之后可以用于执行 SQL 方法。
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{
public List<User> selectUser() {
// SqlSession sqlSession = getSqlSession();
// UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// List<User> user = mapper.selectUser();
// return user;
// 以下为精简版
return getSqlSession().getMapper(UserMapper.class).selectUser();
}
}
标签:12,sqlSessionTemplate,UserMapper,selectUser,user,整合,Mybatis,public
From: https://www.cnblogs.com/itxiaofei/p/16845440.html