IT袋

当前位置:主页 > 经验教程 > 建站编程 >

Spring整合Mybatis

Spring整合Mybatis Spring与Mybatis整合(2)

时间:2024-01-30 18:00:31 来源:IT袋 作者:马勇
导读:Spring整合Mybatis,准备service和dao层基础代码 @Datapublic class Account { private Integer id; //主键 private String name; //用户名 private Double money; //金额} public interface AccountDao { @Insert("inse

Spring整合Mybatis

  1. 准备service和dao层基础代码
@Data
public class Account {
    private Integer id;  //主键
    private String name;  //用户名
    private Double money;  //金额
}
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);
}
/**
 * 业务接口
 */
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(Integer id) {
        accountDao.delete(id);
    }
    public Account findById(Integer id) {
        return accountDao.findById(id);
    }
    public List<Account> findAll() {
        return accountDao.findAll();
    }
}

开始整合

【第一步】导入Spring整合Mybatis依赖

上面的pom.xml文件中已经导入

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.10.RELEASE</version>
</dependency>
<!--mybatis整合spring-->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.3.0</version>
</dependency>
【第二步】创建JdbcConfig配置DataSource数据源
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_db
jdbc.username=root
jdbc.password=root
/**
 * 数据库配置类
 */
public class JdbcConfig {
    @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
    public DataSource dataSource(){
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ds.setUsername(userName);
        ds.setPassword(password);
        return ds;
    }
}
【第三步】创建MybatisConfig整合mybatis

注:MapperScannerConfigurer不能与@Value放在同一个配置类中,否则会导致@Value失效,因为MapperScannerConfigurer会比PropertySource先加载

相关阅读

  • web标准包括哪些 web标准主要包括

    web标准包括哪些 web标准主要包括

    本文为您带来的是web标准包括哪些方面的介绍,web标准包括结构(Structure)、表现(Presentation)和行为(Behavior)三部分,对应的标准也分三方面:结构化标准语言主要包括XHTML和XML,表现标准

  • 服务器搭建ip教程 自己搭建公网ip服务器的方法

    服务器搭建ip教程 自己搭建公网ip服务器的方法

    小编带来的是服务器搭建ip教程和自己搭建公网ip服务器的方法的方法内容,一起来了解了解吧。 1、购买站群IP服务器,并让主机商配置好服务器环境 当企业购买完站群服务器IP时,记得要及时

  • 零基础3Dmax入门教程 从零开始学习3Dmax的入门指南

    零基础3Dmax入门教程 从零开始学习3Dmax的入门指南

    今天小编详解零基础3Dmax入门教程方面的介绍,下面IT袋网为您详细介绍 目前说到建模软件,最为大众所熟知的,那当然是老大哥——3Dmax了! 3Dmax效果逼真,功能强大,不仅广泛使用于室内建

  • ping ttl传输中过期是什么原因和解决方法介绍

    ping ttl传输中过期是什么原因和解决方法介绍

    常在河边走,哪有不湿鞋,有时候就连网络维护人员可能都会遇到 ping ip时报TTL传输过期 的问题,不知道是什么原因引起的,下面IT袋小编就给大家分享下哪些原因会引起 ping ttl传输中过期 。