• Mybatis,Spring整合


    Mybatis,Spring整合

    步骤:

    1. 导入相关jar包
      • junit
      • mybatis
      • mysql
      • spring
      • aspectjweaver
      • mybatis-spring

    回忆mybatis

    1. 编写实体类
    2. 编写核心配置文件
    3. 编写接口
    4. 编写Mapper.xml
    5. 测试

    mybatis-spring

    spring-dao.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!--    spring提供的jdbc-->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=ture&amp;characterEncoding=UTF-8"/>
            <property name="username" value="root"/>
            <property name="password" value="123456"/>
        </bean>
        <!--    sqlSessionFactory-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="configLocation" value="classpath:Mybatis-config.xml"/>
            <property name="mapperLocations" value="classpath:cn/pinked/mapper/*.xml"/>
        </bean>
        <!--    sqlSessionTemplate,就是原来的sqlSession-->
        <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
            <constructor-arg index="0" ref="sqlSessionFactory"/>
        </bean>
    </beans>
    

    applicationContext.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    	<!--    导入-->
        <import resource="spring-dao.xml"/>
    
        <bean id="userMapper" class="cn.pinked.mapper.UserMapperImpl">
            <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
        </bean>
    
    </beans>
    

    userMapperImpl.java

    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();
        }
    }
    

    SqlSessionDaoSupport

    UserMapperImpl2.java

    public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {
        public List<User> selectUser() {
            SqlSession sqlSession = getSqlSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            return mapper.selectUser();
        }
    }
    

    applicationContext.xml

    <bean id="userMapper2" class="cn.pinked.mapper.UserMapperImpl2">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
    
  • 相关阅读:
    Spring4整合hibernate4
    Conference Search不错的学术会议日程提示网站
    python---解决“Unable to find vcvarsall.bat”错误
    PCV 学习笔记-ch1 主成分分析实现
    QQ客服代码,支持临时会话
    [TL-WR845N V4] 无线桥接(WDS)设置 + 迅捷桥接配置
    解决win2008下IIS7的HTTP500错误
    为IIS增加PHP支持
    解决mysql"Access denied for user'root'@'IP地址'"问题
    Windwos2008如何关闭IE增强的安全配置
  • 原文地址:https://www.cnblogs.com/pinked/p/12207849.html
Copyright © 2020-2023  润新知