# 怎么使用Spring操作MySQL?

  1. 导必要的包
<!-- Spring Context 上下文 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.14</version>
</dependency>
<!-- Spring Jdbc  -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.3.14</version>
</dependency>
<!-- Mybatis Spring 整合的桥梁 -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.7</version>
</dependency>
<!-- Mybatis 核心包 -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.9</version>
</dependency>
<!-- MySQL 驱动 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.28</version>
</dependency>
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
  1. 从Mapper层开始,写CRUD
@Repository
public interface UserMapper {
    @Insert("insert into student(name) values('蔡徐坤')")
    void insertOne();
}
1
2
3
4
5
  1. Service层调用,这里加了事务
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Transactional(rollbackFor = Exception.class)
    public void test() {
        userMapper.insertOne();
        throw new NullPointerException();
    }
}
1
2
3
4
5
6
7
8
9
10
  1. 测试Service层是否能调用成功
public class  {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
        UserService userService = applicationContext.getBean(UserService.class);
        userService.test();
    }
}
1
2
3
4
5
6
7

Application类通过IOC容器(AnnotationConfigApplicationContext)去加载配置类(AppConfig),配置类复制给IOC里丢必须的Bean

  1. 看看配置类的写法

这个类配置了Spring组件扫描目录,配置了MyBatis的Mapper文件扫描目录,配置了数据源配置文件的路径等。Ioc容器启动时,加载这个类所有的配置到容器中

@Configuration
@ComponentScan("com.biubiu.demo")
@MapperScan("com.biubiu.demo.mapper")
@PropertySource("classpath:application.properties")
@EnableTransactionManagement
public class AppConfig {
    @Value("${spring.datasource.url}")
    private String url;
    @Value("${spring.datasource.username:admin}") // 不配置用户名取默认写死的
    private String user;
    @Value("${spring.datasource.password:123456}") // 不配置密码取默认写死的
    private String password;
    // 事务管理器
    @Bean
    public TransactionManager transactionManager() {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource());
        return transactionManager;
    }
    // 数据源 (DriverManagerDataSource是 Spring对JDBC标准数据源的 一个简单实现类)
    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUrl(url);
        dataSource.setPassword(password);
        dataSource.setUsername(user);
        return dataSource;
    }
    // MyBatis 核心类,SqlSessionFactory
    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
        return sqlSessionFactoryBean.getObject();
    }
}
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

# 到了SpringBoot

SpringBoot帮我们吧AppConfig这类写好了....大写的牛皮,我们就不用写那么一大堆配置了,而且提供了一个个的打包好的套件starter给我们使用

  1. 比如 spring-boot-starter 这个包就包括
  • spring-context (aop、beans、core、express)
  • spring-jcl logback slf4j 等日志框架
  1. 再比如 mybatis-spring-boot-starter 就包括
  • spring-boot-starter 么错,就是1里的那个,也就是包含了Spring的全套
  • spring-jdbc spring-tx
  • HiKariCP MyBatis MyBatis-Spring

看基本上MyBatis用到的全部有了

SpringBoot有一句经典话语,约定大于配置 也就是说你没特殊需求,你啥页不用配置,默认的配置就够用了...