Spring整合mybatis
思路分析
MyBatis程序核心对象分析

整合MyBatis
- 使用SqlSessionFactoryBean封装SqlSessionFactory需要的环境信息

- 使用MapperScannerConfigurer加载Dao接口,创建代理对象保存到IOC容器中

代码实现
- 前置工作:
- 在pom.xml中添加spring-context、druid、mybatis、mysql-connector-java等基础依赖。
- 准备service和dao层基础代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| public interface AccountService {
void save(Account account);
void delete(Integer id);
void update(Account account);
List<Account> findAll();
Account findById(Integer id);
} @Service public class AccountServiceImpl implements AccountService {
@Autowired private AccountDao accountDao;
public void save(Account account) { accountDao.save(account); }
public void update(Account account){ accountDao.update(account); }
public void delete(Integer id) { accountDao.delete(id); }
public Account findById(Integer id) { return accountDao.findById(id); }
public List<Account> findAll() { return accountDao.findAll(); } } public interface AccountDao {
@Insert("insert into tbl_account(name,money)values(#{name},#{money})") void save(Account account);
@Delete("delete from tbl_account where id = #{id} ") void delete(Integer id);
@Update("update tbl_account set name = #{name} , money = #{money} where id = #{id} ") void update(Account account);
@Select("select * from tbl_account") List<Account> findAll();
@Select("select * from tbl_account where id = #{id} ") Account findById(Integer id); }
|
导入Spring整合Mybatis依赖
1 2 3 4 5 6 7 8 9 10 11
| <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.10.RELEASE</version> </dependency>
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency>
|
创建JdbcConfig配置DataSource数据源
1 2 3 4
| jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/spring_db?useSSL=false jdbc.username=root jdbc.password=123456
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class MybatisConfig { @Bean public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){ SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean(); ssfb.setTypeAliasesPackage("edu.heuet.domain"); ssfb.setDataSource(dataSource); return ssfb; } @Bean public MapperScannerConfigurer mapperScannerConfigurer(){ MapperScannerConfigurer msc = new MapperScannerConfigurer(); msc.setBasePackage("edu.heuet.dao"); return msc; } }
|
创建SpringConfig主配置类进行包扫描和加载其他配置类
1 2 3 4 5 6 7
| @Configuration @ComponentScan("edu.heuet")
@PropertySource("classpath:jdbc.properties") @Import({JdbcConfig.class,MybatisConfig.class}) public class SpringConfig { }
|
定义测试类进行测试
1 2 3 4 5 6 7 8 9 10
| public class App { 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单元测试
导入整合的依赖坐标spring-test
1 2 3 4 5 6 7 8 9 10 11 12
| <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.9.RELEASE</version> </dependency>
|
使用Spring整合Junit专用的类加载器
加载配置文件或者配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringConfiguration.class})
public class AccountServiceTest { @Autowired private AccountService accountService;
@Test public void testFindById(){ System.out.println(accountService.findById(1)); }
@Test public void testFindAll(){ System.out.println(accountService.findAll()); } }
|
注意:junit的依赖至少要是4.12版本,可以是4.13等版本,否则出现如下异常:
