• spring整合mybatis


    数据库配置文件

    jdbc.driver=com.mysql.cj.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/dormitory?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true
    jdbc.username=root
    jdbc.password=root
    

    创建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"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/aop
            https://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <!--    注入数据库配置文件-->
        <context:property-placeholder location="db.properties"/>
    
    <!--    注入DataSource-->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="${jdbc.driver}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>
    
    <!--    注入SqlSessionFactory-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!--        注入DataSource-->
            <property name="dataSource" ref="dataSource"/>
    <!--        绑定所有的Mapper-->
            <property name="mapperLocations" value="classpath*:mapper/*.xml"/>
    
        </bean>
    
    <!--    注入SqlSession-->
        <bean id="sqlSession" 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"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/aop
            https://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <!--    导入Mybatis配置-->
        <import resource="spring-dao.xml"/>
    
        <bean id="adminMapper" class="com.lh.mapper.impl.AdminMapperImpl">
            <property name="sqlSessionTemplate" ref="sqlSession"/>
        </bean>
    
    
    </beans>
    

    创建实体类Admin(属性名要与数据库字段对应)

    package com.lh.pojo;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import lombok.ToString;
    
    /**
     * @author LiHao
     * @create 20201110 23:33
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    public class Admin {
        private Integer a_id;
        private String a_username;
        private String a_password;
        private String a_name;
        private String a_power;
        private String a_describe;
    }
    
    

    创建AdminMapper

    package com.lh.mapper;
    
    import com.lh.pojo.Admin;
    
    import java.util.List;
    
    /**
     * @author LiHao
     * @create 20201110 23:46
     */
    public interface AdminMapper {
        public List<Admin> getAdminList();
    }
    

    创建AdminMapperImpl

    package com.lh.mapper.impl;
    
    import com.lh.mapper.AdminMapper;
    import com.lh.pojo.Admin;
    import org.mybatis.spring.SqlSessionTemplate;
    
    import java.util.List;
    
    /**
     * @author LiHao
     * @create 20201110 23:47
     */
    public class AdminMapperImpl implements AdminMapper {
    
        private SqlSessionTemplate sqlSessionTemplate;
    
        public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
            this.sqlSessionTemplate = sqlSessionTemplate;
        }
    
        public List<Admin> getAdminList() {
            AdminMapper adminMapper=sqlSessionTemplate.getMapper(AdminMapper.class);
            return adminMapper.getAdminList();
        }
    }
    

    创建与AdminMapper对应的adminmapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <!--一个mapper与一个接口绑定-->
    <mapper namespace="com.lh.mapper.AdminMapper">
        <select id="getAdminList" resultType="com.lh.pojo.Admin">
            select * from d_admin
        </select>
    </mapper>
    
  • 相关阅读:
    django.db.utils.OperationalError: (1071, 'Specified key was too long; max key length is 767 bytes');
    mysql的utf8与utf8mb4 异同;utf8mb4_unicode_ci 与 utf8mb4_general_ci 如何选择
    /etc/skel 目录作用
    react组件引用时的default常见错误
    Java效率工具之Lombok
    Java问题定位之如何借助线程堆栈进行问题分析
    Java线程堆栈分析
    Java问题定位之Java线程堆栈分析
    Java项目性能瓶颈分析及定位(八)——Java线程堆栈分析(五)
    SpringMVC的各种参数绑定方式
  • 原文地址:https://www.cnblogs.com/haheihei/p/13956740.html
Copyright © 2020-2023  润新知