zoukankan      html  css  js  c++  java
  • 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>
    
  • 相关阅读:
    今天面试一些程序员(新,老)手的体会
    UVA 10635 Prince and Princess
    poj 2240 Arbitrage
    poj 2253 Frogger
    poj 2485 Highways
    UVA 11258 String Partition
    UVA 11151 Longest Palindrome
    poj 1125 Stockbroker Grapevine
    poj 1789 Truck History
    poj 3259 Wormholes
  • 原文地址:https://www.cnblogs.com/haheihei/p/13956740.html
Copyright © 2011-2022 走看看