Spring整合Mybatis和Juint
spring整合mybatis直接舍弃springmybatis.xml配置文件,使用整合后mybatis后能够简化代码,直接将SqlSession对象封装起来
这是为整合之前的mybatis
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="jdbc.properties"></properties> <typeAliases> <package name="com.domain"/> </typeAliases> <environments default="mysql"> <environment id="mysql"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <package name="com.dao"/> </mappers> </configuration>
package com; import com.dao.AccountDao; import com.domain.Account; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; public class App { public static void main(String[] args) throws IOException { // 1 创建SqlSessionfactoryBuilder对象 SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); // 2 加载SqlMapConfig.xml文件 InputStream input = Resources.getResourceAsStream("SqlMapConfig.xml"); // 3 创建SqlSessionFactory对象 SqlSessionFactory sessionFactory = sqlSessionFactoryBuilder.build(input); // 4 获取SqlSession SqlSession session = sessionFactory.openSession(); // 5 执行SqlSession对象 AccountDao dao = session.getMapper(AccountDao.class); Account a = dao.findbyId(1); System.out.println(a); // 6 释放资源 session.close(); } }
在整合的过程中需要配置SpringConfig、JdbcConfig、MybatisConfig
首先要引入坐标
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.18</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency>
<!--注意版本的对应关系-->
package com.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.PropertySource; @Configuration @ComponentScan("com") @PropertySource("classpath:jdbc.properties")//加载文件,使jdbcConfig能够运行 @Import({jdbcConfig.class,MybatisConfig.class})//导入jdbcConfig public class SpringConfig { }
package com.config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import javax.sql.DataSource; 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; } }
package com.config; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.mapper.MapperScannerConfigurer; import org.springframework.context.annotation.Bean; import javax.sql.DataSource; public class MybatisConfig { @Bean public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){ SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean(); ssfb.setTypeAliasesPackage("com.domain"); ssfb.setDataSource(dataSource); return ssfb; } @Bean public MapperScannerConfigurer mapperScannerConfigurer(){ MapperScannerConfigurer msc = new MapperScannerConfigurer(); msc.setBasePackage("com.dao"); return msc; } }
mybatisConfig中一共有两个bean,第一个bean代替了.xml中的环境配置,第二个bean代替了mapper指向
注:在jdbcConfig的配置中,jdbc.properties不能直接使用username,不然会运行报错,原因是username会与Spring中的命名重复
package com; import com.config.SpringConfig; import com.dao.AccountDao; import com.domain.Account; import com.service.AccountService; import com.service.impl.AccountServiceImpl; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class App2 { public static void main(String[] args) { ApplicationContext act = new AnnotationConfigApplicationContext(SpringConfig.class); // AccountDao dao = act.getBean(AccountDao.class); // Account account = dao.findbyId(2); AccountServiceImpl service = act.getBean(AccountServiceImpl.class); Account account = service.findById(2); System.out.println(account); } }
spring整合Juint
第一步仍然是导入坐标
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.18</version> </dependency>
第二步是对test类进行注解
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = SpringConfig.class)
之后便直接可以测试了。