MyBatis 数据结构查询篇


//返回map
public interface TuringEmailDao {
   
@Select("SELECT  * FROM turing_email WHERE email_id = #{emailId}")
   
public Map<String,Object> doGetId(Integer emailId);
}

@Test
public void test2() throws IOException {
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        TuringEmailDao turingEmailDao = sqlSession.getMapper(TuringEmailDao.class);
        Map<String, Object> map = turingEmailDao.doGetId(5);
        System.out.println(map);
    } finally {
        sqlSession.close();
    }

 

 

 

级联映射查询
@Select("SELECT e.*,s.*\n" +
         "FROM `turing_email` e INNER JOIN `turing_email_mapping` s \n" +
         "ON e.`email_id` = s.`email_id` WHERE e.email_id = #{emailId}")
  @ResultType(TuringEmail.class//此处返回类型为java类
 public TuringEmail doFindById(Integer emailId);
 
@Test
public void test2() throws IOException {
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        TuringEmailDao turingEmailDao = sqlSession.getMapper(TuringEmailDao.class);
        TuringEmail turingEmail = turingEmailDao.doFindById(5);
        System.out.println(turingEmail);
    } finally {
        sqlSession.close();
    }
}

 

 

//当返回多条数据时用List集合接收
@Select("SELECT e.*,s.*\n" +
         "FROM `turing_email` e INNER JOIN `turing_send_email_status` s \n" +
         "ON e.`email_id` = s.`email_id` WHERE e.email_id = #{emailId}")
  @ResultType(TuringEmail.class//此处返回类型为一个包含java类的List集合
 public List<TuringEmail> doFindById(Integer emailId);

 

@Test
public void test2() throws IOException {
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        TuringEmailDao turingEmailDao = sqlSession.getMapper(TuringEmailDao.class);
        List<TuringEmail> turingEmail = turingEmailDao.doFindById(5);
        System.out.println(turingEmail);
    } finally {
        sqlSession.close();
    }

 

  

//一对一级联分步查询

public String doFindById() {
   
return new SQL() {{
       
SELECT("email_id", "site_id", "user_id", "emailTheme", "sendStatus", "delFlag").
               
FROM("turing_email").
               
WHERE("email_id= #{emailId}");
   
}}.toString();
}

配置级联分步映射
@Results(value = {
       
@Result(id = true, column = "email_id", property = "emailId"),
       
@Result(property = "turingEmailMappingMapper", column = "email_id",
               
one = @One(select = "com.jbit.dao.TuringEmailMappingMapperDao.doFindById"
       
))
})

配置SQL语句及建立SQL语句的方法

@SelectProvider(type = TuringEmialSQLBuilder.class, method = "doFindById")
Map<String, Object> doFindById(Integer emailId) ;

 

建立SQL语句的方法

public  String  doFindById(){
        return new SQL(){{
       
SELECT("m_id","group_id","member_id").
               
FROM("turing_email_mapping").
               
WHERE("email_id = #{emailId}");
   
}}.toString();
}

@SelectProvider(type = TuringEmailMappingSQLBuulder .class,method = "doFindById")
public TuringEmailMappingMapper doFindById(Integer emailId);

 

 

@Test
public void test2() throws IOException {
   
String resource = "mybatis-config.xml";
   
InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
   
SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
       
TuringEmailMapperDao turingEmailDao = sqlSession.getMapper(TuringEmailMapperDao.class);
       
Map<String, Object> map = turingEmailDao.doFindById(5);
       
System.out.println(map);
   
} finally {
       
sqlSession.close();
    }
}

 

 

一对一、一对多级联分步查询

public String doFindById() {
   
return new SQL() {{
       
SELECT("email_id", "site_id", "user_id", "emailTheme", "sendStatus", "delFlag").
               
FROM("turing_email").
               
WHERE("email_id= #{emailId}");
   
}}.toString();
}

@Results(value = {
       
@Result(id = true, column = "email_id", property = "emailId"),
       
@Result(property = "turingEmailMappingMapper", column = "email_id",
               
one = @One(select = "com.jbit.dao.TuringEmailMappingMapperDao.doFindById"
               
)),
       
@Result(property = "turingSendEmailStatusMapperList", column = "email_id",
               
many = @Many(select = "com.jbit.dao.TuringSendEmailStatusMapperDao.doFindById"
               
))
})
@SelectProvider(type = TuringEmialSQLBuilder.class, method = "doFindById")
Map<String, Object> doFindById(Integer emailId);

 
public String doFindById(){
    return new SQL(){{
        SELECT("user_id","member_name","member_phone" ).
                FROM("turing_send_email_status").
                WHERE("email_id = #{emailId}");
    }}.toString();
}

 @SelectProvider(type = TuringEmailStatusSQL.class,method = "doFindById")
public List<TuringSendEmailStatusMapper> doFindById(Integer emailId);

 

public  String  doFindById(){
        return new SQL(){{
       
SELECT("m_id","group_id","member_id").
               
FROM("turing_email_mapping").
               
WHERE("email_id = #{emailId}");
   
}}.toString();
}

@SelectProvider(type = TuringEmailMappingSQLBuulder .class,method = "doFindById")
public TuringEmailMappingMapper doFindById(Integer emailId);

 

 

@Test
public void test2() throws IOException {
   
String resource = "mybatis-config.xml";
   
InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
   
SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
       
TuringEmailMapperDao turingEmailDao = sqlSession.getMapper(TuringEmailMapperDao.class);
       
Map<String, Object> map = turingEmailDao.doFindById(5);
       
System.out.println(map);
   
} finally {
       
sqlSession.close();
    }
}

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/***********************基本描述**********************************/ 0、根据可以单独生成javaBean后缀可以自定义 1、工具本身是非常简单的,每个人都能做就是使用模板替换生成相应文件 2、工具主要针对SpringMvc+Mybatis注解+Mysql生成对象,dao、sqlDao、interface、实现接口 3、根据生成Excel 4、生成成功后倒入到自己对应的项目中,然后Ctrl+Shipt+O(Eclipse快速倒入包)实现 5、里面因为运用的是注解,所以很多包我就没有提供了因为这些都是很基础的东西,不会的同学可以去网上查看搭建Mybatis的注解 6、生成了些什么,具体主要是对单的增、删、改、查(分页) /********************************/ /********************************/ /*************完全免费***********/ /********************************/ /********************************/ 如果大家喜欢可以再给我提其他功能,有时间我加上 /*********************************************************************************/ 模板介绍: MySql.Data.dll :连接Mysql基本dl我们的的驱动。 foxjava.exe :直接运行程序 xml : Excel文件夹 ##### TemplateXml.xml 根据数据库对应生成字段描述,生成后最好用WPS打开,然后重新另存为office认识的Excel template : 文件生成模板(非常重要的不能修改) ##### BasePojo.template 所有基础对象都要继承,方便序列化(系统自动生成) ##### Pager.template 分页对象 (系统自动生成) ##### dao.template 数据库接口Dao(mybatis接口方式,在方法上写sql,复杂的使用sqlProvider) ##### daoSqlProvider.template 复杂sql提供者 ##### service.template 对外开放的接口 ##### serviceImpl.template 实现开放接口,基本数据操作逻辑 /*********************************************************************************/

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值