踩大坑mevan静态资源第二次害死我,以及自己的马虎,又又又害死自己,spring-mybatis

使用mybatis有四个步骤:

  1. 写mapper
  2. 写mapper.xml
  3. 写mybatis-config.xml
  4. 加载mybatis-config
  5. package com.quxiao.util;
    
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    import org.springframework.stereotype.Component;
    
    import java.io.IOException;
    import java.io.InputStream;
    
    @Component
    public class getSession {
        private static String name = "t1.xml";
        private static InputStream inputStream;
    
        static {
            try {
                inputStream = Resources.getResourceAsStream(name);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    
        private static SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //true为增加数据时直接加入
        private static SqlSession session = sqlSessionFactory.openSession(true);
    
        public static SqlSession getSession() {
            return session;
        }
    }
    

spring简化开发(简化了个屁),代理了创建sqlsession,脑壳疼sqlsession中主要是;;连接池、sql工厂、 还有spring最常见的模板sqlSessionTemplate。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--   连接池-->
    <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/user?UseSSL=false"></property>
        <property name="username" value="root"></property>
        <property name="password" value="qx@123456"></property>
    </bean>
<!--sqlsessin工厂-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="datasource"></property>
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <property name="mapperLocations" value="classpath:*.xml"></property>
    </bean>
<!--spring的模板-->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

    <bean id="userMapper" class="com.quxiao.dao.impl.UserMapperImpl">
        <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
    </bean>

    <bean id="userMapper2" class="com.quxiao.dao.impl.UserMapper2">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>

</beans>

面对这些,我陷入了沉思。。。。。。。。。。。。。。。。。。。。。。。。。。

我认为还是接触spring太少了,所以还停留在以往的编程思想。

package com.quxiao.dao.impl;

import com.quxiao.dao.UserMapper;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

/**
 * @program: fxspring
 * @author: quxiao
 * @create: 2023-05-12 16:51
 **/
public class UserMapperImpl implements UserMapper {
    private SqlSessionTemplate sqlSessionTemplate;


    @Override
    public int select() {
        UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
        return mapper.select();
    }

    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }
}

仅仅是模板,我认为完成spring有很多方法,可以注解吧获取sqlsession设置为util,然后spring管理  。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值