发表于: 2020-06-03 20:31:39

3 1516


今日作为:  完成任务1-17使用Mybatis


                                                特别重要!!!建议有官网的去官网看文档,网上的太坑爹

 今日收获 :看了一天的Mybatis的文档,收获说不上来但是问题一大堆!!!


          

问题1:

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  

<property name="mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper" /> 

  <property name="sqlSessionFactory" ref="sqlSessionFactory" /></bean>

 第一句这段话能直接用id来使用UserMapper(是个接口,里面有@Select "SQL语句"),说明Mybatis在幕后生成了这个接口的对象并且实现了方法?

 证据是因为Java规定接口不能直接使用?


 第二句是把UserMapper丢到mapperInterface里面去?说明mapperInterface是个有参数的方法?而且属于MapperFactoryBean?


下面的第三句话又是啥意思?


虽然我用的是Java语句实现的Mybatis但是很好奇,这东西又查不到,想请教一下师兄。


问题2:官网的文档上面说Mybatis是动态SQL,查资料说了很多,但是我感觉最大的和JDBC或者Spring JDBC Template的区别是不需要用类似

String sql = "select * from students";
Connection conn = DatabaseConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);

这样的语句了?

但少了这样的语句,难道不是因为像问题1一样Mybatis在幕后做的工作么?那不就还是静态的么?


问题3:发现使用MyBatis用Java语句实现的时候全都是面对的系统给的对象,但是感觉代码没少多少,使用MyBatis的最大好处其实是因为都是对系统给

            的对象的set方法进行操作?所以更加的符合封装和面向对象编程的特性?


问题4:在使用MyBatis的时候我感觉不同包中的类相互紧密的联系在一起(MyBatis在幕后做的工作?),那这样不就不符合解耦的要求了么?

            在之前使用Spring JDBC Template的时候我记得在教程看见过说Spring JDBC Template可以解耦,而且对JDBC数据持久化还有模板支持,所以为什么还要学习Mybatis。。。。至少这两天我看的教程里面又有一篇文章提到过这个问题。



问题5:为什么我的Mybatis输出数据库的结果是乱的?程序能跑,没有任何报错,也麻烦师兄看一下


目录结构:

数据库结构:

结果:


代码


dao:

package com.jnshu.dao;


import org.apache.ibatis.annotations.Select;


import java.util.List;
import java.util.Map;

public interface StudentDao {
@Select("select * from students")
public List<Map<String,Object>> query();
}

mybatis_config:

package com.jnshu.mybatis;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;


import javax.sql.DataSource;
@Configuration
@ComponentScan({"com.jnshu"})
@MapperScan("com.jnshu.dao")

public class Mybatis_config {
@Bean
   public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
       factoryBean.setDataSource(dataSource);
       return factoryBean.getObject();
   }
@Bean
   public DataSource dataSource() {
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
       driverManagerDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
       driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/it-xzy?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=true");
       driverManagerDataSource.setUsername("root");
       driverManagerDataSource.setPassword("54229103w");
       return driverManagerDataSource;

   }
}

service:

package com.jnshu.service;

import com.jnshu.dao.StudentDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class StudentService {
@Autowired
   StudentDao studentDao;

   public void query(){
System.out.println(studentDao.query());
   }
}

test:

import com.jnshu.mybatis.Mybatis_config;
import com.jnshu.service.StudentService;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class test {
@Test
       public static void main(String[] args) {
AnnotationConfigApplicationContext
ac = new AnnotationConfigApplicationContext(Mybatis_config.class);
           ac.getBean(StudentService.class).query();
       }
}


个人猜想是不是因为我没有了对应表字段的实体类?然后沙雕Mybatis自己生成的接口实现类对象出问题了?


以上是今天的疑问,收获感觉真说不上来,感觉懂怎么用了也感觉没懂。。。。。明天再去看看



明天的计划:1.继续熟悉一下Mybatis,@Select和@MapperScan("")倒是真的懂了。。。。至少是干嘛的

                     2.继续做1-17后面的任务,虽然我好像1-17顺便做了不少。。。。

                     3.把连接数据库操作像Spring JDBC Template一样丢到XML里面去 好看也方便修改





            


返回列表 返回列表
评论

    分享到