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

xml映射(利用映射完成helloworld)

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

xml映射(利用映射完成helloworld)

    什么是xml映射
    MyBatis提供了注解定义SQL与Mapper接口的映射关系。但是注解不能解决复杂的映射关系,例如sql拼接,所以需要使用xml映射。

    helloWorld程序

    文件结构

someMapper.xml




    
        select 'hello world'
    

namespace对应DemoMapper地址id对应方法名,此处是demomapper中的test方法resultType对应方法的返回值,test返回值为String

DemoMapper

package cn.tedu.mapper;

import org.apache.ibatis.annotations.Select;

public interface DemoMapper {
    @Select("select empno,ename,sal,deptno from emp where empno=1;")
    String hello();

    String test();
}

MybatisConfig.java

//配置SqlSessionFactory的作用是告诉mybatis如何找到目标数据库
    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource,
           @Value("${mybatis.mapper.location}") Resource[] mapperLocation)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        //将所有的Mapper映射xml文件注入给MyBatis
        bean.setMapperLocations(mapperLocation);
        return bean.getObject();
    }

@Value("${mybatis.mapper.location}") Resource[] mapperLocation获取映射关系xml,从jdbc.properties中获取bean.setMapperLocations(mapperLocation);注入MyBatis

jdbc.properties

db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/newdb3?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
db.username=root
db.password=
db.maxActive=10
db.initialSize=2
mybatis.mapper.location=classpath:mappers/*.xml
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/723770.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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