Spring整合mybatis并使用druid连接池(纯注解模式)

1.用maven导入所需要的依赖

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>

2.数据库信息配置文件

首先在resource目录下创建一个db.properties文件,里面写上数据库连接的属性

3.读取配置信息

        这里注意DataSource是DruidDataSource的接口,按道理来说返回接口就行了,但是实际上返回接口会报错,这里干脆直接返回实现类

package com.yc.pro1.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;

@Configuration
public class JdbcConfig {

    @Value("${uname}")
    private String username;

    @Value("${password}")
    private String password;

    @Value("${url}")
    private String url;

    @Value("${driverClassName}")
    private String driverClassName;


    @Bean()
    public DruidDataSource druidDataSource() {
        DruidDataSource ds = new DruidDataSource();
        ds.setUsername(username);
        ds.setPassword(password);
        ds.setUrl(url);
        ds.setDriverClassName(driverClassName);
        return ds;
    }
}

4.创建MyBatis的配置类

        

@Configuration
public class MybatisConfig {

    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DruidDataSource dataSource) {
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        ssfb.setTypeAliasesPackage("com.yc.pro1.domain");
        ssfb.setDataSource(dataSource);
        return ssfb;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.yc.pro1.dao");
        return msc;
    }
}

5.创建主配置类

@Configuration
@ComponentScan("com.yc.pro1")
@PropertySource("classpath:db.properties")
public class SpringConfig {

}

5.在Dao层创建一个接口

        其中@Update代表要执行更新操作,#{}里面的值代表传入的参数

@Repository
public interface AccountDao {
    @Update("update tb_account set money=money+#{money} where id=#{id}")
    void addMoney(int id, double money);

    @Update("update tb_account set money=money-#{money} where id=#{id}")
    void subMoney(int id, double money);

    @Select("select * from tb_account where id=#{id}")
    List<Account> show(int id);

    @Select("select * from tb_account")
    List<Account> showAll();
}

6.Service层中创建接口

@Service
@Transactional  // 声明该类中的方法都应该在事务中执行
public interface AccountService {
    void addMoney(int id, double money);

    void subMoney(int id, double money);

    List<Account> show(int id);

    List<Account> showAll();
}

7.实现Service中的接口

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    @Override
    public void addMoney(int id, double money) {
        accountDao.addMoney(id, money);
    }

    @Override
    public void subMoney(int id, double money) {
        accountDao.subMoney(id, money);
    }

    @Override
    public List<Account> show(int id) {
        return accountDao.show(id);
        // 可以根据需要在这里处理查询结果或者返回结果
    }

    @Override
    public List<Account> showAll() {
        return accountDao.showAll();
    }
}

8.封装数据

要写上getset方法和构造方法

public class Account implements Serializable {
    Integer id;
    String name;
    Double money;

    public Account(Integer id, String name, Double money) {
        this.id = id;
        this.name = name;
        this.money = money;
    }

9.测试

@Configuration
@ComponentScan("com.yc.pro1")
public class App {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(App.class);
        AccountService bean = ac.getBean(AccountService.class);
        List<Account> accounts =bean.showAll();
        System.out.println(accounts);
    }
}

运行结果如下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值