整合mybatis

整合Mybatis

步骤:

1.导入相关jar包

  • junit
  • mybatis
  • mysql数据库
  • spring相关的
  • aop织入
  • mybatis-spring

2.编写配置文件

3.测试

复习mybatis

  1. 编写实体类
    @Data
    public class User {
    private int id;
    private String pwd;
    private String name;
    }

  2. 编写核心配置文件







        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="com.mysql.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf8"/>
                    <property name="username" value="root"/>
                    <property name="password" value="123456"/>
                </dataSource>
            </environment>
        </environments>
    <!--注册mapper-->
        <mappers>
            <mapper class="com.xxx.mapper.UserMapper"/>
        </mappers>
    </configuration>
    
  3. 编写接口
    public interface UserMapper {
    public List selectUser();
    }

  4. 编写Mapper.xml

    <mapper namespace="com.xxx.mapper.UserMapper">
    
    <select id="selectUser" resultType="user">
        select * from mybatis.user;
    </select>
    </mapper>
    
  5. 测试
    public class MyTest {

        @Test
        public void test() throws IOException {
            String resources="mybatis-config.xml";
            InputStream in = Resources.getResourceAsStream(resources);
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
            SqlSession sqlSession = sessionFactory.openSession(true);
    
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            List<User> userList = mapper.selectUser();
            for (User user : userList) {
                System.out.println(user);
            }
        }
    }
    

    mybatis-spring

    1. 编写数据源

      <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
              <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
              <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf8"/>
              <property name="username" value="root"/>
              <property name="password" value="123456"/>
          </bean>
      
    2. sqlSessionFactory

       <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
              <property name="dataSource" ref="dataSource"/>
      <!--绑定Mybatis配置文件-->
              <property name="configLocation" value="classpath:mybatis-config.xml"/>
              <property name="mapperLocations" value="classpath:com/xxx/mapper/UserMapper.xml"/>
          </bean>
      
    3. sqlSessionTemplate

      <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
      <!-- 只能通过构造器注入sqlSessionFactory,因为它没有set方法-->
              <constructor-arg index="0" ref="sqlSessionFactory"/>
          </bean>
      
    4. 需要给接口加实现类
      public class UserMapperImpl implements UserMapper{

          //我们所有的操作,原来都使用sqlSession来执行,现在都使用SqlSessionTemplate
          private SqlSessionTemplate sqlSession;
      
          public void setSqlSession(SqlSessionTemplate sqlSession) {
              this.sqlSession = sqlSession;
          }
      
          public List<User> selectUser() {
              UserMapper mapper = sqlSession.getMapper(UserMapper.class);
              return mapper.selectUser();
          }
      }
      
    5. 将自己写的实现类,注入到Spring中

          <!--mapper注册文件-->
          
      <import resource="spring-dao.xml"/>
      
          <bean id="userMapper" class="com.xxx.mapper.UserMapperImpl">
              <property name="sqlSession" ref="sqlSession"/>
          </bean>
      
      </beans>
      
    6. 测试使用即可
      public class MyTest {

          @Test
          public void test() throws IOException {
      
              ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
              UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
              for (User user : userMapper.selectUser()) {
                  System.out.println(user);
              }
      
posted @ 2021-08-05 10:00  青歌与  阅读(38)  评论(0)    收藏  举报