目录
一、spring整合mybatis
1、回顾mybatis
第一步加依赖:
第二步提供接口和mapper文件:
第三步编写mybatis主配置文件:
第四步加载配置文件获取代理生成的接口实现类对象:
2、spring整合mybatis
第一步加依赖:
第二步提供接口和mapper文件:
第三步配置类中第三方bean放入spring容器:
第四步编写spring主配置文件,将用到的配置类、资源文件加载进来:
第五步加载配置文件获取代理生成的接口实现类对象:
3、spring整合junit单元测试 (课外小知识)
第一步加依赖:
第二步在测试类上加注解,编写方法测试:
一、spring整合mybatis
1、回顾mybatis
我是这样理解的,单独用mybatis我们的步骤如下:
第一步加依赖:
mybatis依赖:
org.mybatis mybatis3.5.6
mysql依赖:
mysql mysql-connector-java5.1.47
第二步提供接口和mapper文件:
可以通过注解的方式将接口和mapper文件(sql语句)二合一:
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 findAll();
@Select("select * from tbl_account where id = #{id} ")
Account findById(Integer id);
}
第三步编写mybatis主配置文件:
定义别名,连接信息,mapper扫描器,资源文件扫描器
第四步加载配置文件获取代理生成的接口实现类对象:
public class App {
public static void main(String[] args) throws IOException {
// 1. 创建SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 2. 加载SqlMapConfig.xml配置文件
InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml.bak");
// 3. 创建SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
// 4. 获取SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 5. 执行SqlSession对象执行查询,获取结果User,,,获取到了动态代理生成的实现类对象
AccountDao accountDao = sqlSession.getMapper(AccountDao.class);
}
}
2、spring整合mybatis
第一步加依赖:
第一步加依赖:
mybatis依赖:
org.mybatis mybatis3.5.6
mysql依赖:
mysql mysql-connector-java5.1.47
spring依赖:
org.springframework spring-context5.2.10.RELEASE
mybatis-spring依赖:
org.mybatis mybatis-spring1.3.0
spring连接jdbc依赖:
org.springframework spring-jdbc5.2.10.RELEASE
druid连接池依赖(会用到):
第二步提供接口和mapper文件:com.alibaba druid1.1.16
可以通过注解的方式将接口和mapper文件(sql语句)二合一:
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 findAll();
@Select("select * from tbl_account where id = #{id} ")
Account findById(Integer id);
}
第三步配置类中第三方bean放入spring容器:
懂得都懂,我就不做过多赘述了。我的理解是将SqlSessionFactoryBean(实际上指的是SqlSessionFactory)和MapperScannerConfigurer两个bean放入spring容器中,spring就会通过动态代理创建出接口的实现类对象来。
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;
}
}
第四步编写spring主配置文件,将用到的配置类、资源文件加载进来:
引入其他配置类,标签扫描器,资源文件扫描器
@Configuration
@ComponentScan("com.itheima")
//@PropertySource:加载类路径jdbc.properties文件
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class,MybatisConfig.class})
public class SpringConfig {
}
第五步加载配置文件获取代理生成的接口实现类对象:
这里没有直接获取生成的dao接口代理类对象,而是直接从容器里获取的service对象,因为我已经把dao接口的实现类对象注入到了service属性中去了,可以通过service对象来调用dao接口实现类对象。
public class App2 {
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);
}
}
3、spring整合junit单元测试 (课外小知识)
第一步加依赖:
junit
junit
4.12
test
org.springframework
spring-test
5.2.10.RELEASE
第二步在测试类上加注解,编写方法测试:
设置类运行器
@RunWith(SpringJUnit4ClassRunner.class)
设置Spring环境对应的配置类
@ContextConfiguration(classes = {SpringConfiguration.class}) //加载配置类
//设置类运行器
@RunWith(SpringJUnit4ClassRunner.class)
//设置Spring环境对应的配置类
@ContextConfiguration(classes = {SpringConfiguration.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());
}
}
好,先到这里,午休,下午干aop和spring事务。



