栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring学习之整合mybatis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring学习之整合mybatis

代码来自黑马程序员

要想将mybatis和Spring结合使用,首先要做的就是,在maven得pom.xml中导入mybatis中使用Spring相关得jar包坐标 如下:


  org.mybatis
  mybatis-spring
  1.3.0

 对应得Spring和mybatis结合使用的话是需要操作数据库的所以还需要导入Spring管理jdbc的坐标


    
      org.springframework
      spring-jdbc
      5.2.10.RELEASE
    

 配置好后再在配置类的包里首先要写的就是jdbc相关的配置

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;
    }
}

在jdbcConfig配置类中,使用@Bean来管理jdbc数据源的对象,由自动装配和通过配置文件给变量赋值

接在再写

ublic class MybatisConfig {
    //定义bean,SqlSessionFactoryBean,用于产生SqlSessionFactory对象
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        ssfb.setTypeAliasesPackage("com.itheima.domain");
        ssfb.setDataSource(dataSource);
        return ssfb;
    }
    //定义bean,返回MapperScannerConfigurer对象
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.itheima.dao");
        return msc;
    }
}

Mybatis的配置类,SqlSessionFactoryBean 是 mybatis 使用Spring提供jar包中提供的,是一个创建SqlSessionFactory bean 的类,

这个两个bean可以看作是一个mybatis的另一种形式

ssfb.setTypeAliasesPackage("com.itheima.domain");设置数据的封装类

ssfb.setDataSource(dataSource);设置连接数据库需要的资源,例如用户名密码路径端口号等等,这些在jdbcConfig中被设置成一个bean,在mybatisConfig中通过自动装配拿到并设置好值

msc.setBasePackage("com.itheima.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 findAll();

    @Select("select * from tbl_account where id = #{id} ")
    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 findAll() {
        return accountDao.findAll();
    }
}

实现的接口

public interface AccountService {

    void save(Account account);

    void delete(Integer id);

    void update(Account account);

    List findAll();

    Account findById(Integer id);

}

还有一个数据库对应封装类

public class Account implements Serializable {

    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 +
                ", name='" + name + ''' +
                ", money=" + money +
                '}';
    }
}

主类里

public class App2 {
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);

        AccountService accountService = ctx.getBean(AccountService.class);

        Account ac = accountService.findById(2);
        System.out.println(ac);
    }
}

 请仔细阅读该代码,最好有mybatis相关的基础知识

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1004163.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号