能够掌握注解开发定义Bean对象
能够掌握纯注解开发模式
能够配置注解开发依赖注入
能够配置注解开发管理第三方Bean
能够配置注解开发为第三方Bean注入资源
能够使用Spring整合Mybatis
能够使用Spring整合Junit
mybatis进行数据层操作的核心对象是谁?



问题1:Spring整合mybatis的依赖叫什么?
问题2:Spring整合mybatis需要管理配置哪两个Bean,这两个Bean作用分别是什么?
xxxxxxxxxx
public interface AccountService { void save(Account account); void delete(Integer id); void update(Account account); List<Account> findAll(); Account findById(Integer id);}public class AccountServiceImpl implements AccountService { 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 into tbl_account(name,money)values(#{name},#{money})") void save(Account account); ("delete from tbl_account where id = #{id} ") void delete(Integer id); ("update tbl_account set name = #{name} , money = #{money} where id = #{id} ") void update(Account account); ("select * from tbl_account") List<Account> findAll(); ("select * from tbl_account where id = #{id} ") Account findById(Integer id);}
xxxxxxxxxx
<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>
xxxxxxxxxx
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/spring_db?useSSL=falsejdbc.username=rootjdbc.password=root
xxxxxxxxxx
public class JdbcConfig { ("${jdbc.driver}") private String driver; ("${jdbc.url}") private String url; ("${jdbc.username}") private String userName; ("${jdbc.password}") private String password; public DataSource dataSource(){ DruidDataSource ds = new DruidDataSource(); ds.setDriverClassName(driver); ds.setUrl(url); ds.setUsername(userName); ds.setPassword(password); return ds; }}
xxxxxxxxxx
public class MybatisConfig { //定义bean,SqlSessionFactoryBean,用于产生SqlSessionFactory对象 public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){ SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean(); ssfb.setTypeAliasesPackage("com.itheima.domain"); ssfb.setDataSource(dataSource); return ssfb; } //定义bean,返回MapperScannerConfigurer对象 public MapperScannerConfigurer mapperScannerConfigurer(){ MapperScannerConfigurer msc = new MapperScannerConfigurer(); msc.setBasePackage("com.itheima.dao"); return msc; }}
xxxxxxxxxx
("com.itheima")//@PropertySource:加载类路径jdbc.properties文件("classpath:jdbc.properties")({JdbcConfig.class,MybatisConfig.class})public class SpringConfig {}
xxxxxxxxxx
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的两个注解作用分别是什么?
xxxxxxxxxx
<!--junit--><dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version></dependency><!--spring整合junit--><dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.9.RELEASE</version></dependency>
xxxxxxxxxx
//【第二步】使用Spring整合Junit专用的类加载器(SpringJUnit4ClassRunner.class)//【第三步】加载配置文件或者配置类(classes = {SpringConfiguration.class}) //加载配置类//@ContextConfiguration(locations={"classpath:applicationContext.xml"})//加载配置文件public class AccountServiceTest { //支持自动装配注入bean private AccountService accountService; public void testFindById(){ System.out.println(accountService.findById(1)); } public void testFindAll(){ System.out.println(accountService.findAll()); }}
==注意:junit的依赖至少要是4.12版本,可以是4.13等版本,否则出现如下异常:==
