mybatis思想
mybatis是一个用Java编写的持久层框架,它使用ORM实现了结果集的封装。
ORM是Object Relational Mapping 对象关系映射。简单来说,就是把数据库表和实体类及实体类的属性对应起来,让开发者操作实体类就实现操作数据库表。
它封装了jdbc操作的很多细节,使开发者只需要关注sql语句本身,而无需关注注册驱动,创建连接等烦杂过程
1.首先创建一个数据库,利用mybatis来进行操作
2.导入项目的相关依赖
3.创建一个模型类来实现set和get方法
4.创建Dao接口,对数据库进行操作
5.创建Service接口和实现类
6.添加jdbc.properties
//useSSL:关闭mysql的SSL连接
7.添加mybatis核心配置文件
8.编写运行程序
整合思路
真正需要交给Spring管理的是SqlSessionFactory
整合Mybatis,就是将Mybatis用到的内容交给Spring管理,分析下配置文件
configuration>
<!--读取外部properties配置文件-->
<properties resource="jdbc.properties"></properties>
//读取外部properties配置文件,Spring有提供具体的解决方案@PropertySource ,需
要交给Spring
<!--别名扫描的包路径-->
<typeAliases>
<package name="com.itheima.domain"/>
</typeAliases>
//起别名包扫描,为SqlSessionFactory服务的,需要交给Spring
<!--数据源-->
//主要用于做连接池,Spring之前我们已经整合了Druid连接池,这块也需要交给
Spring
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}">
</property>
<property name="password" value="${jdbc.password}">
</property>
</dataSource>
</environment>
</environments>
//上面三个标签都是为了创建SqlSession对象用的,那么用Spring管理SqlSession对象吗?
回忆下SqlSession是由SqlSessionFactory创建出来的,所以只需要将
SqlSessionFactory交给Spring管理即可。
<!--映射文件扫描包路径-->
<mappers>
<package name="com.itheima.dao"></package>
</mappers>
</configuration>
//是Mapper接口和映射文件[如果使用注解就没有该映射文件],这个是在获取到
SqlSession以后执行具体操作的时候用,所以它和SqlSessionFactory创建的时机都不在
同一个时间,可能需要单独管理。
开始整合
第一件事是:Spring要管理MyBatis中的SqlSessionFactory
第二件事是:Spring要管理Mapper接口的扫描
1.导入jar包
<dependency>
<!--Spring操作数据库需要该jar包-->
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<!--
Spring与Mybatis整合的jar包
这个jar包mybatis在前面,是Mybatis提供的
-->
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
2.创建Spring主配置类SpringConfig
配置类注解
@Configuration
//包扫描,主要扫描的是项目中的AccountServiceImpl类
@ComponentScan("com.itheima")
public class SpringConfig {
}
3.创建数据源的配置类JdbcConfig @Import
4.主配置文件读入jdbc.properties @PropertySource
5.创建mybatis配置类并配置SqlSesessionFactory
public class MybatisConfig {
//定义bean,SqlSessionFactoryBean,用于产生SqlSessionFactory对象
@Bean
public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
//设置模型类的别名扫描
ssfb.setTypeAliasesPackage("com.itheima.domain");
//设置数据源
ssfb.setDataSource(dataSource);
return ssfb;
}
//定义bean,返回MapperScannerConfigurer对象
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("com.itheima.dao");
return msc;
}
}
整合后的代码区别
6.主配置类中引入mybatis配置类 @Import
7.编写运行文件
public static void main(String[] args) {
ApplicationContext ctx = new
AnnotationConfigApplicationContext(SpringConfig.class);
AccountService accountService = ctx.getBean(AccountService.class);
Account ac = accountService.findById(1);
System.out.println(ac);
}
Spring整合Junit
1.创建test实现类AccountServiceTest
2.导入依赖
3.test类的书写
@RunWith(SpringJUnit4ClassRunner.class)
//设置Spring环境对应的配置类
@ContextConfiguration(classes = {SpringConfig.class}) //加载配置类
//@ContextConfiguration(locations={"classpath:applicationContext.xml"})//加载配置文件
public class AccountServiceTest {
//支持自动装配注入bean
@Autowired
private AccountService accountService;
@Test
public void testFindById(){
System.out.println(accountService.findById(1));
}
@Test
public void testFindAll(){
System.out.println(accountService.findAll());
}
}
涉及知识点