Mabtis复习日志--mybatis和spring的整合-三种方式
使用mybatis的配置文件
注:所有的对象和spring整合,都是将对象的实例化交给spring
1.在spring的配置文件中添加
<bean id="sessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
2.mybatis保持原有的配置
3.代码中的使用
注入SQLSessionFactory这个对象。
package com.test;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import com.bean.UserBean;
import com.mapper.UserMapper;
@Component(value = "testMyBatis_1")
public class TestMyBatis_1 {
@Autowired
private SqlSessionFactory sessionFactory;
private void query() {
System.out.println(this.sessionFactory);
//手工打开Session
SqlSession sqlSession = this.sessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<UserBean> userList = userMapper.queryUserList();
for (UserBean userBean : userList) {
System.out.println(userBean.getUserid() + "\t" + userBean.getUsername());
}
//需要手工关闭
sqlSession.close();
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
TestMyBatis_1 testMyBatis_1 = (TestMyBatis_1) context.getBean("testMyBatis_1");
testMyBatis_1.query();
}
}
使用mybatis和mapperSanner
把mapper的实例化交给spring,直接注入接口就好
spring中的增加的配置
org.mybatis.spring.mapper.MapperScannerConfigurer增加对mapper接口的扫描
<!--对于mybatis的配置的使用-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
<property name="basePackage" value="com.mapper"/>
</bean>
mybatis的配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="NO_LOGGING"/>
<setting name="cacheEnabled" value="true"/>
</settings>
<plugins>
<plugin interceptor="com.plugins.MybatisInterceptor"></plugin>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"/>
<property name="offsetAsPageNum" value="false"/>
</plugin>
</plugins>
<!--这个数据源在spring配置完,环境就可以不要-->
<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://127.0.0.1:3306/lk?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="12345678"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/userMapper.xml"/>
</mappers>
</configuration>
测试代码:
package com.test;
import com.bean.UserBean;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestTwo {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
UserService service = (UserService) applicationContext.getBean("userService");
UserBean bean=service.getone(1);
System.out.println(bean.getUsername()+"\t"+bean.getPwd());
}
}
不用mybatis的文件
<bean id="sessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="com.bean"></property>
<property name="plugins">
<array>
<bean class="com.plugin.MybatisInterceptor"></bean>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<props>
<prop key="dialect">mysql</prop>
</props>
</property>
</bean>
</array>
</property>
<property name="mapperLocations"
value="classpath:mapper/*.xml">
</property>
<property name="configuration">
<bean class="org.apache.ibatis.session.Configuration">
<property name="logImpl"
value="org.apache.ibatis.logging.stdout.StdOutImpl"></property>
<property name="cacheEnabled" value="true"></property>
</bean>
</property>
</bean>
<!--开启对包的扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName"
value="sessionFactory"></property>
<property name="basePackage" value="com.mapper"></property>
</bean>
注意点:方法不能使用private进行修饰。使用public