Spring整合Mybatis操作数据库
作为一个新技术,Spring在对老技术的支持上已经是非常全面,而Mybatis同时向Spring提供了对应的依赖,以保证能够完美兼容
依赖导入
需要引入的依赖有:
spring-context | spring-jdbc | mybatis | mybatis-spring | mysql-connector-java | druid | lombok
搭建三层架构

实现步骤
配置一个名叫jdbc.properties的数据源用来存放关于jdbc的信息
配置类
Spring配置类
- 使用@Configurable声明
- 设置扫描com.itheima下的文件
- 引入对应的数据源(jdbc.properties)
- 导入Jdbc配置类和Mybatis配置类
Jdbc配置类
这个类主要是将数据源中的数据取出并通过druid的DataSource方法使用
MybatisConfig
将Mybatis的xml简写
domain层
pojo层Getter、Setter
Dao层
利用注解设置增删改查方法
Service层
设置Service层接口和实现类
实现类中
利用自动装配的dao层,将参数传到Service中
运行类
具体实现
数据库

配置类
SpringConfig
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.itheima.config;
import org.springframework.beans.factory.annotation.Configurable; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.PropertySource;
@Configurable
@ComponentScan("com.itheima")
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class,MybatisConfig.class}) public class SpringConfig { }
|
JdbcConfig
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
| package com.itheima.config;
import com.alibaba.druid.pool.DruidDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.itheima.config;
import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.mapper.MapperScannerConfigurer; import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
public class MybatisConfig { @Bean public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){ SqlSessionFactoryBean ssf=new SqlSessionFactoryBean(); ssf.setTypeAliasesPackage("com.itheima.domain"); ssf.setDataSource(dataSource); return ssf; } @Bean public MapperScannerConfigurer mapperScannerConfigurer(){ MapperScannerConfigurer msc=new MapperScannerConfigurer(); msc.setBasePackage("com.itheima.dao"); return msc; } }
|
domain层
Account
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 37 38 39 40 41 42
| package com.itheima.domain;
import java.io.Serializable;
public class Account { private Integer id; private String name; private Double money;
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Double getMoney() { return money; }
public void setMoney(Double money) { this.money = money; }
@Override public String toString() { return "Account{" + "id=" + id + ", name='" + name + '\'' + ", money=" + money + '}'; } }
|
Dao层
接口AccountDao
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.itheima.Dao;
import com.itheima.domain.Account; import org.apache.ibatis.annotations.*;
import java.util.List;
public interface AccountDao { @Insert("insert into tbl_account(id,name,money)values(#{id},#{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> selectAll();
@Select("select * from tbl_account where id = #{id} ") Account findById(Integer id); }
|
service层
接口AccountService
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.itheima.service;
import com.itheima.domain.Account;
import java.util.List;
public interface AccountService { void save(Account account);
void delete(int id);
void update(Account account);
List<Account> selectAll(Account account); }
|
实现类AccountServiceImpl
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 37 38
| package com.itheima.service.impl;
import com.itheima.Dao.AccountDao; import com.itheima.domain.Account; import com.itheima.service.AccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
import java.util.List;
@Service public class AccountServiceImpl implements AccountService { @Autowired private AccountDao accountDao;
public void save(Account account){ accountDao.save(account); }
@Override public void delete(int id) { accountDao.delete(id); }
public void update(Account account){ accountDao.update(account); } @Override public List<Account> selectAll(Account account) { return accountDao.selectAll(); } }
|
运行类
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 37 38 39
| package com.itheima;
import com.itheima.config.SpringConfig; import com.itheima.domain.Account; import com.itheima.service.AccountService; import com.itheima.service.impl.AccountServiceImpl; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.util.List;
public class App { public static void main(String[] args) { ApplicationContext crx=new AnnotationConfigApplicationContext(SpringConfig.class); AccountService accountService=crx.getBean(AccountService.class);
Account account=new Account(); account.setId(3); account.setMoney(400.0); account.setName("王五"); accountService.save(account); accountService.delete(1);
Account account=new Account(); account.setId(3); account.setName("赵六"); account.setMoney(500.0); accountService.update(account);
Account account=new Account(); List<Account> list1=accountService.selectAll(account); System.out.println(list1); } }
|