Spring整合Mybatis及Junit

一、Spring整合Mybatis思路分析

1.1 环境准备

步骤1:准备数据库表

Mybatis是来操作数据库表,所以先创建一个数据库及表

create database spring_db character set utf8;
use spring_db;
create table tbl_account(
    id int primary key auto_increment,
    name varchar(35),
    money double
);

插入测试数据如下

步骤2:创建项目导入jar包

项目的pom.xml添加相关依赖

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.10.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.16</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.6</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.47</version>
    </dependency>
</dependencies>

步骤3:根据表创建模型类

public class Account implements Serializable {
​
    private Integer id;
    private String name;
    private Double money;
    //setter...getter...toString...方法略    
}

步骤4:创建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);
}

步骤5:创建Service接口和实现类

public interface AccountService {
​
    void save(Account account);
​
    void delete(Integer id);
​
    void update(Account account);
​
    List<Account> findAll();
​
    Account findById(Integer id);
​
}
​
@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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值