文章目录
- Spring整合mybatis
- 思路分析
- Mybatis程序核心对象分析
- 整合Mybatis
- 代码实现
- Spring整合Junit
- 修改成警告
Spring整合mybatis
思路分析
Mybatis程序核心对象分析
上面图片是mybatis的代码,上述有三个对象,分别是sqlSessionFactory,sqlSession,accountDao这三个对象,那么哪个是核心对象,哪些类适合交给Spring来管理,因为spring默认是单例的,所有他管理的核心对象最好是无状态的,单例的,对于sqlSessionFactory来说它只创建一次,而sqlSession每次用的时候都要获取,用完还要关闭,而accountDao更不用说了,它本来就是由sqlSession创建的。(就像是在没学框架之前写那个mybatis工具类时只需在工具类中创建一次sqlSessionFactory对象,之后都是由工具类创建sqlSession对象)所以核心对象就是sqlSessionFactory对象
整合Mybatis
- 使用SqlSessionFactoryBean封装SqlSessionFactory需要的环境信息
- 使用MapperScannerConfigurer加载Dao接口,创建代理对象保存到IOC容器中
代码实现
前置工作
创建与数据库表对应的实体类,还有dao接口并写对应的方法以及对应的sql语句
public class Student {
private Integer id;
private String name;
public Student() {
}
public Student(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
public interface StudentDao {
@Select("select * from student where id = #{id}")
Student findById(Integer id);
}
- 导入依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.11</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
</dependencies>
- 创建JdbcConfig配置DataSource数据源
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_db?useSSL=false
jdbc.username=root
jdbc.password=root
JdbcConfig
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String userName;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource dataSource(){
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(userName);
ds.setPassword(password);
return ds;
}
}
- 创建MybatisConfig整合Mybatis
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;
}
}
- 创建SpringConfig主配置类进行包扫描和加载其他配置类
@Configuration
@ComponentScan("com.itheima")
//@PropertySource:加载类路径jdbc.properties文件
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class,MybatisConfig.class})
public class SpringConfig {
}
- 定义测试类进行测试
public class App {
public static void main( String[] args ) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
StudentDao studentDao = ctx.getBean(StudentDao.class);
Student student = studentDao.findById(3);
System.out.println(student);
}
}
Spring整合Junit
- 导入依赖坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
- 测试哪个类在test包下创建相同包的类(规范)
- 使用Spring整合junit专用的类加载器和加载配置类
//【第二步】使用Spring整合Junit专用的类加载器
@RunWith(SpringJUnit4ClassRunner.class)
//【第三步】加载配置文件或者配置类
@ContextConfiguration(classes = {SpringConfiguration.class}) //加载配置类
//@ContextConfiguration(locations={"classpath:applicationContext.xml"})//加载配置文件
public class AccountServiceTest {
//支持自动装配注入bean
@Autowired
private StudentDao studentDao;
@Test
public void testFindById(){
Student student = studentDao.findById(1);
System.out.println(student);
}
}
注意:junit的依赖至少要是4.12版本,可以是4.13等版本,否则出现如下异常
修改成警告
如何将上图中箭头处修改成警告,看下图
鼠标点击studentDao处,按alt+enter键