服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Spring整合Mybatis具体代码实现流程

Spring整合Mybatis具体代码实现流程

2022-12-15 15:36仰望星空的快乐 Java教程

这篇文章主要介绍了Spring整合Mybatis实操分享,文章首先通过介绍Mybatis的工作原理展开Spring整合Mybatis的详细内容,需要的小伙伴可以参考一下

原始方式读取mybatis配置文件,获取SqlSession SqlSessionFactory 等

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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中的所有方法都可以调用)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?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>

新加的类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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();
    }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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,

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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();
    }
}

调用是一样的

?
1
2
3
4
5
6
7
8
9
10
11
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());
    }
}

到此这篇关于Spring整合Mybatis具体代码实现流程的文章就介绍到这了,更多相关Spring整合Mybatis内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/sharesb/article/details/124773382

延伸 · 阅读

精彩推荐