1.Spring整合mybatis
前提了解:
spring 管理 mybatis
mybatis 管理 mapper
要依赖于德鲁伊连接池
spring 管理sqlsessionfactory
思路
Spring整合Mybatis主要做两件事:
1.使用Spring管理MyBatis中的SqlSessionFactory
2.使用Spring管理Mapper接口的扫描
主要使用MybatisConfig配置文件中的SqlSessionFactoryBean和MapperScannerConfigurer这两个类来完成
1.pom.xml增加
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring_mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<!--导入jdk版本-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<!--导入spring的坐标spring-context,对应版本是5.2.10.RELEASE-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!--导入druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<!--导入mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!--导入mysql 对应版本是5.1.46RELEASE-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!--导入spring整理jdbc的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!--导入spring整理mybatis的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
</project>
2.准备service和dao层基础代码
//service
public interface AccountService {
void save(Account account);
void delete(Integer id);
void update(Account account);
List<Account> findAll();
Account findById(Integer id);
}
//service实现类
@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();
}
}
//dao
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);
}
3.在resources下创建
jdbc.prepertios
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_db?useSSL=false
jdbc.username=root
jdbc.password=320321
4.创建JdbcConfig配置DataSource数据源
//注入第三方数据源 jdbc.properties的数据
public class JdbcConfig {
//私有四个数据 加value获取
@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 //加注解bean 表示当前方法的返回值是一个bean对象,添加到IOC容器中
//直接创建数据源 添加四大金刚 返回对象
public DataSource dataSource() {
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}
}
5.在config创建核心配置类
@Configuration //表名自身是核心配置类
@ComponentScan("com.itheima") //扫包
@PropertySource("classpath:jdbc.properties") //读取properties配置文件
@Import({JdbcConfig.class,MybatisConfig.class}) //快速导入
public class SpringConfig {
}
6.创建MybatisConfig整合mybatis.xml
//mybatisconfig.java代替mybatisconfig.xml文件的操作
public class MyBatisConfig {
//定义bean,SqlSessionFactoryBean,用于产生SqlSessionFactory对象
@Bean //使用@bean注解 注入引用类型 在形参加对应的参数
public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
//需要代替mybatis_config.xml的文件
//设置类型别名的包
ssfb.setTypeAliasesPackage("com.itheima.domain");
//替代dataSource 设置数据源 先在加形参
ssfb.setDataSource(dataSource);
return ssfb;
}
//设置代理对象
//定义bean,返回MapperScannerConfigurer对象
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("com.itheima.dao");
return msc;
}
}
7.定义测试类进行测试
public class App {
public static void main(String[] args) {
//基于配置文件
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
//获取bean
AccountService bean = context.getBean(AccountService.class);
Account byId = bean.findById(2);
System.out.println(byId);
}
}
2.Spring整合Junit单元测试
主要是两个注解
//使用Spring整合Junit专用的类加载器
@RunWith(SpringJUnit4ClassRunner.class) //spring专用类
@ContextConfiguration(classes= SpringConfig.class) //加载配置类 注意要加classes
.导入整合的依赖坐标spring-test
<!--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>
2.在test创建com.itheima.service
//1.使用Spring整合Junit专用的类加载器
@RunWith(SpringJUnit4ClassRunner.class) //spring专用类
@ContextConfiguration(classes= SpringConfig.class) //加载配置类 注意要加classes
public class AccountServiceTest {
//自动装配
@Autowired
private AccountService accountService;
@Test
public void testFindAll(){
List<Account> accounts = accountService.findAll();
System.out.println(accounts);
}
@Test
public void testFindById(){
System.out.println(accountService.findById(1));
}
}