zoukankan      html  css  js  c++  java
  • Spring与Mybatis的整合

    Spring与Mybatis的整合

    方法一:

    package com.mapper;
    
    import com.pojo.User;
    import org.mybatis.spring.SqlSessionTemplate;
    
    import java.util.List;
    
    public class UserMapperImpl implements UserMapper {
    
        private SqlSessionTemplate sqlSessionTemplate;
    
        public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
            this.sqlSessionTemplate = sqlSessionTemplate;
        }
    
        public List<User> selectUser() {
            UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
            return mapper.selectUser();
        }
    }
    

    mybatis.xml

    <?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>
    
        <typeAliases>
            <package name="com.pojo"/>
        </typeAliases>
    
    </configuration>
    

    spring.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            https://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <!--data source-->
        <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://111.230.212.103:3306/mybatis?userSSL=true&amp;
                    userUnicode=true&amp;characterEncoding=UTF-8"/>
            <property name="username" value="root"/>
            <property name="password" value="hdk123"/>
        </bean>
    
        <!--sqlsession-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="datasource" />
            <!--bound mybatis-->
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
            <property name="mapperLocations" value="classpath:com/mapper/UserMapper.xml"/>
        </bean>
    
        <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
            <constructor-arg index="0" ref="sqlSessionFactory"/>
        </bean>
    
        <bean id="userMapper" class="com.mapper.UserMapperImpl">
            <property name="sqlSessionTemplate" ref="sqlSession"></property>
        </bean>
    
    </beans>
    

    test

    import com.mapper.UserMapper;
    import com.pojo.User;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import java.io.IOException;
    
    
    public class Mytest {
    
    
        public static void main(String[] args) throws IOException {
    
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
            UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
    
            for (User user : userMapper.selectUser()) {
                System.out.println(user);
            }
        }
    }
    

    方法二:

    <?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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!--data source-->
        <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://111.230.212.103:3306/mybatis?userSSL=true&amp;
                    userUnicode=true&amp;characterEncoding=UTF-8"/>
            <property name="username" value="root"/>
            <property name="password" value="hdk123"/>
        </bean>
    
        <!--sqlsession-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="datasource" />
            <!--bound mybatis-->
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
            <property name="mapperLocations" value="classpath:com/mapper/UserMapper.xml"/>
        </bean>
    
        <!--<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">-->
            <!--<constructor-arg index="0" ref="sqlSessionFactory"/>-->
        <!--</bean>-->
    
        <!--<bean id="userMapper" class="com.mapper.UserMapperImpl">-->
            <!--<property name="sqlSessionTemplate" ref="sqlSession"></property>-->
        <!--</bean>-->
    
        <bean id="userMapper2" class="com.mapper.UserMapperIml2">
            <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
        </bean>
    
    </beans>
    
    package com.mapper;
    
    import com.pojo.User;
    import org.apache.ibatis.session.SqlSession;
    import org.mybatis.spring.support.SqlSessionDaoSupport;
    
    import java.util.List;
    
    public class UserMapperIml2 extends SqlSessionDaoSupport implements UserMapper {
        public List<User> selectUser() {
            SqlSession sqlSession = getSqlSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            return mapper.selectUser();
        }
    }
    
  • 相关阅读:
    sql 用户自定义表类型和使用
    java idea常用快捷键
    sql强大的行转列功能(内置函数pivot及注意事项)(转载)
    while循环,for循环
    基本运算符,流程控制之if判断
    常量,基本数据类型,输入输出,基本运算符
    python解释器安装,运行python程序的方式及程序运行的步骤,变量与自动的内存管理
    计算机硬件基础知识普及,操作系统,编程语言分类
    计算机硬件组成与工作原理
    面向对象编程,类与对象的使用,内置函数
  • 原文地址:https://www.cnblogs.com/xiaxiaopi/p/14463257.html
Copyright © 2011-2022 走看看