Spring整合Mybatis
仰望星空的快乐 人气:0原始方式读取mybatis配置文件,获取SqlSession SqlSessionFactory 等
package com.atguigu.rj1192.zyk; 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 java.io.IOException; import java.io.InputStream; import java.util.List; public class TestMybatis { public static void main(String[] args) throws IOException { //从配置文件中构建SqlSessionFactory String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //创建一个SqlSession对象(获取自动事务管理的session) SqlSession session = sqlSessionFactory.openSession(true); //获取Mapper对象(反射,设计模式之代理模式) UserMapper mapper = session.getMapper(UserMapper.class); User user= new User(); user.setUsername("sdfs"); user.setPassword("123456"); user.setPhone("123456578909"); user.setStatus(1); mapper.insert(user); System.out.println(mapper.selectById(3)); } }
这种方式就是将 SqlSession SqlSessionFactory等类,全部用bean标签放到ioc容器中,
如果这样的话,我只能从ioc中获得到sqlsession,要使用接口的方法,还需要getbean(),不方便,可以再加一个类,它去getbean(),并把这个类放进ioc容器中,使用方法的时候直接从ioc中拿这个类,再.方法名就行了
这个新的类,有mapper生成的sqlsession,sqlsession中有mapper的所有的方法,所以这个类中要有sqlsession的所有方法,即实现 接口(有点绕,就是为了调用这个类的时候,mapper中的所有方法都可以调用)
<?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"> <!-- dataSource 使用spring的数据源替换mybatis的连接池 还可以用c3p0 dbcp--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://127.0.0.1:3306/account?serverTimezone=UTC"></property> <property name="username" value="root"></property> <property name="password" value="mysql"></property> </bean> <!-- sqlsessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 绑定mybatis的配置文件--> <!-- 这个文件可以写别名,和绑定的mapper 但是为了方便管理,这两项单独写个mybatis-config.xml 再导入该文件即可--> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!-- sqlsessionTemplate 就是我们用的sqlsession--> <bean id="sqlsession" class="org.mybatis.spring.SqlSessionTemplate"> <!-- 因为没有set方法,只能使用构造器注入--> <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg> </bean> <bean id="accountdaoimpl" class="com.atguigu.rj1192.zyk.dao.AccoountDaoImpl"> <property name="sqlSession" ref="sqlsession"></property> </bean> </beans>
新加的类
package com.atguigu.rj1192.zyk.dao; import com.atguigu.rj1192.zyk.pojo.Account; import org.apache.ibatis.session.SqlSession; import org.mybatis.spring.SqlSessionTemplate; import java.util.List; public class AccoountDaoImpl implements AccountDao { public SqlSessionTemplate sqlSession; public void setSqlSession(SqlSessionTemplate sqlSession) { this.sqlSession = sqlSession; } @Override public List<Account> selectall() { AccountDao accountDao= sqlSession.getMapper(AccountDao.class); return accountDao.selectall(); } }
import com.atguigu.rj1192.zyk.dao.AccountDao; import com.atguigu.rj1192.zyk.pojo.Account; 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.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; import java.io.InputStream; import java.util.List; public class test { @Test public void query() throws IOException { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("Spring-dao.xml"); AccountDao accountadoimpl = (AccountDao) applicationContext.getBean("accountdaoimpl"); System.out.println(accountadoimpl.selectall()); } }
第二种方法 那个新的类 继承SqlSessionDaoSupport,配置文件就可以只配置sqlSessionFactory和datasource,
<?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"> <!-- dataSource 使用spring的数据源替换mybatis的连接池 还可以用c3p0 dbcp--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://127.0.0.1:3306/account?serverTimezone=UTC"></property> <property name="username" value="root"></property> <property name="password" value="mysql"></property> </bean> <!-- sqlsessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 绑定mybatis的配置文件--> <!-- 这个文件可以写别名,和绑定的mapper 但是为了方便管理,这两项单独写个mybatis-config.xml 再导入该文件即可--> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> 将新的类放进ioc容器中 <bean id="AccoountDaoImpl2" class="com.atguigu.rj1192.zyk.dao.AccoountDaoImpl2"> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> </beans>
package com.atguigu.rj1192.zyk.dao; import com.atguigu.rj1192.zyk.pojo.Account; import org.apache.ibatis.session.SqlSession; import org.mybatis.spring.support.SqlSessionDaoSupport; import java.util.List; public class AccoountDaoImpl2 extends SqlSessionDaoSupport implements AccountDao{ @Override public List<Account> selectall() { // 和第一种是一样的,只是将赋值写在了 SqlSessionDaoSupport类中,不用自己在xml中赋值了 //getSqlSession();父类中的方法 SqlSession sqlSession=getSqlSession(); AccountDao accountDao=sqlSession.getMapper(AccountDao.class); return accountDao.selectall(); } }
调用是一样的
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class test { @Test public void query() throws IOException { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationcontext.xml"); AccoountDaoImpl2 accountadoimpl = (AccoountDaoImpl2) applicationContext.getBean("AccoountDaoImpl2"); System.out.println(accountadoimpl.selectall()); } }
加载全部内容