使用mybatis有四个步骤:
- 写mapper
- 写mapper.xml
- 写mybatis-config.xml
- 加载mybatis-config
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管理 。