- package com.example.Configure;
-
- import com.alibaba.druid.pool.DruidDataSource;
- import com.example.Beans.otherBeans;
- import org.mybatis.spring.SqlSessionFactoryBean;
- import org.mybatis.spring.annotation.MapperScan;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.*;
-
- import javax.sql.DataSource;
-
- @Configuration // todo 标注当前类是一个配置类(替代配置文件)、其中包含@Compoent注解
- //
- @ComponentScan({"com.example"})
-
- //
- @PropertySource("jdbc.properties")
-
- //
- @Import(otherBeans.class)
-
- // Mapper接口扫描
- @MapperScan("com.example.Mapper")
- public class SpringConfig {
- @Bean // 将非自定义的bean对象交给Spring容器管理
- public DataSource dataSource(@Value("${jdbc.driver}") String driver,
- @Value("${jdbc.url}") String url,
- @Value("${jdbc.username}") String username,
- @Value("${jdbc.password}") String password) {
- DruidDataSource dataSource = new DruidDataSource();
- dataSource.setDriverClassName(driver);
- dataSource.setUrl(url);
- dataSource.setUsername(username);
- dataSource.setPassword(password);
- return dataSource;
- }
-
- @Bean
- public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
- SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
- sqlSessionFactoryBean.setDataSource(dataSource);
- return sqlSessionFactoryBean;
- }
-
-
- }
与数据库建立连接的同时,扫描指定的mapper接口,实现实现数据库的操作
- package com.example.Mapper;
-
- import com.example.pojo.Emp;
- import org.springframework.stereotype.Repository;
-
- import java.util.List;
-
- @Repository
- public interface EmpMapper {
- List
findAll(); - }
- "1.0" encoding="UTF-8" ?>
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.example.Mapper.EmpMapper">
- <select id="findAll" resultType="com.example.pojo.Emp">
- select *
- from tb_emp;
- select>
- mapper>
业务层调用持久层
- package com.example.Service.Impl;
-
-
- import com.example.Mapper.EmpMapper;
- import com.example.Service.UserService;
- import com.example.pojo.Emp;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import java.util.List;
-
- @Service("userService")
- public class UserServiceImpl implements UserService {
- @Autowired
- private EmpMapper empMapper;
-
- @Override
- public void show() {
- List
empList = empMapper.findAll(); - for (Emp emp : empList) {
- System.out.println(emp);
- }
- }
-
-
- }
上述中直接注入的mapper接口类
测试代码
- package com.example.Test;
-
-
- import com.example.Configure.SpringConfig;
- import com.example.Service.UserService;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
-
- public class TestApplicationContext {
- public static void main(String[] args) {
- // 注解方式加载Spring容器的核心配置类
- ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
- UserService bean = context.getBean(UserService.class);
- bean.show();
- }
- }
-
运行结果如下:

小结
用注解的方式整合第三方框架,以MyBatis框架为例,首先得与数据库建立连接的操作由配置文件转换为配置类,使用@Bean注解,Spring框架会自动调用这两个方法,并生成对应的bean对象交给Spring容器管理,与数据库成功建立连接。然后在业务层直接注入Mapper接口对象,调用其中的方法,实现对于数据库的操作。